forked from LuKuangChen/stacker-on-racket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.rkt
59 lines (48 loc) · 1.53 KB
/
utilities.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#lang plait
(require (rename-in (typed-in racket [format : (String 'a -> String)])
[format raw-format]))
(define-syntax let-values
(syntax-rules ()
[(let-values (((x ...) e)) body)
(let ([tmp e])
(local ((define-values (x ...) tmp))
body))]))
(define (displayln x)
(begin
(display x)
(display "\n")))
(define (string-of any)
(format "~a" any))
(define (string-join t s*)
(ind-List s*
""
(lambda (_IH s)
(let ([string*-of-s* (map (lambda (s) (string-append t s)) s*)])
(string-append s
(ind-List string*-of-s*
""
(lambda (IH elm)
(string-append elm IH))))))))
(define (format template datum)
(raw-format template datum))
(define (ind-List (x* : (Listof 'a)) (base : 'b) (step : ('b 'a -> 'b)))
(foldr (λ (x IH) (step IH x)) base x*))
(define (hash-set* base ⟨k×v⟩*)
(ind-List ⟨k×v⟩*
base
(λ (IH ⟨k×v⟩)
(hash-set IH (fst ⟨k×v⟩) (snd ⟨k×v⟩)))))
(define (hash-ref* h k*)
(hash-set* (hash (list))
(map (lambda (k)
(values k (some-v (hash-ref h k))))
k*)))
(define-type (Dec 'x 'y)
(yes [it : 'x])
(no [it : 'y]))
(define (get-last xs)
(type-case (Listof 'x) (reverse xs)
(empty
(no (values)))
((cons x xs)
(yes (values (reverse xs) x)))))