I'm new to Haskell and even though I've done the basic, I still don't understand why the following result gives 4:
d f x = f x x
main = print ((d (.)) (d ( )) 1)
CodePudding user response:
d (.)
will result in a function \x -> (.) x x
which takes as input x
a function and will thus take a variable y
and applies x
two times, so \x y -> x (x y)
.
The d ( )
expression. d ( )
is again a function that looks like \z -> ( ) z z
, or less verbose \z -> z z
, this is thus a function that takes as input a number z
, and will return z z
.
We use this function \z -> z z
as variable x
in the \x y -> x (x y)
expression, so d (.) (d ( ))
is equivalent to \y -> (\z -> z z) ((\z -> z z) y)
.
Finally we use that function with 1
as parameter, the result is thus (\z -> z z) ((\z -> z z) 1)
which is equivalent to:
(\z -> z z) ((\z -> z z) 1)
→ (\z -> z z) (1 1)
→ (\z -> z z) 2
→ (2 2)
→ 4