-
Notifications
You must be signed in to change notification settings - Fork 0
/
pointfree.zp
67 lines (56 loc) · 1.77 KB
/
pointfree.zp
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
(define-syntax define-compose
(syntax-rules "define a symbol <par>name</par> to be the composition of
the functions <par>fs</par>.
params:
- name: the symbol to which the composition should be bound
- fs: the functions to <par>compose</par> together
complexity: O(1)
returns: the composition of <par>fs</par>" ()
((_ name fs ...)
(define name (compose fs ...)))))
(define-syntax arg-count
(syntax-rules ()
((_ n expr)
(lambda args
(let ((n (length args)))
(apply expr args))))))
(define-syntax define-arg-count
(syntax-rules ()
((_ name n expr)
(define name (arg-count n expr)))))
(define (until-fixpoint f)
"run a given function <par>f</par> until it reaches its fixpoint.
params:
- f: the function to run
complexity: O(n*k) where n is the complexity of the function and k is the number of times we run it
returns: the fixpoint of <par>f</par>"
(define (fixpoint-f v)
(let ((fv (f v)))
(if (eq? fv v)
fv
(fixpoint-f fv))))
fixpoint-f)
(define (join . fs)
"join a number of functions <par>fs</par> with their inputs.
Example:
<zepto>
(define addsub (join add1 sub1))
(addsub [1 1]) ; [2 0]
</zepto>
params:
- fs: the functions to join
complexity: O(1)
returns: a function that takes in a list of inputs to join with <par>fs</par>"
(lambda vs (apply values (for-each apply fs vs))))
(define (join* f)
"join one function <par>f</par> over a list of inputs.
Example:
<zepto>
(define addall (join* add1))
(addall [0 1 2]) ; [1 2 3]
</zepto>
params:
- f: the function to join
complexity: O(1)
returns: a function that takes in a list of inputs to join over <par>f</par>"
(lambda vs (apply values (map f vs))))