I am learning about higher-order functions from 'Learn You a Haskell for Great Good!' by Miran Lipovaca. I know that the flip function takes a function and returns a function like the original, but with the first two arguments flipped.
I don't fully understand how the following example with the map function works.
ghci> map (flip subtract 20) [1,2,3,4]
[19,18,17,16]
Map takes a function and applies it to each element of the list, resulting in a new list. But since flip is the function map takes, with parameters subtract 20 [1,2,3,4]
, would the resulting function be 20 subtract [1,2,3,4]
?
I don't think this is correct since a value such as 19 is only produced if you type subtract 1 20
. I am not sure how subtract would work in the above example to produce the output list.
flip
is sometimes unnecessary if you could use an infix notation of a function. Such as`subtract` 20
. So you may domap (`subtract` 20) [1,2,3,4]
. Or bettermap ((-) 20) [1,2,3,4]
– Redu(flip subtract 20)
. – molbdnilo