Reputation: 519
I get this
(define (ident x) x)
is the same as
(define ident (lambda (x) x))
But why use lambda when you can simply use the former here? Doesn't it seem a bit more simple?
Upvotes: 1
Views: 136
Reputation: 48775
Here's an example using the short form and lambda in the same procedure:
(define (make-generator from step-proc) ; short form
(lambda () ; the lambda here is the procedure returned
(let ((tmp from))
(set! from (step-proc from))
tmp)))
;; can't use short form here since make-generator
;; returns the procedure
(define natural
(make-generator 1
;; use an anonymous procedure
;; to increase the previous number by 1
(lambda (x) (+ 1 x))))
(natural) ; ==> 1
(natural) ; ==> 2
(natural) ; ==> 3
;; can't use short form here since make-generator
;; returns the procedure
(define rational
(make-generator 1
;; use an anonymous procedure
;; to halve the previous number
(lambda (x) (/ x 2))))
(rational) ; ==> 1
(rational) ; ==> 1/2
(rational) ; ==> 1/4
Now, Scheme didn't originally have the short form (not until R3RS in 1986, 11 years after the first specification), but now that it's been around for several revisions it's safe to use it always as long as one understands it is the same. Some books first editions predates R3RS and some haven't updated to use it since it may be confusing having more than one way to define procedures. One of these books is the excellent Little Schemer.
Upvotes: 2
Reputation: 16260
Yes, normally you'd use the shorthand when defining a function and giving it a name.
Sometimes it's handy to define a function to be used immediately, and it doesn't need a name. Often people call these "anonymous functions". When you don't need it to have a name, it's nice not to be forced to provide one.
For example let's say you want to add 2 to every item in a list. You can use map
for this, supply it a function that adds 2. You could write:
(define (add-2 x)
(+ x 2))
(map add-2
(list 1 2 3))
However if you only needed the add-2
stuff this once, instead you could simply write this:
(map (lambda (x) (+ x 2))
(list 1 2 3))
By the way you could also use curry
to write this as:
(map (curry + 2)
(list 1 2 3))
curry
is a function that creates a function. In languages like Scheme and Racket, using such "higher-order functions" can be a way to express things concisely and leverage patterns.
In Scheme and Racket, a variable can have a value like a number or a string -- and it can also be a function. Functions are "first-class" values, just like numbers and strings. The first form you asked about makes that clear:
(define x 1)
(define y "foo")
(define f (lambda (x) (+ x 2)) ;a function is a value like any other
It's good to learn and remember that, even if you'll normally use the shorthand form to define a named function.
Upvotes: 2
Reputation: 223183
Well, if your example is the only use of a lambda
, then obviously it's redundant. But there are many other applications of lambda
outside of define
, such as this:
(map (lambda (x y)
(sqrt (+ (* x x) (* y y))))
'(3 6 9) '(4 8 12))
Upvotes: 2
Reputation: 3718
Likely (I'd imagine) you are doing this for a class. Probably they want you to use lambda so you are more familiar with how lambda works. Both the functions do the same thing. The former is simpler. But, the latter is also simple and sometimes simple examples are demonstrative.
Also, you will likely be using the map
function later. That is often expressed with lambda functions, although even in that case either example works.
Upvotes: 1