]> git.donarmstrong.com Git - lilypond.git/commitdiff
add split-list , split-list-by-separator , list-element-index
authorHan-Wen Nienhuys <hanwen@xs4all.nl>
Sun, 26 Nov 2006 14:59:20 +0000 (15:59 +0100)
committerHan-Wen Nienhuys <hanwen@xs4all.nl>
Sun, 26 Nov 2006 15:04:47 +0000 (16:04 +0100)
scm/lily-library.scm

index 7bdf96d05d542a73b97e5a2a9d5f5b1d45e83c87..741a74f73de4bf1d74870d56ecf73ac31c1f7a51 100644 (file)
@@ -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))