[Scheme] Few basics...

2008-11-21 Thread Michael Käppler

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. The source list is now defined globally. How can I pass it to the 
procedure addlist without carrying it along through all instances of 
the recursion?
II. I assume also the index number (nr) doesn't has to be passed to 
addlist every recursion stage. How to achieve this?


I'd be happy about any advice!

Cheers,
Michael






___
lilypond-devel mailing list
lilypond-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/lilypond-devel


Re: [Scheme] Few basics...

2008-11-21 Thread Gilles THIBAULT

Scheme hackers, [..]


I am far from a Scheme hacker,  and i cannot certify that is good 
programmation, but the following seems to work.


;

(define (addlist i nr)
   (let ((local-list (list 0 1 2 3 4 5 6 7)))
   (define (local-addlist i)
   (cons (list-ref local-list i) (if (= (+ i 1) nr) '() 
(local-addlist (+ i 1)

(local-addlist i)
))

(display (addlist 0 6))

;;;

Is it what you wanted ?

Gilles




___
lilypond-devel mailing list
lilypond-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/lilypond-devel


Re: [Scheme] Few basics...

2008-11-21 Thread Han-Wen Nienhuys
On Fri, Nov 21, 2008 at 4:46 PM, Michael Käppler [EMAIL PROTECTED] 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 - [EMAIL PROTECTED] - http://www.xs4all.nl/~hanwen
___
lilypond-devel mailing list
lilypond-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/lilypond-devel