Week 14 - FPL Flashcards

1
Q

What is the design of imperative languages based on?

A

Von Neumann architecture.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Why is it challenging to manage the state changes in large imperative programs?

A

Managing the state changes in large imperative programs is a daunting task due to the importance of variables and how the program’s state changes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What does a function definition specify?

A

A function definition specifies the domain and range sets along with the mapping described by an expression or table.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How are functions applied?

A

Functions are applied to elements of the domain set to yield elements of the range set.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What controls the evaluation order of mathematical functions?

A

The evaluation order is controlled by recursion and conditional expressions rather than sequencing and iterative repetition.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

How are function definitions written?

A

Function definitions are written with the name, list of parameters in parentheses, and a mapping expression.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a higher-order function or functional form?

A

A higher-order function or functional form takes functions as parameters and yields a function as its result or does both.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Describe functional composition.

A

Functional composition is a functional form that takes two functions as parameters and yields a function whose value is the first actual parameter function applied to the application of the second, e.g., h(x) ≡ f(g(x)).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

What is the apply-to-all functional form?

A

The apply-to-all functional form takes a single function as a parameter and yields a list of values obtained by applying the given function to each element of a list of parameters.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the objective of designing functional programming languages (FPLs)?

A

The objective of designing FPLs is to mimic mathematical functions.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How is the basic process of computation in FPLs different from imperative languages?

A

In FPLs, variables and assignments are not necessary, freeing the programmer from concerns of memory cells. Computation is performed through recursion rather than iterative constructs.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is referential transparency in FPLs?

A

Referential transparency means the evaluation of a function always produces the same result given the same parameters, making the semantics of FPLs simpler than imperative languages.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are primitive functions in FPLs?

A

Primitive functions are a set of basic functions provided in FPLs, with a smaller number being considered beneficial.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Why are output functions usually not needed?

A

Because the interpreter always displays the result of a function evaluated at the top level.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is a predicate function?

A

A predicate function returns a Boolean value.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What does (= x 11) return if x is 11?

A

T.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does (> x 11) return if x is 11?

A

().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does (< x 15) return if x is 11?

A

T.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What does (EVEN? x) return if x is 11?

A

().

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does (ODD? x) return if x is 11?

A

T.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the COND function used for in Scheme?

A

Multiple selection.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What does the QUOTE function do in Scheme?

A

It takes one parameter and returns it without evaluation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Why is the QUOTE function used in Scheme?

A

To avoid parameter evaluation when it is not appropriate.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How can QUOTE be abbreviated in Scheme?

A

With the apostrophe prefix operator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does (sort ‘(3 7 5 1 9)) return in Scheme?

A

(1 3 5 7 9).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is tail recursion and why is it important in Scheme?

A

A function is tail recursive if its recursive call is the last operation in the function. Tail recursive functions can be converted to use iteration by the compiler, making them faster. Scheme requires that all tail recursive functions be converted to use iteration.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

How is functional composition defined and used in Scheme?

A

Functional composition is when one function is applied to the result of another function. If h is the composition of f and g, then h(x) = f(g(x)).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

How can Scheme functions build and evaluate code?

A

In Scheme, it is possible to define a function that builds Scheme code and requests its interpretation using the interpreter function EVAL.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

How can a non-tail-recursive function be converted into a tail-recursive function in Scheme?

A

A non-tail-recursive function can be converted into a tail-recursive function by using an auxiliary helper function that carries an additional parameter to accumulate the result. This ensures that the recursive call is the last operation.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

What is functional composition, and how is it implemented in Scheme?

A

Functional composition is the process of applying one function to the result of another. In Scheme, it can be implemented using the compose function, which takes two functions f and g and returns a new function that applies f to the result of g.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

What do functional programming languages (FPLs) mimic and why is this important?

A

FPLs mimic mathematical functions, which is important because it allows for a more predictable and reliable way to manage data and behavior, as functions always produce the same output given the same input without side effects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

n functional programming, what replaces loops and why?

A

Recursion replaces loops in functional programming because it allows iteration without changing state, maintaining immutability and functional purity.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

How are macros used in Common Lisp, and what is their purpose?

A

Macros in Common Lisp are used to extend the language by allowing the creation of new syntactic constructs. They are expanded and then evaluated, enabling complex code transformations and abstractions. Users can define their own macros using DEFMACRO.

24
Q

How do functional programming languages handle data instead of using variables and assignments?

A

Functional programming languages handle data by passing it directly to functions and returning new data without altering the original data, avoiding traditional variables and assignments.

25
Q

Define referential transparency

A

Referential transparency means that a function’s output depends only on its input values and produces no side effects, ensuring consistent results and making code easier to understand and debug.

26
Q

What are the basic operations for manipulating lists?

A

Basic operations for lists include accessing the first element (head), retrieving all elements except the first (tail), obtaining all elements except the last (init), and accessing the last element (last).

26
Q

How is list concatenation performed?

A

List concatenation is performed using the ++ operator, such as [1, 3] ++ [5, 7], resulting in [1, 3, 5, 7].

