From 76b7e6eeaf99e5aeac3d9f651bc548f2c537ce85 Mon Sep 17 00:00:00 2001 From: Case Duckworth Date: Thu, 24 Aug 2023 12:42:22 -0500 Subject: bleh --- lib/util.sld | 38 +++++++++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) (limited to 'lib/util.sld') diff --git a/lib/util.sld b/lib/util.sld index 64c633e..fe407a2 100644 --- a/lib/util.sld +++ b/lib/util.sld @@ -7,9 +7,12 @@ (export identity o + constantly assoc-ref slurp - eval/q) + eval/q + intersperse + string-intersperse) (begin (define (identity x) x) @@ -24,6 +27,9 @@ h (lambda (x) (h ((loop t) x)))))))) + (define (constantly x) + (lambda _ x)) + (define assoc-ref (case-lambda ((key alist) @@ -47,4 +53,34 @@ (define (eval/q form env) ; this is probably a bad idea (eval (list 'quasiquote form) env)) + (define (intersperse xs delim) + (if (null? xs) + '() + (let loop ((acc (list (car xs))) + (rest (cdr xs))) + (if (null? rest) + (reverse acc) + (loop (cons (car rest) (cons delim acc)) + (cdr rest)))))) + + (define (string-intersperse ss delim) + (apply string-append (intersperse ss delim))) + + (define index + (case-lambda + ((xs needle) + (index xs needle eq? (constantly #f))) + ((xs needle comparator) + (index xs needle comparator (constantly #f))) + ((xs needle comparator fail) + (let loop ((i 0) + (xs xs)) + (cond + ((null? xs) (fail)) + ((comparator (car xs) needle) i) + (else (loop (+ i 1) (cdr xs)))))))) + + (define (string-index str ch) + (index (string->list str) ch)) + )) -- cgit 1.4.1-21-gabe81