Reputation: 309
I'm new to scheme. When I run the following code
(define lst '(1))
(let ((func1 (lambda lst
(begin (display lst)
lst))))
(begin (display lst)
(func1 lst)))
I got (1)((1))'((1))
, which means lst displays as (1)
when called in the fourth line, but when send it to the function func1
, it becomes ((1))
. What exactly happened here?
Upvotes: 1
Views: 64
Reputation: 236004
In a lambda
form, when you write this:
(lambda x <body>)
... you're declaring that x
is a list of parameters with zero or more elements. On the other hand, this:
(lambda (x) <body>)
... is stating that x
is a single parameter. In the question, this code (the begin
is unnecessary for the body part in a lambda
):
((lambda lst (display lst) lst) '(1))
... will display and return the list of parameters; if we pass '(1)
it will evaluate to '((1))
: a list with a single element which happens to be a list.
Surely you intended to do this instead:
((lambda (lst) (display lst) lst) '(1))
... which will display and return the single parameter it receives - in case of passing '(1)
the above expression will evaluate to '(1)
, the parameter itself.
Upvotes: 2
Reputation: 363607
(lambda Args E)
means: bind the variable-length argument list of this function to Args
. E.g.
(define f (lambda args `(got ,(length args) arguments)))
(display (f 'foo 'bar 'baz))
will print (got 3 arguments)
. If you change the lambda
expression to
(lambda (lst) (begin (display lst) lst))
;;; ^---^
then the function will print, and return, its single argument.
Upvotes: 3