diff options
Diffstat (limited to 'csc/list.csc')
| -rw-r--r-- | csc/list.csc | 24 |
1 files changed, 17 insertions, 7 deletions
diff --git a/csc/list.csc b/csc/list.csc index dcc1766..95fff04 100644 --- a/csc/list.csc +++ b/csc/list.csc @@ -5,7 +5,8 @@ intercalate revappend split-at - take) + take + unzip) (import (scheme base) (only (csc loop) loop @@ -22,11 +23,13 @@ (define (split-at n xs) - (loop for i from 1 to n - for x in xs - collect x into first-half - for second-half = xs then (cdr second-half) - finally (return (values first-half second-half)))) + (if (<= n 0) + (values '() xs) + (loop for i from 1 to n + for x in xs + for second-half = (cdr xs) then (cdr second-half) + collect x into first-half + finally (return (values first-half second-half))))) (define (revappend a b) @@ -60,4 +63,11 @@ ((x . xs) (if (p x) (loop xs (cons x acc)) - (loop xs acc)))))))) + (loop xs acc)))))) + + + (define (unzip l) + (loop for x in l + collect (car x) into xs + collect (cdr x) into ys + finally (return (values xs ys)))))) |
