From a77e83a973b89c0cc2b99e1aea8965a3464d29ec Mon Sep 17 00:00:00 2001 From: Han-Wen Nienhuys Date: Sun, 26 Nov 2006 15:59:20 +0100 Subject: [PATCH] add split-list , split-list-by-separator , list-element-index --- scm/lily-library.scm | 32 ++++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) 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)) -- 2.39.2