aboutsummaryrefslogtreecommitdiffstats
path: root/csc/list.csc
diff options
context:
space:
mode:
authorRose Hogenson <rhogenson@posteo.net>2022-01-22 17:51:20 -0800
committerRose Hogenson <rhogenson@posteo.net>2022-01-22 17:51:20 -0800
commita54f483b1f9ae3f03f1f00124eec296c2d9218d7 (patch)
treea59b4cf13cca2b4adfec2f65a78ac63de5199b9e /csc/list.csc
parent0d7cb228076354f893527bce2d426e21697832a9 (diff)
downloadchromatopelma-a54f483b1f9ae3f03f1f00124eec296c2d9218d7.tar.zst
Add a loop macro.
Diffstat (limited to 'csc/list.csc')
-rw-r--r--csc/list.csc23
1 files changed, 11 insertions, 12 deletions
diff --git a/csc/list.csc b/csc/list.csc
index f425146..dcc1766 100644
--- a/csc/list.csc
+++ b/csc/list.csc
@@ -7,27 +7,26 @@
split-at
take)
(import (scheme base)
+ (only (csc loop)
+ loop
+ return)
(only (csc match)
match))
(begin
(define (take n xs)
- (let loop ((n n)
- (xs xs)
- (acc '()))
- (if (or (not (positive? n)) (null? xs))
- (reverse acc)
- (loop (- n 1) (cdr xs) (cons (car xs) acc)))))
+ (loop for x in xs
+ for i from 1 to n
+ collect x))
(define (split-at n xs)
- (let loop ((n n)
- (xs xs)
- (acc '()))
- (if (or (not (positive? n)) (null? xs))
- (values (reverse acc) xs)
- (loop (- n 1) (cdr xs) (cons (car xs) acc)))))
+ (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))))
(define (revappend a b)