• Re: sum over list?

    From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp,comp.lang.scheme on Tue Sep 17 13:45:13 2024
    From Newsgroup: comp.lang.scheme

    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)))
    --- Synchronet 3.22a-Linux NewsLink 1.2