You do not have permission to delete messages in this group
Copy link
Report message
Sign in to report message
Show original message
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
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