2

Evaluate (((lambda(x y) (lambda (x) (* x y))) 5 6) 10) in Scheme.

I am not sure how to do this actually!

((lambda (x y) (+ x x y)) 3 5)

Is fairly simple. Here x=3, y=5.

But in this the body is: (lambda (x) (* x y))) 5 6) and the parameter is 10?

So we evaluate separately? As in (lambda (x) (* x y))) 5 6) = (* 5 y) And then (((lambda(x y) (lambda (x) (* x y))) 5 6) 10) = (((lambda (x y) (* 5 y) 10))

But how can that be evaluated?

1
  • Please don't completely change your question. That invalidates the answers that people have taken time to write. If you have a new question, ask a new question. Commented Nov 22, 2016 at 19:05

3 Answers 3

2

It is easier if you rename the parameter in the inner lambda:

(((lambda (x y) (lambda (z) (* z y))) 5 6) 10)

The outer lambda is applied to 5 and 6:

((lambda (x y) (lambda (z) (* z y))) 5 6)

This evaluates to

(lambda (z) (* z 6))

since yis bound to 6.
Note that x from the outer lambda (which is bound to 5) is never used.

This function is then applied

((lambda (z) (* z 6)) 10)

which yields 60.

Sign up to request clarification or add additional context in comments.

Comments

2
(((lambda (x y) (lambda (x) (* x y))) 5 6) 10)

is

(  (  (lambda (x y) 
         (lambda (x) (* x y))
         )
      5 
      6) 
   10)

is

enter image description here

See? The inner lambda is returned as a value, as result of the application of the outer lambda to arguments 5 and 6. The new value is enclosed in its defining environment, where x=5 and y=6 -- such lambda is otherwise known as closure:

enter image description here

It is then applied to 10:

(((lambda (x y) (lambda (x) (* x y))) 5 6) 10)

= ( (let ((x 5)
          (y 6))
       (lambda (x) (* x y)))
    10)

= (let ((x 5)
        (y 6))
     ( (lambda (x) (* x y))
       10))

= (let ((x 5)
        (y 6))
      (let ((x 10))
          (* x y)))

=         (* 10 6)

=         60

Comments

1

Let's start with (lambda(x y) (lambda (x) (* x y))). This is a function expression that takes two parameters and returns another function expression. For convenience, I will refer to the entire expression as f.

Now consider (f 5 6). This returns another lambda, (lambda (x) (* x y), with y bound to the value passed in. However, the inner x parameter shadows the outer one, so the result is (f 5 6) = (lambda (x) (* x 6).

From here, we can directly evaluate ((lambda (x) (* x 6)) 10). This yields a final result of 60.

1 Comment

No. The 10 is applied to the result of ((lambda (x y) ... ) 5 6). Look carefully at the parentheses (or use an editor that can show you matching parens).

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.