Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how is this haskell expression evaluated

Tags:

haskell

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.

like image 703
Programm-ist Avatar asked Apr 15 '15 19:04

Programm-ist


1 Answers

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.)

like image 127
Lynn Avatar answered Oct 01 '22 00:10

Lynn