3

I'm totally new to scheme, coming from a c# background.

I thought I was getting a handle on lambda, but I don't understand why this example works:

(let ([f (lambda x x)]) (f 1 2 3 4) )

The answer is (1 2 3 4), but how? It's the x x part that I don't get. The lambda takes no parameters so the body is x x, right? But I don't know what that means in this case, can someone explain? Thanks.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
BarnumBailey
  • 391
  • 1
  • 4
  • 13

2 Answers2

5

In fact, the lambda in your code is receiving parameters, and an aribtrary number of them. The syntax for a lambda with no parameters is different:

(let ([f (lambda () 1)]) (f))
=> 1

The lambda expression in the question is something else:

(lambda x x)

It receives a variable number of arguments as a list, and then returns them. You named it f, and when it's invoked like this:

(f 1 2 3 4) 

All of the arguments get bound to x, a list and then the value of x is returned:

'(1 2 3 4)

So, f is nothing more than an identity function that receives multiple arguments. Perhaps this answer will clarify how variadic functions are defined in Scheme. For completeness' sake, here's an example of a lambda that receives a single argument and returns it:

(let ([f (lambda (x) x)]) (f 1))
=> 1

So there you have it, that's how we define lambdas that receive 0, 1 or many parameters.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • Thank you so much for the explanation, and for the link as well. I am reading the book _The Scheme Programming Language_ by R. Kent Dybvig and I am not quite grasping all of its the explanations when it comes to things like lists. And concepts like pairs, cells, and others. If you could recommend another book it would be appreciated. – BarnumBailey Dec 09 '18 at 21:17
  • Of course! The Little Schemer, How to Design Programs and SICP are the best books for learning how to think in Scheme. – Óscar López Dec 09 '18 at 22:05
  • 1
    When I returned to the office yesterday, I found The Little Schemer at my desk, courtesy of a coworker. I like it already. I will check out the other title, many thanks. :) – BarnumBailey Dec 11 '18 at 11:16
  • How cool! For me the best book is SICP, it's a classic. A bit more advanced, but it'll teach you not only the language, but how to build an interpreter and a compiler for it (!). And it's a first-year book for some universities. – Óscar López Dec 11 '18 at 11:18
  • @THedge [Tourezky's CL: a Gentle Intro to Symbolic Computation](https://www.cs.cmu.edu/~dst/LispBook) spends a lot of time on list structure, i.e. pairs and such. *a lot*. with *diagrams*. highly recommended for a beginner. If you want concepts *explained* to you, stay clear from the Little books. – Will Ness Dec 17 '18 at 09:50
  • @will-ness thank you, I will download the original version. I definitely want to understand concepts. – BarnumBailey Dec 17 '18 at 22:53
2

A lambda syntax is like this:

(lambda argument-list body ...)

Looking at your lambda:

(lambda x x)

Thus the argument list is x and the body of the lambda is x.

A list in Scheme looks like (a b c), but it is really (a . (b . (c . ()))) and in a procedure argument list if the chain ends up in a symbol, like (a b . c) then the procedure takes 2 arguments or more. The "rest" arguments are accumulated in a list called c. In ES6 it's the same as (a, b, ...c) => expression. Now if you have no mandatory argument, eg. you allow 0 or more arguments, the argument list is just a symbol. eg (lambda x x) is the same as (...x) => x.

I believe in C# instead of x one would have written params type[] x. C# doesn't do anonymous lambda + rest argument so I hope you know some ES6.

Sylwester
  • 47,942
  • 4
  • 47
  • 79
  • Thank you also for the explanation, it all helps.Yes, in c# _params_ allows you to specify a variable number of parameters, it appears at the end of a procedure's parameter list. – BarnumBailey Dec 09 '18 at 21:22
  • Meant to say a method's parameter list in my previous answer. And thanks for the info about the dot notation. The dot notation is an example of one of the things that, in my opinion, is not well explained in the book that I mentioned I am reading. (In my answer to Oscar.) – BarnumBailey Dec 09 '18 at 21:31
  • Since you know C# you should try to implement a Scheme subset interpreter in C# after you know how to do it in Scheme. With Scheme in Scheme lots of stuff are easy since you use the host language, but in C# you learn what the primitives and system needs to do to make it work. I'm a big fan of the [SICP videos](https://www.youtube.com/watch?v=2Op3QLzMgSY&index=1&list=PL8FE88AA54363BC46) presented by the magicians themselves. – Sylwester Dec 10 '18 at 01:47
  • I will check out the videos you referenced, thank you! – BarnumBailey Dec 11 '18 at 11:17