guile-user
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: default stack size


From: Lynn Winebarger
Subject: Re: default stack size
Date: Sat, 06 Sep 2003 05:33:21 -0500
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.0.1) Gecko/20020830



Viktor Pavlenko wrote:
"bt" == barney toma <address@hidden> writes:


    bt> I receive a stack overflow error when creating a list of 500
    bt> entries.

    bt> (define (rep n)          ;(rep n) ==> (n n-1 n-2  ... 1)
    bt>     (cond ((= n 0) '())
    bt>        (t (cons n (rep (- n 1) )))))
    bt> (rep 500)

    bt> I believe the default stacksize is larger than this.  So I'm
    bt> wondering if this indicates some abnormal condition such as a
    bt> possible hardware problem.

I don't think so, unless I have a hardware problem too. A tail-
recursive procedure will give you a list as long as you wish (but in
ascending order):

(define (rep n ls)
  (cond ((= n 0) ls)
        (else (rep (- n 1) (cons n ls)))))

(rep 10000 '())

I think by setting the stack limit so low (389) they encourage you to
write tail recursive procedures (correct me if I'm wrong).


   Yeah, but why not get it in the right order:

(define (rep n)
  (let loop ((n n)
             (k (lambda (v) v)))
    (if (= n 0)
        (k '())
        (loop (- n 1)
          (lambda (v)
            (k (cons n v))))))

   Though reliance on the stack may be/is probably more efficient.  All this
version does is allocate the stack in the heap, one disconnected frame at a 
time.

Lynn






reply via email to

[Prev in Thread] Current Thread [Next in Thread]