Hi,

Sebastian Tennant <[EMAIL PROTECTED]> 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.



Reply via email to