• Re: sum over list?

    From B. Pym@21:1/5 to Frode Vatvedt Fjeld on Tue Sep 17 13:45:13 2024
    XPost: 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)))

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)