Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

f1 = flip const map. How does this function work?

Let's say we have this point-free function:

f1 = flip const map

I'm clueless about how exactly does it work and what it is supposed to do? I.e. I know what map, const and flip functions are. But putting them together like this just doesn't make sense to me. What exactly is happening inside this function? It seems to return the same thing I pass into it every time but... Why?

like image 225
Peter Bosák Avatar asked Oct 04 '13 14:10

Peter Bosák


2 Answers

Keep in mind that all functions in Haskell take only one argument but simulate taking multiple arguments by returning another function. So flip const map can also be written as (flip const) map. const normally ignores its second argument and returns its first argument. flip reverses the order of the arguments, so flip const ignores the first argument and returns the second argument. So map gets ignored and a function is returned that always returns its argument.

like image 86
Dirk Holsopple Avatar answered Oct 09 '22 09:10

Dirk Holsopple


Let's see what this function does, bit by bit

flip const map x = (flip const) map x
                 = const x map
                 = x

That's why it always returns what you give it!

like image 28
Tom Ellis Avatar answered Oct 09 '22 09:10

Tom Ellis