aboutsummaryrefslogtreecommitdiffstats
path: root/csc/list.csc
diff options
context:
space:
mode:
authorRose Hogenson <rhogenson@posteo.net>2022-01-11 22:01:23 -0800
committerRose Hogenson <rhogenson@posteo.net>2022-01-11 22:01:23 -0800
commit68986fe0410584c6934c835bb0ee784655f5f8c5 (patch)
treed51bc2f3e09df35ef81b7a0c462ff555b4344701 /csc/list.csc
parentAdd a first implementation of a macro expander. (diff)
downloadchromatopelma-68986fe0410584c6934c835bb0ee784655f5f8c5.tar.zst
Move scheme compiler into a separate directory.
Diffstat (limited to 'csc/list.csc')
-rw-r--r--csc/list.csc64
1 files changed, 64 insertions, 0 deletions
diff --git a/csc/list.csc b/csc/list.csc
new file mode 100644
index 0000000..f425146
--- /dev/null
+++ b/csc/list.csc
@@ -0,0 +1,64 @@
+(define-library (csc list)
+ (export
+ enumerate
+ filter
+ intercalate
+ revappend
+ split-at
+ take)
+ (import (scheme base)
+ (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)))))
+
+
+ (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)))))
+
+
+ (define (revappend a b)
+ (let loop ((xs a)
+ (acc b))
+ (if (null? xs)
+ acc
+ (loop (cdr xs) (cons (car xs) acc)))))
+
+
+ (define (intercalate x l)
+ (match l
+ ('() '())
+ ((_) l)
+ ((head . tail) (cons head (cons x (intercalate x tail))))))
+
+
+ (define (enumerate l)
+ (let loop ((i 0)
+ (l l))
+ (match l
+ ('() '())
+ ((head . tail) (cons (cons i head) (loop (+ 1 i) tail))))))
+
+
+ (define (filter p l)
+ (let loop ((l l)
+ (acc '()))
+ (match l
+ ('() (reverse acc))
+ ((x . xs)
+ (if (p x)
+ (loop xs (cons x acc))
+ (loop xs acc))))))))