Let us examine two simple cases so that we can understand how we can rewrite any function that uses let with lambda:
In our first case we have one let. This function is very simple, it returns a given input by adding 10 to it:
(define (test x)
(let ((b 10))
(+ x b)))
Now let us turn this into an expression using lambda:
(define (test-lambda x)
((lambda (b)
(+ x b))
10))
As you can see, test-lambda returns a lambda evaluation that is evaluated with the value 10. Testing this we can say:
(test-lambda 10)
which will return 20.
- Now for more than one let, we nest lambda-expressions within lambda-expressions.
Our let case we have two let statements:
(define (lets x)
(let ((a 10)
(b 20))
(+ x a b)))
We can write this with lambda like so:
(define (lets-lambda x)
((lambda (a)
((lambda (b)
(+ x a b))
20))
10))
So now we are evaluating each of the lambda expressions giving them a value, and the innermost lambda expression takes care of what we want to compute using the variable names that each lambda expression has been assigned.
Hope this was clear and might help others see more clearly!