-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommon.rkt
111 lines (96 loc) · 2.24 KB
/
common.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#lang eopl
(require (only-in racket/base
format))
(provide out)
(provide filter)
(provide my-foldl)
(provide concat)
(provide concat-map)
(provide assert)
(provide curry2)
(provide memf)
(provide const)
(provide identity)
(provide format)
(provide non-empty?)
(provide compose)
(provide flip)
; compose unaries
; compose (... h . g . f) x = ... $ h $ g $ f x
; => compose-inv (f . g . h ...) x
; => (compose-inv (g . h ...)) $ f x
(define (compose . procs)
(define (compose-inv procs)
(if (null? procs)
identity
(lambda (x)
((compose-inv (cdr procs)) ((car procs) x)))))
(let ((procs-inv (reverse procs)))
(compose-inv procs-inv)))
; flip f x y = f y x
(define (flip f)
(lambda (a b) (f b a)))
; to prove readability of `pair?` that actually means
; a non-empty stuff
(define non-empty? pair?)
; output all arguments, line-by-line
(define out
(lambda args
(for-each
(lambda (a) (display a) (newline))
args)))
(define (filter pred lst)
(if (null? lst)
'()
(if (pred (car lst))
(cons (car lst)
(filter pred (cdr lst)))
(filter pred (cdr lst)))))
; fold from left using `proc`
(define (my-foldl proc init lst)
(if (null? lst)
init
(my-foldl
proc
(proc init (car lst))
(cdr lst))))
; concat lists
(define (concat xs)
(apply append xs))
; map and then concat
(define (concat-map proc . args)
(concat (apply map (cons proc args))))
; assertion with optional reason
(define (assert v . reason)
(if v
'done
(eopl:error
'assert
(string-append
"Assertion failed"
(if (null? reason)
""
(string-append
": " (car reason)))))))
; return curried version of a binary function
(define (curry2 f)
(lambda (a)
(lambda (b)
(f a b))))
; memf: (a -> Bool) -> [a] -> Either a Bool
; search through `xs`, and get the list starting from the first element that
; satisfies `pred`, return #f if `xs` does not have an element
; that meets the predicate.
(define (memf pred xs)
(if (null? xs)
#f
(if (pred (car xs))
xs
(memf pred (cdr xs)))))
; return `v` whatever is given
(define (const v)
(lambda ignored
v))
; identity function
(define (identity x)
x)