[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: default stack size
From: |
Joshua Judson Rosen |
Subject: |
Re: default stack size |
Date: |
Sat, 6 Sep 2003 10:09:19 -0400 |
User-agent: |
Mutt/1.5.4i |
On Sat, Sep 06, 2003 at 05:33:21AM -0500, Lynn Winebarger wrote:
>
>
> 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)
Tangentially: are you sure that you meant "t" here, and not "#t"?
t -is- bound to 't, in guile, while -is- true due to Scheme's `all
things other than #f are true' rule, but the canonical booleans in
Scheme, unlike in other most lisps, are #t and #f, not t and nil; in
fact, nil (which is bound to 'nil) is also `true', in guile/scheme....
You can also use the `else' syntax inside a Scheme cond, as Viktor has
demonstrated below:
> >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).
(I recall someone (either Marius or Mikael, I -think-) saying that the
stack-limit was `to be friendly to newbie programmers', because
newbies are likely to write infinitely-recursive routines and would
like prefer to have them fail sooner rather than later)
> 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.
... and you can also use an `if', as Lynn has demonstrated above.
Lynn, that's certainly an interesting point, but what's wrong with
just changing the direction in which the counter progresses? :)
Like:
(define (rep n)
(let loop ((m 1)
(ls '()))
(if (> m n)
ls
(loop (+ m 1) (cons m ls)))))
--
"The difference between theory and real life is that in theory,
there is no difference between theory and real life, but in real life,
there is a difference." [who originally said this?]
pgpaY7CzC1ooR.pgp
Description: PGP signature