lonewolf
lonewolf

Reputation: 69

Scheme nested lambda function

I am a beginner in Scheme. I found this question in MIT exam 1 for SICP lecture.

What's the value and type for –

((lambda (a) (lambda (b) (+ (sqrt a) (sqrt b)))) 5)

I am having a hard time understanding how this function works. I am really confused about the parameter b. Only 5 is passed as a parameter to the outer lambda function, then what value does b take for the inner lambda function?

I tried running this function in mit-scheme but the resulting value gets incremented each time it's run.

Upvotes: 1

Views: 450

Answers (1)

Jeff Ames
Jeff Ames

Reputation: 1446

You're correct that only the outer lambda form is applied to the argument 5. Then it returns its body with a replaced with 5, so it would return

(lambda (b) (+ (sqrt 5) (sqrt b)))

which is itself a function. This could later be applied to another argument, to produce an actual numeric value.

Upvotes: 2

Related Questions