This is the mail archive of the guile@cygnus.com mailing list for the guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: list-split


[Sascha Ziemann]

| Hi,
| 
| is there another way to split a list than to use list-head and
| list-tail?
| 
| I would like to avoid running twice through the list to the point where
| the list should be splitted.

Something like this (done destructively)?  (Someone more skilled in
Scheme than me would probably do this in a much smarter way -- perhaps
iteration is a better solution than recursion in this case?)

;; Split list after (or is it "at"?) cons #N, counting from zero.
(define (split-list-after! n list)
  (if (null? list)
      '()
      (if (= 0 n)
	  (let ((tail (cdr list)))
	    (set-cdr! list '())
	       tail)
	  (split-list-after! (- n 1) (cdr list)))))

-- 
Harald