In SICP 1.2.1 there is a function that makes a rational number, as follow:
(define (make-rat n d)
(let ((g (gcd n d)))
(cons (/ n g) (/ d g))))
I'm just curious how you can implement the same thing using lambda instead of let, without calling GCD twice. I couldn't figure it out myself.
Looking at SICP section 1.3.2,
is equivalent to
So your procedure,
should be equivalent to
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:
Now let us turn this into an expression using lambda:
As you can see, test-lambda returns a lambda evaluation that is evaluated with the value 10. Testing this we can say:
which will return 20.
Our let case we have two let statements:
We can write this with lambda like so:
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!
These two things are same:
and