Deutsch   English   Français   Italiano  
<vb56ap$2vpjo$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!weretis.net!feeder8.news.weretis.net!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: 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=
Bytes: 1776

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