[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Scheme] Few basics...
From: |
Han-Wen Nienhuys |
Subject: |
Re: [Scheme] Few basics... |
Date: |
Fri, 21 Nov 2008 22:08:31 -0200 |
On Fri, Nov 21, 2008 at 4:46 PM, Michael Käppler <address@hidden> wrote:
> Scheme hackers,
> following simple code extract:
>
> (define testlist (list 0 1 2 3 4 5 6 7))
>
> (define (addlist i nr)
> (cons (list-ref testlist i) (if (= (+ i 1) nr) '() (addlist (+ i 1) nr)))
> )
>
> (display (addlist 0 3))
>
> The procedure addlist returns all list elements up to the element >before<
> that whose index is given through the second argument.
I think you should look into the srfi-1 library. Also,
guile> (help list-head)
`list-head' is a primitive procedure in the (guile) module.
c snarfed from list.c:491
@deffn {Scheme Procedure} list-head lst k
Copy the first @var{k} elements from @var{lst} into a new list, and
return it.
@end deffn
Iwould probably write something like
(define (take lst n)
(define (helper lst acc n)
(if (or (=? 0 n) (null? lst))
acc (helper (cdr lst) (cons lst acc) (1- n))))
(reverse (helper lst '() n)))
--
Han-Wen Nienhuys - address@hidden - http://www.xs4all.nl/~hanwen