Recursive Function
factorial = function(n){ if(n == 0){ return 1 } else{ n * factorial(n - 1) } }
Pseudo Code
Recursive Functionlet’s simplify this to an ‘infinite’ function.
fact-forever = function(n){ n * fact-forever(n - 1) }
Recursive FunctionWhat if we can’t use a variable?
fact-forever = function(n){ n * fact-forever(n - 1) }
Recursive Function
What is this inside ‘fact-forever’ gonna be?
fact-forever = function(n){ n * fact-forever(n - 1) }
function(n){!
n * (n - 1)!
}
Recursive FunctionI’m imagining if we have a ‘magical function’, that prepares itself again when we need it…
Thought Experiment #1Is it possible to think of a lambda, which
returns a lambda, that takes ‘itself’ as an argument?
((lambda(me)(me me)) (lambda(x)(quote hi)))
Thought Experiment #1Is it possible to think of a lambda, which
returns a lambda, that takes ‘itself’ as an argument?
((lambda(me)(me me)) (lambda(x)(quote hi)))
=> ‘hi
((lambda(me)(me me)) (lambda(x)( )))
Thought Experiment #2Since ‘me’ function takes ‘me’ as an argument, isn’t it fun if we can somehow use it inside it?
quote hi
((lambda(me)(me me)) (lambda(x)( )))
Thought Experiment #2Since ‘me’ function takes ‘me’ as an argument, isn’t it fun if we can somehow use it inside it?
x
((lambda(me)(me me)) (lambda(x)( )))
Thought Experiment #2Since ‘me’ function takes ‘me’ as an argument, isn’t it fun if we can somehow use it inside it?
=> expects 1 argument, but found none.
x
((lambda(me)(me me)) (lambda(x)( )))
Thought Experiment #3Yeah my bad, x is a lambda expecting
one argument. so… pass in x itself maybe?
x
((lambda(me)(me me)) (lambda(x)( )))
Thought Experiment #3Yeah my bad, x is a lambda expecting
one argument. so… pass in x itself maybe?
xx
=>
((lambda(me)(me me)) (lambda(x)( )))
Thought Experiment #3Yeah my bad, x is a lambda expecting
one argument. so… pass in x itself maybe?
xx
((lambda(x)(x x)) (lambda(x)(x x)))=>
((lambda(me)(me me)) (lambda(x)( )))
Thought Experiment #3Yeah my bad, x is a lambda expecting
one argument. so… pass in x itself maybe?
=> infinite loop!
xx
((lambda(x)(x x)) (lambda(x)(x x)))=>
Thought Experiment #3
This is the Javascript version of same thing.
function(x){ return x(x)}(function(x){return x(x)})
((lambda(me)(me me)) (lambda(x)( )))
Resume the ProblemSo, how can we use this piece
with the original problem?
xx
function(n) n * (n - 1)
((lambda(x) x )(quote hi))
Functional RefactoringWrap Function
((lambda(y)((lambda(x)x)y))(quote hi))
if you have a lambda that takes one argument, you can wrap it with another lambda which also takes one argument, and call it with that argument, you still get the same result.
((lambda(me)( me me )) (lambda(x)
Combine themSo, here I use a twisted version of Wrap Function,
in order to accumulate the calculation.
(lambda(number)(number * (
( x x )))
(number - 1))))
((lambda(me)( me me )) (lambda(x)
Combine themSo, here I use a twisted version of Wrap Function,
in order to accumulate the calculation.
(lambda(number)(number * (
( x x )
)) (number - 1))))
((lambda(me)( me me )) (lambda(x)
Combine themSo, here I use a twisted version of Wrap Function,
in order to accumulate the calculation.
(lambda(number)(number * (( x x )
)) (number - 1))))
((lambda(me)( me me )) (lambda(x)
Combine themSo, here I use a twisted version of Wrap Function,
in order to accumulate the calculation.
(lambda(number)(number * (( x x )
)) (number - 1))))
Ω Combinator
((lambda(me)( me me )) (lambda(x)
The code below is an application of Ω Combinator.
(lambda(number)(number * (( x x )
)) (number - 1))))
Demo Available