Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

But, for my own part, it was Greek to me. -- William Shakespeare, "Julius Caesar"


comp / comp.lang.lisp / Re: Stack overflow problem

SubjectAuthor
o Re: Stack overflow problemB. Pym

1
Subject: Re: Stack overflow problem
From: B. Pym
Newsgroups: comp.lang.lisp, comp.lang.scheme
Organization: A noiseless patient Spider
Date: Mon, 2 Sep 2024 20:14 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,comp.lang.scheme
Subject: Re: Stack overflow problem
Date: Mon, 2 Sep 2024 20:14:19 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 36
Message-ID: <vb56ap$2vpjo$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Mon, 02 Sep 2024 22:14:19 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="a435632fcbba468531e98d5e4c12abf8";
logging-data="3139192"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+GJrc94T3RFIWpnpbxiQrt"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:GVkK8uslVgf3WJKv1g49ngKUhKQ=
View all headers

> > (defun nums (n) (loop for i from 1 to n collect i))
> >
> > (defmethod sum ((x null)) 0)
> > (defmethod sum ((x list)) (+ (first x) (sum (rest x)))
> >
> > (sum (nums 100)) => Stack overflow.
> >
> > I was hoping someone could toss some insight my way as to why this is.
>
> The recursion is simply too deep.
>
> If you're blowing out at 100 deep, you are probably running
> the code interpreted, which would add a bunch of additional
> stack frames. If you compile the methods, you'll probably
> get an order of magnitude farther.
>
> But you could just write instead:
>
> (reduce #'+ (nums 100))
> or
> (loop for i from i to n summing i)
>
> and not have to worry about the stack.

Gauche Scheme:

(define-method object-apply ((lst <list>)) (fold + 0 lst))

gosh> '(1 3 5 7 9)
(1 3 5 7 9)

gosh> ('(1 3 5 7 9))
25

gosh> ((iota 333 1 2))
110889

1

rocksolid light 0.9.8
clearnet tor