I am learning haskell
and I came across this expression which I could not understand.
(flip const 1 . const flip 3 const 4) 5
The final result is 5 but I have no idea how it is evaluated.
By definition of (.)
:
flip const 1 $ ((const flip 3) const 4) 5
By definition of const
:
= flip const 1 $ flip const 4 5
By definition of flip
:
= flip const 1 $ const 5 4
By definition of const
:
= flip const 1 5
By definition of flip
:
= const 5 1
Which is 5
.
(As a little bonus insight, can you find out why flip const y
is just id
for all y
? This reduces your expression to (id . id) 5
.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With