After learning a bit of Scheme from SICP, I started reading The Little Schemer (which I find quite entertaining) and am about one fourth done. I noticed that I can write many (most? all?) solutions without using lambda whereas The Little Schemer always uses them. For example, the very first definition is
(define atom?
(lambda (x)
(and (not (pair? x)) (not (null? x)))))
which, unless I am mistaken, can be written more simply as
(define (atom? x)
(and (not (pair? x)) (not (null? x))))
Am I missing something fundamental if I write lambda-less solutions?
I strongly prefer the lambda
-heavy style for teaching, since it makes function creation more explicit, as Jay says.
When learning, the simple functions you start with like atom?
are define
d at the top level. This means it's possible, and even more compact, to create the function with the defun
-style define
you mention.
However, when you start using functions as first-class values, e.g., as an argument to map
, you'll be seeing lambda
for the first time, and it might seem weirder and more magical than it really is.
Instead, if you've been defining your functions with lambda
the whole time, it's less of a leap to see that functions are just like any other value. They happen to be on the right-hand side of define
pretty frequently, but are no different from a number or a quoted constant:
(define x 1)
(define l '(2 3 4 5))
(define s (cons x ls))
(define f (lambda (n) (+ n 2)))
Of course, the language supports both forms, so it comes down to style eventually. To me, there is an appealing consistency in the usage of define
when all of your functions are made with lambda
: the first argument is always a symbol, and the second argument is just any old expression. And the fact that lambda
is just like any old expression is one of the most important things for any functional programmer to learn.
Originally, define
had a single syntax, to set a variable to a value. That's the style used in such old (and timeless) books. Later on, define
got a different syntax as a shortcut, which is the one you're using.
Just for fun, search on your Scheme libraries, you might find a macro that expands the non-lambda form into the old lambda-heavy one.
You can see what your Scheme expands these shortcuts (macros) into using expand
(if supported):
mzscheme 4.2.4 (with DrScheme):
> (expand '(define (add1 x) (+ 1 x)))
#<syntax (define-values (add1) (lambda...>
(define-values
(add1)
(lambda (x) (apply + '1 x)))
Chez Scheme 8.0:
> (expand '(define (add1 x) (+ 1 x)))
(begin
(set! add1
(lambda (x)
(+ 1 x)))
(void))
The lambda
appears plain as day.
I vaguely remember a professor discussing something like this.
I think the lambda solution is used for two reasons:
The first is purely a historical thing. At one point in time, that was the only way it was possible. So some people still use that method.
The second is that some people just like to be more explicit about the fact that a function is being created, so they like to see the word lambda.
So I believe the choice comes down to what ever you personally like the best.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With