4.12.2.1 Functional language programs Flashcards

1
Q

What is a higher order function?

A

A function is higher-order if it takes a function as an argument or returns a function as a result, or does both.

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

What is the map function?

A

A higher-order function that applies a given function to each element of a list,returning a list of results.

items = [1,2,3,4,5]
squared = list(map(lambda x: x**2, items))
# returns [1, 4, 9, 16, 25]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the filter function?

A

A higher-order function that processes a data structure, typically a list, in some order to produce a new data structure containing exactly those elements of the original data structure that match a given condition.

number_list = [-3,-2,-1,0,1,2,3]
print (list(filter(lambda x: x < 0, number_list)))
# returns [-3,-2,-1]
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is the reduce / fold function?

A

A higher-order function which reduces a list of values to a single value by repeatedly applying a combining function to the list values.

product = functools.reduce((lambda x, y: x * y), [1,2,3,4])
# returns 1 * 2 * 3 * 4 = 24
How well did you know this?
1
Not at all
2
3
4
5
Perfectly