Re: Stack overflow problem

Liste des GroupesRevenir à cl lisp 
Sujet : Re: Stack overflow problem
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lisp comp.lang.scheme
Date : 02. Sep 2024, 22:14:19
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vb56ap$2vpjo$1@dont-email.me>
User-Agent : XanaNews/1.18.1.6
(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

Date Sujet#  Auteur
2 Sep 24 o Re: Stack overflow problem1B. Pym

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal