Sujet : Re: sum over list?
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lisp comp.lang.schemeDate : 17. Sep 2024, 15:45:13
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vcc158$3hk11$1@dont-email.me>
User-Agent : XanaNews/1.18.1.6
Frode Vatvedt Fjeld wrote:
(sum '(2 3 4)) => 9
(sum '(5 6)) => 11
The usual way of doing this would be
(reduce #'+ '(2 3 4))
which is considered to be preferable to using APPLY. LOOP can do the
job almost as well:
(loop for x in '(2 3 4) sum x)
The thing about LOOP is that it's much more flexible, in case you need
some slight variation of the theme, like for example:
(loop for x in '(2 3 a 4) when (numberp x) sum x)
Gauche Scheme
(use gauche.lazy) ;; lfilter [lazy filtering]
(fold + 0 (lfilter number? '(2 3 a 4)))