Given this line of Haskell code, my task was to evaluate it to its most simple form.
let g h k = (\x -> k (h x)) in g (+1) (\x -> x+x) 20
I have already been given the answer (and of course evaluated it myself in GHCI): 42
However, I would like get a better understanding of how the evaluation actually works here. In general, I think I know how (simple) let expressions work:
Example
a = let y = 5 in y * 5 -- a == 25
This evaluates to 25
because we bind y
to the value of 5
and a
gets assigned to the value of y*5
(the part after the in
). The binding y = 5
is only valid within the scope of the let
.
So far, the only interpretation (which at least evaluates to 42) is the following:
let g h k = (\x -> k (h x)) in g (+1) (\x -> x+x) 20
g
is(\x -> k (h x))
h
is(+1)
(the function(\x -> x+1)
)k
is(\x -> x+x)
20
is the input ofg
which yieldsk (h 20)
h 20
gives20 + 1
=21
k (h 20)
=k 21
=21 + 21
=42
But what confuses me is the use of g h k
after the let. What does that mean?
g
isg = \h k x -> k (h x)
the variables on the left are parameters as well. – Willem Van Onsemg = flip (.) = (>>>)
.g h k x = k . h $ x = x & (h >>> k) = h x & k
. – Will Ness