Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

A is for Apple. -- Hester Pryne


comp / comp.lang.lisp / Re: Simple recursive functions in Lisp

SubjectAuthor
* Re: Simple recursive functions in LispB. Pym
`- Re: Simple recursive functions in LispB. Pym

1
Subject: Re: Simple recursive functions in Lisp
From: B. Pym
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Sun, 14 Jul 2024 19:46 UTC
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Nobody447095@here-nor-there.org (B. Pym)
Newsgroups: comp.lang.lisp
Subject: Re: Simple recursive functions in Lisp
Date: Sun, 14 Jul 2024 19:46:50 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 12
Message-ID: <v719v7$9kar$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Sun, 14 Jul 2024 21:46:50 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="78677f07eb040e8118f6c654ec373cab";
logging-data="315739"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/a4zVOlDcb5HCdlaVnKGmU"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:+anjwK6jAsQe1hP7uHDm5OUPJwI=
View all headers

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

Subject: Re: Simple recursive functions in Lisp
From: B. Pym
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Sun, 14 Jul 2024 19:56 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Nobody447095@here-nor-there.org (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: <v71ahj$9ncm$1@dont-email.me>
References: <v719v7$9kar$1@dont-email.me>
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=
View all headers

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)))

1

rocksolid light 0.9.8
clearnet tor