27
Q

How are elements accessed by index in lists?

A

Elements in lists can be accessed by index using the !! operator, for example, [1, 2, 3, 4] !! 1 retrieves the element at index 1, resulting in 2.

27
Q

What is the syntax for creating lists?

A

Lists are created by enclosing elements within square brackets, like directions = [“north”, “south”, “east”, “west”].

28
Q

Explain the concept of pattern parameters in functions.

A

Pattern parameters allow functions to be defined based on different patterns, enabling flexible function definitions like product [] = 1 and product (a:x) = a * product x for calculating the product of a list.

29
Q

What are guards in function definitions?

A

Guards in function definitions are conditions added to lines of a function to specify when that definition can be applied, providing a clear way to handle different cases within a function.

30
Q

How do languages handle function definitions compared to each other?

A

While some languages may require reserved words to introduce function definitions, others, like Haskell, don’t, and alternative definitions of functions with different formal parameters have the same appearance.

31
Q

What are list comprehensions used for?

A

List comprehensions are used to generate new lists by applying transformations to existing lists, making it easier to create lists with specific patterns or properties.

32
Q

How is string comprehension performed?

A

String comprehension is similar to list comprehension, treating strings as lists of characters, enabling operations like pairing characters with corresponding numbers or extracting substrings.

33
Q

Explain the concept of higher-order functions with examples.

A

Higher-order functions can take other functions as parameters or return functions as results, providing powerful tools for abstraction and composition in functional programming. Examples include map, which applies a function to each element of a list, and foldr, which reduces a list to a single value by repeatedly applying a function.

34
Q

What does foldr do in functional programming?

A

Reduces a list to a single value by recursively applying a function.

35
Q

How can foldr define the length function?

A

By recursively counting elements in a list.

36
Q

How can foldr define the reverse function?

A

By appending each element to the result list in reverse order.

37
Q

What is function composition in functional programming?

A

Combining functions where the output of one becomes the input of another.

38
Q

How is the all function defined?

A

It checks if a predicate is true for all elements in a list.

39
Q

What is lazy evaluation?

A

Delays computation until necessary, improving efficiency and enabling infinite lists.

40
Q

How are infinite data structures created?

A

Using lazy evaluation to generate elements as needed.

41
Q

What is the member function?

A

Checks if an element is in a list.

42
Q

How can member be improved for infinite lists?

A

By ensuring it terminates correctly when searching for an element.

43
Q

How can you define a mutable variable in F#?

A

Using let mutable.

44
Q

What are the key features of F#?

A

Functional language with imperative and OOP features.

45
Q

What is let binding in F#?

A

A way to define values and functions.

46
Q

What is type inferencing in F#?

A

The compiler deduces types automatically.

47
Q

What are some primitive types in F#?

A

bool, byte, int, float, double, char, unit.

47
Q

What are tuples in F#?

A

Group of values of different types.

48
Q

What are generic sequences in F#?

A

Collections that support lazy evaluation.

49
Q

What is a struct tuple in F#?

A

A value type tuple stored in the stack.

50
Q

What is an immutable list in F#?

A

An ordered collection of elements that cannot be changed.

51
Q

What is currying in F#?

A

Treating functions with multiple arguments as a series of functions.

52
Q

What are sequences in F#?

A

Collections of elements generated lazily.

53
Q

What is the purpose of the rec keyword in F#?

A

To define recursive functions.

54
Q

What does List.map do in F#?

A

Applies a function to each list element and returns a new list.

55
Q

What does List.filter do in F#?

A

Filters list elements based on a predicate function.

56
Q

What does List.fold do in F#?

A

Accumulates results from a list based on an initial value.

57
Q

What is function composition in F#?

A

Combining functions where the output of one is the input of another using&raquo_space;.

58
Q

What is forward composition in F#?

A

let inline (») f g x = g (f x)

59
Q

What is the result of negateSquare 3?

A

-9

59
Q

What is reverse composition in F#?

A

let inline («) f g x = f (g x)

60
Q

What does the pipeline operator (|>) do?

A

Sends the value of its left operand to the last parameter of the call on its right.

61
Q

What is the syntax for the pipeline operator in F#?

A

let inline (|>) x f = f x

62
Q

What is the return value of evensTimesFive when myNums is [1; 2; 3; 4; 5]?

A

[10; 20]

63
Q

What is a common restriction in imperative languages for functional programming?

A

Lack of support for higher-order functions.

64
Q

What is an example of an anonymous function in C#?

A

(input-parameters) => expression

65
Q

What are some characteristics of imperative languages?

A

Complex semantics and syntax, efficient execution, concurrency designed by the programmer.

66
Q

What are some characteristics of functional languages?

A

Simple semantics and syntax, less efficient execution, automatic concurrency.

67
Q

What control mechanisms do functional programming languages use?

A

Function application, conditional expressions, recursion, and functional forms.

68
Q

What is Haskell known for in the functional programming world?

A

Supporting lazy evaluation, infinite lists, and set comprehension.