Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: "B. Pym" Newsgroups: comp.lang.lisp Subject: Re: Simple recursive functions in Lisp Date: Sun, 14 Jul 2024 19:56:39 -0000 (UTC) Organization: A noiseless patient Spider Lines: 33 Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Injection-Date: Sun, 14 Jul 2024 21:56:39 +0200 (CEST) Injection-Info: dont-email.me; posting-host="78677f07eb040e8118f6c654ec373cab"; logging-data="318870"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1985gwzH1lsnx4l27YhROJO" User-Agent: XanaNews/1.18.1.6 Cancel-Lock: sha1:KOxH3q6blaBK05EW02wxjaTxI/w= Bytes: 1523 B. Pym wrote: > Pascal Bourguignon wrote: > > > (defun sum (list) > > (loop :for x :in list :sum x)) > > Gauche Scheme > > (define (sum lst) (fold + 0 lst)) > > (sum '(1 3 5 7 9)) > ===> > 25 Pascal Bourguignon wrote: > >> Of course, since LOOP is a higher level abstraction. You should > >> compare with this iterative form: > >> > >> (defun sum (list) > >> (do ((current list (cdr current)) > >> (sum 0)) > >> ((null current) sum) > >> (setf sum (+ sum (car current))))) Better: (define (sum lst) (do ((xs lst (cdr xs)) (res 0 (+ res (car xs)))) ((null? xs) res)))