MastRofDsastR
MastRofDsastR

Reputation: 161

Explanation of complex evaluation for this Scheme procedure using lambdas

I was wondering how the following code evaluates to 15.

((lambda(x y) (+ (x * y) (x + y))) (lambda (a b) (a b b)) 3)

I have been looking at it for a while and cannot seem to understand why the evaluation does not result in an error. Could someone provide a detailed step by step instruction on how this evaluates to 15?

Upvotes: 1

Views: 80

Answers (1)

assefamaru
assefamaru

Reputation: 2789

Perhaps it is easier to see what is happening if you named the lambdas. For example,

(define (f x y)
  (+ (x * y) (x + y)))

(define (g a b)
  (a b b))

Then the expression ((lambda (x y) (+ (x * y) (x + y))) (lambda (a b) (a b b)) 3) becomes:

(f g 3)

and it evaluates as:

(f g 3)
=> (+ (g * 3) (g + 3))
=> (+ (* 3 3) (+ 3 3))
=> (+ 9 6)
=> 15

Upvotes: 4

Related Questions