Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Stack overflow problem

19 views
Skip to first unread message

WJ

unread,
Feb 25, 2015, 2:30:08 PM2/25/15
to
> > (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:

(use srfi-42)
(sum-ec (:range i 1 5999888) i)
===>
17999325006328
0 new messages