Previous | Next --- Slide 10 of 50
Back to Lecture Thumbnails
lexicologist

Python also supports operations like fold and map as well as lambda functions. It's nice to use these sorts of functional programming inspired patterns because it makes for clean code. It also tends to impress interviewers

https://book.pythontips.com/en/latest/map_filter.html

ufxela

It was noted in class that the reduce operation is a specific type of a fold operation.

anon33

Could someone explain the b -> ((b,a) -> b) -> seq a -> b notation? I understand that (b,a) -> b is the binary operation, but why is fold not of type

((b, a) -> b) -> ((b, seq a) -> b)

if I am understanding the notion of second order function correctly?

lonelymoon

@anon33 I am not sure about this equation. Is there any possibility that in this case, the target function g is b -> ((b,a) -> b), and this is applied to the arrow between seq a->b ?

tspint

@anon33 I was also confused by this arrow notation. In lecture, it was described that the arguments to fold are 1) a starting element of type b, 2) a binary function that takes a pair (b,a) to an element of type b, and 3) a sequence of elements of type a.

For every element in the sequence, it runs the binary function with the result of the binary function on the previous element. So conceptually for me, it does something like (b, ((b,a)->b), seq a) -> b. There is probably a reason that the commas I used are actually arrows in the definition in the slide, but I am also unsure why.

blipblop

@anon33 @tspint I think this is standard currying notation.

"Currying is when you break down a function that takes multiple arguments into a series of functions that each take only one argument." (https://stackoverflow.com/questions/36314/what-is-currying)

Parentheses are implied back to front. So you can read fold :: b -> ((b,a) -> b) -> seq a -> b equivalently as fold :: b -> ( ((b,a) -> b) -> (seq a -> b) ).

Say b_el is an element of type b, f is a function of type (b,a) -> b, and a_seq is of type 'seq a'. Function application has implied parentheses going left to right. If you do c = fold b_el then c is a function of type ((b,a) -> b) -> (seq a -> b) If you do d = fold b_el f then d is a function of type seq a -> b Doing result = fold b_el f a_seq means result has type b

Any function that takes a tuple as input can be written in curried form. For example, the function f : (b,a) -> b can be written as f : (b -> a) -> b.

Please correct me if I'm wrong or if this is confusing.

Please log in to leave a comment.