aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRose Hogenson <rhogenson@posteo.net>2022-01-09 13:17:20 -0800
committerRose Hogenson <rhogenson@posteo.net>2022-01-09 13:17:20 -0800
commit6b8be199ae88979e508f98fb8a8b6fc17c2a7f2d (patch)
tree38a11e7c79208f612356086022561f00d5c22e48
parentImprove match syntax. (diff)
downloadchromatopelma-6b8be199ae88979e508f98fb8a8b6fc17c2a7f2d.tar.zst
Define helper intercalate.
-rw-r--r--list-test.csc32
-rw-r--r--list.csc14
2 files changed, 44 insertions, 2 deletions
diff --git a/list-test.csc b/list-test.csc
index ec4b4ed..e4981e5 100644
--- a/list-test.csc
+++ b/list-test.csc
@@ -125,3 +125,35 @@
(unless (equal? got (want tc))
(errorf t "(revappend {} {}) = {}, want {}." (a tc) (b tc) got (want tc)))))
tests)))
+
+
+(define-test (test-intercalate t)
+ (define-record-type <test-case>
+ (test-case desc x l want)
+ test-case?
+ (desc desc)
+ (x x)
+ (l l)
+ (want want))
+ (let ((tests (list
+ (test-case
+ "simple"
+ ","
+ '("a" "b" "c")
+ '("a" "," "b" "," "c"))
+ (test-case
+ "empty"
+ ","
+ '()
+ '())
+ (test-case
+ "singleton"
+ ","
+ '(1)
+ '(1)))))
+ (for-each
+ (lambda (tc)
+ (let ((got (intercalate (x tc) (l tc))))
+ (unless (equal? got (want tc))
+ (errorf t "(intercalate {} {}) = {}, want {}." (x tc) (l tc) got (want tc)))))
+ tests)))
diff --git a/list.csc b/list.csc
index c4bc013..799b070 100644
--- a/list.csc
+++ b/list.csc
@@ -1,9 +1,12 @@
(define-library (csc list)
(export
+ intercalate
revappend
split-at
take)
- (import (scheme base))
+ (import (scheme base)
+ (only (csc match)
+ match))
(begin
@@ -30,4 +33,11 @@
(acc b))
(if (null? xs)
acc
- (loop (cdr xs) (cons (car 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))))))))