yrazlik
yrazlik

Reputation: 10777

How to decide the parantheses in scheme

I am a little bit confused about putting the parantheses in scheme. The following example:

=>(define foo1 (lambda (n) (+ n 1)))
=>(foo1 ((lambda () 5)))

=>value:6

Gives the result 6. But i am surprised why this did not give an error. Here is how i thnik it should be computer: First the (lambda () 5) expression is computed and it returns 5. Now we an remove its parantheses:

=>(foo (5))

And now this should be invalid since we do not use parantheses for the parameters while calling a function. But it gives no error. Can somebody tell me what i am doing wrong?

Thanks

Upvotes: 1

Views: 92

Answers (2)

WorBlux
WorBlux

Reputation: 1413

Scheme makes no fundamental distinction between code and data. Why do I mention this? It's because Lambda's return is always either a function or an error, that is it returns code rather than simple data. Any other function that returns a function is going to behave in the same way.

Upvotes: 1

Arxo Clay
Arxo Clay

Reputation: 876

Alright! Finally figured out what's going on here.

This statement actually evaluates to 5:

((lambda () 5))

Try it in your REPL.

The inner part (lambda () 5) creates a procedure. The outer paranthesis pair '(', ')' invoke the function. Obviously that makes it return 5!

Upvotes: 3

Related Questions