Seems a function should have a name to be recursive(in order to call itself), so how does a lambda function be recursive?
I searched wikipedia and it says this could be done by a "Y-combinator". I don't have much math theory backgroup, it just tell me that "Y-combinator" is discovered by Haskell himself. Called "fix" keyword in Haskell language, I tried:
Prelude> let fact = fix (\f n -> if n == 0 then 1 else n * (f (n-1)))
<interactive>:17:12: Not in scope: ‘fix’
Seems failed, but how to use "fix" keyword to do what I expected?
fix
is defined as:so it expands into
f (f (f (f ...)))
i.e. an infinite sequence of nested applications of the input functionf
to some parameterx
.You can give your lambda function a top-level definition e.g.
or equivalently:
you can provide this function to
fix
to obtain a function(Int -> Int)
i.e.if you expand this definition you get
due to laziness, the repeated applications of
factF
are only evaluated ifn /= 0
so the above function applied to0
will evaluate immediately to 1.You can see in this expansion that
factF
is being provided as an argument to itself which it then applies to smaller version ofn
. SincefactN
is the same as your lambda function, the same thing is happening there - the parameterf
inside the lambda is the lambda itself which it can then call recursively.