From: Han-Wen Nienhuys Date: Sun, 26 Nov 2006 14:59:20 +0000 (+0100) Subject: add split-list , split-list-by-separator , list-element-index X-Git-Tag: release/2.10.1-1~25 X-Git-Url: https://git.donarmstrong.com/?a=commitdiff_plain;h=a77e83a973b89c0cc2b99e1aea8965a3464d29ec;p=lilypond.git add split-list , split-list-by-separator , list-element-index --- diff --git a/scm/lily-library.scm b/scm/lily-library.scm index 7bdf96d05d..741a74f73d 100644 --- a/scm/lily-library.scm +++ b/scm/lily-library.scm @@ -230,7 +230,31 @@ found." ;;;;;;;;;;;;;;;; ;; list - +(define (split-list lst n) + "Split LST in N equal sized parts" + + (define (helper todo acc-vector k) + (if (null? todo) + acc-vector + (begin + (if (< k 0) + (set! k (+ n k))) + + (vector-set! acc-vector k (cons (car todo) (vector-ref acc-vector k))) + (helper (cdr todo) acc-vector (1- k))))) + + (helper lst (make-vector n '()) (1- n))) + +(define (list-element-index lst x) + (define (helper todo k) + (cond + ((null? todo) #f) + ((equal? (car todo) x) k) + (else + (helper (cdr todo) (1+ k))))) + + (helper lst 0)) + (define-public (list-join lst intermediate) "put INTERMEDIATE between all elts of LST." @@ -301,8 +325,8 @@ found." (set-car! c (reverse! (car c))) c)) -(define-public (split-list lst sep?) - "(display (split-list '(a b c / d e f / g) (lambda (x) (equal? x '/)))) +(define-public (split-list-by-separator lst sep?) + "(display (split-list-by-separator '(a b c / d e f / g) (lambda (x) (equal? x '/)))) => ((a b c) (d e f) (g)) " @@ -318,7 +342,7 @@ found." (if (null? lst) '() (let* ((c (split-one sep? lst '()))) - (cons (reverse! (car c) '()) (split-list (cdr c) sep?))))) + (cons (reverse! (car c) '()) (split-list-by-separator (cdr c) sep?))))) (define-public (offset-add a b) (cons (+ (car a) (car b))