guile-devel
[Top][All Lists]
Advanced

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

Re: srfi-1 take and drop


From: Rob Browning
Subject: Re: srfi-1 take and drop
Date: Thu, 08 May 2003 11:35:10 -0500
User-agent: Gnus/5.1001 (Gnus v5.10.1) Emacs/21.3 (gnu/linux)

address@hidden (Paul Jarc) writes:

> Rob Browning <address@hidden> wrote:
>> Yep, I have a "take" I haven't committed yet that avoids both stack
>> growth and reverse! by tracking the end pair...
>
> Are you using (ice-9 q)?  It seems to be the right tool for that job.

Nope.  Didn't even know that was there.  I had just done this as a
first pass:

  (define (take lst k)
    ;; avoid a reverse! or stack growth -- easy on the cache,
    ;; hard on the eyes...
    (cond
     ((zero? k) '())
     ((negative? k) (error "negative count in call to take."))
     (else
      (let ((result (cons (car lst) '())))
        (let lp ((n (- k 1)) (rest (cdr lst)) (end-pair result))
          (if (zero? n)
              result
              (let ((new-end (cons (car rest) '())))
                (set-cdr! end-pair new-end)
                (lp (- n 1) (cdr rest) new-end))))))))

though thanks for pointing out (ice-9 q).  That might be a better idea...

-- 
Rob Browning
rlb @defaultvalue.org, @linuxdevel.com, and @debian.org
Previously @cs.utexas.edu
GPG starting 2002-11-03 = 14DD 432F AE39 534D B592  F9A0 25C8 D377 8C7E 73A4




reply via email to

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