[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Parameters
From: |
Ludovic Courtès |
Subject: |
Re: Parameters |
Date: |
Mon, 04 Feb 2008 16:47:28 +0100 |
User-agent: |
Gnus/5.11 (Gnus v5.11) Emacs/22.1 (gnu/linux) |
Hi,
Sebastian Tennant <address@hidden> writes:
> Understood... up to a point. However, I don't understand why calling
> the first procedure like this:
>
> (add '(1 2 3 4))
>
> and calling the second procedure like this:
>
> (add 1 2 3 4)
>
> doesn't result in 'l' having the same value; '(1 2 3 4), in each case?
L should have the same value in both cases, i.e.,
((lambda l l) 1 2 3 4)
=> (1 2 3 4)
((lambda (l) l) '(1 2 3 4))
=> (1 2 3 4)
> Why does the second procedure fail regardless of how it is called?
Ah, I hadn't noticed it: your second procedure should read
"(apply add (cdr l))" instead of "(add (cdr l))".
>> (define add
>> (lambda (l)
>> (let loop ((l l)
>> (result 0))
>> (if (null? l)
>> result
>> (loop (cdr l) (+ result (car l)))))))
>
> Noted.
Or, more elegantly:
(use-modules (srfi srfi-1))
(define add
(lambda (l)
(fold + 0 l)))
Cheers,
Ludovic.