Deutsch English Français Italiano |
<vcc158$3hk11$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: "B. Pym" <Nobody447095@here-nor-there.org> Newsgroups: comp.lang.lisp,comp.lang.scheme Subject: Re: sum over list? Date: Tue, 17 Sep 2024 13:45:13 -0000 (UTC) Organization: A noiseless patient Spider Lines: 25 Message-ID: <vcc158$3hk11$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Injection-Date: Tue, 17 Sep 2024 15:45:13 +0200 (CEST) Injection-Info: dont-email.me; posting-host="a46abdd1c4c6debf9f002d978f341ceb"; logging-data="3723297"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18662EqziM2o1jGLwBZyvpV" User-Agent: XanaNews/1.18.1.6 Cancel-Lock: sha1:nhcs8l7e/pfvg1hR7E5b8ZiEvbw= Bytes: 1428 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)))