William James
5/14/2015 6:59:00 AM
Steve Gonedes wrote:
> Rather than construct a new list then add the numbers you could just
> add the numbers as you recurse through the list. You just have to
> return 0 when you hit a nil (the end of the list usually). I think you
> got the harder part right (returning a zero), you just have to move
> the `+' function.
>
> (defun add-numbers (list)
> (cond ((null list) 0)
> ((atom list) list)
> (t (+ (add-numbers (first list))
> (add-numbers (rest list))))))
>
> (add-numbers '(1 ((5 6 ) 3) (2))) => 17
Gauche Scheme:
(define (add-numbers list)
(fold
(^(x acc) (+ acc (if (list? x) (add-numbers x) x)))
0
list))
(add-numbers '(1 ((5 6 ) 3) (2)))
===>
17
--
The conquering Sabaeans ... fecklessly committed the crime of miscegenation,
producing mongrels [who] ... neglected maintenance of the dyke of Ma'rib, which
finally broke sometime between 542 and 570, and was famed thereafter as a
cause, rather than a consequence, of the disastrous end of Sabaean culture.
--- R. P. Oliver