0

Can anyone explain to me how this expression in Scheme would return 100?

   (((lambda (f) ((lambda (g) (lambda (h) (f (g (h 4))))) double)) square) inc)

I know it somehow breaks down to "(2*(4+1))^2", but for the life of me, I can't figure out how.

This is for an exam with similar questions. There will be about 6 or 7 of these, and we have to find their answers in about 1 or 2 minutes (as they are only 1 section of the test). Our professor hasn't offered any help except study harder, but I am at a total loss on how to do these, let alone quickly.

Any help would be greatly appreciated! Thanks.

2 Answers 2

5

Let's go step-by-step - and notice how a good indentation makes everything easier to understand!

(((lambda (f)      ; call outermost lambda, f is bound to square
    ((lambda (g)   ; call mid lambda, g is bound to double
       (lambda (h) ; return lambda, h is unbound
         (f (g (h 4)))))
     double))
  square)
 inc)

The above expression has been reduced to this:

((lambda (h)
  (square (double (h 4))))
 inc)

Now we finally call the last lambda, and h is bound to inc, resulting in the following expression:

(square (double (inc 4)))
=> 100
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the explanation. With yours and Sylwester's explanation, I was able to start knocking these out!
1

Looking at the expression you see right away that most of this is the operator.. The calls have the form:

(operator operands ...)

Thus you know the operator is this:

((lambda (f)
    ((lambda (g)
       (lambda (h)
         (f (g (h 4)))))
     double))
  square)

and the sole operand is this:

inc

When looking at the operator you see again open parentheses and that means it also is a call. The new operator is binds f to square and does a new call binding g to double and since the last lambda does not have parentheses around it the return is a function/closure/procedure as expected. So we can replace the bindings without changing it's meaning and in this case you get:

(lambda (h)
  (square (double (h 4))))

Now lets do the top (operator operand) again:

((lambda (h)
   (square (double (h 4))))
 inc)

Here you see h gets bound to inc. If we substitute that we can replace the whole thing with the procedure body:

(square (double (inc 4))) ; ==> 100

When you see ((lambda ...) ...) you are looking at a anonymous procedure getting called right away. It's the same as (something ...) which also is a call but here the variable something shoudl get evaluated to a procedure. An extra parentheses around either of these like (((lambda .. or ((something ... should tell you that the function is expected to return a function that is then called. It's standard correct Scheme but not the most common so just learn to be aware of these since it requires some more seconds of attention. When writing code yourself considerer making this clear with bindings:

(let ((helper (something a b c))))
  (helper d))

Rather than:

((something a b c) d)

Even though these look equally simple at time of writing even you will thank yourself when you look again after a long pause.

1 Comment

Thanks for breaking it down like this. It really helped me understand how these were working. Just went back to the few practice problems I had and were able to knock them out quickly!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.