If I have a recursive function like this:
(define (double-n-times x n)
(if (= n 0)
x
(double-n-times (* 2 x) (- n 1))))
How can I make a lambda version of it and never give it a name? ... like if i want to inline it somewhere. Is that possible? (I mean in this case I could use fold - so maybe the example isn't that great) - Is there some kind of symbol or placeholder for "self" that I haven't been able to find? Or do you just have to give it a name.
The Y-Combinator in Racket is:
This function can take any anonymous function and apply it on themselves recursively.
Let us define your function's part.
double-n-times
-part written only with lambdas:where
f
we could name as we want - so we could also call itdouble-n-part
.If we apply the Y-Combinator on this, we get:
This spits out a function which takes the arguments
x
andn
and applies the inner function of the second definiton on them.So now, without any named functions - only using
lambda
expressions - you can apply on your arguments - let's sayx=3
andn=4
:This is more convenient to read. But we could also define the
Y combinator
withoutapply
andargs
when we allow only monadic functions (functions with one arguments) instead of variadic ones. Then it looks like this (and we have to give the arguments one after another like this):