aboutsummaryrefslogtreecommitdiff
path: root/module/hnh/util.scm
diff options
context:
space:
mode:
authorHugo Hörnquist <hugo@lysator.liu.se>2023-04-10 22:02:44 +0200
committerHugo Hörnquist <hugo@lysator.liu.se>2023-04-10 23:45:29 +0200
commit93d0ba9844dbba55658ce9c0d8570e294bd096b0 (patch)
tree4f4a24ebc152a2c044a6b70be7a3fc1d3cd5d301 /module/hnh/util.scm
parentAdd number of missing (hnh util) tests. (diff)
downloadcalp-93d0ba9844dbba55658ce9c0d8570e294bd096b0.tar.gz
calp-93d0ba9844dbba55658ce9c0d8570e294bd096b0.tar.xz
Add init+last.
Diffstat (limited to 'module/hnh/util.scm')
-rw-r--r--module/hnh/util.scm14
1 files changed, 12 insertions, 2 deletions
diff --git a/module/hnh/util.scm b/module/hnh/util.scm
index 759cdd48..c88a029e 100644
--- a/module/hnh/util.scm
+++ b/module/hnh/util.scm
@@ -17,6 +17,7 @@
find-extreme find-min find-max
filter-sorted
!=
+ init+last
take-to
string-take-to
string-first
@@ -150,9 +151,12 @@
+;; TODO this is called flip in Haskell land
(define (swap f)
(lambda args (apply f (reverse args))))
-
+;; Swap would be
+;; (define (swap p)
+;; (xcons (car p) (cdr p)))
;; Allow set to work on multiple values at once,
;; similar to Common Lisp's @var{setf}
@@ -253,6 +257,12 @@
;; (define (!= a b) (not (= a b)))
(define != (negate =))
+
+(define (init+last l)
+ (let ((last rest (car+cdr (reverse l))))
+ (values (reverse rest) last)))
+
+
(define (take-to lst i)
"Like @var{take}, but might lists shorter than length."
(if (> i (length lst))
@@ -396,7 +406,7 @@
(reverse (cons (map list last) rest ))))))
;; Given an arbitary tree, do a pre-order traversal, appending all strings.
-;; non-strings allso allowed, converted to strings and also appended.
+;; non-strings also allowed, converted to strings and also appended.
(define (string-flatten tree)
(cond [(string? tree) tree]
[(list? tree) (string-concatenate (map string-flatten tree))]