-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.egi
More file actions
139 lines (87 loc) · 2.78 KB
/
test.egi
File metadata and controls
139 lines (87 loc) · 2.78 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
(match-all {1 2 3} (List Integer)
[<cons $x $xs> [x xs]])
-> {[1 {2 3}]}
(match-all {1 2 3} (Multiset Integer)
[<cons $x $xs> [x xs]])
-> {[1 {2 3}] [2 {1 3}] [3 {1 2}]}
(match-all {1 2 3} (Set Integer)
[<cons $x $xs> [x xs]])
-> {[1 {2 3}] [2 {1 3}] [3 {1 2}]
[1 {2 3 1}] [2 {1 3 2}] [3 {1 2 3}]}
(match-all {2 8 7 2 7} (Multiset Integer)
[<cons $x <cons ,x _>> x])
-> {2 7}
(match-all {3 4 2 1 5 6} (Multiset Integer)
[<cons $x
<cons ,(- x 1)
<cons ,(- x 2)
<cons ,(- x 3)
_>>>>
x])
-> {4 5 6}
(match-all {1 2 1 4 2 1} (Multiset Integer)
[<cons $x ^<cons ,x _>> x])
-> {4}
(loop $l $i {1 2 3} <cons $a_i l> _)
-> <cons $a_1 <cons $a_2 <cons $a_3 _>>>
(loop $l $i (between 1 n) <cons $a_i l> _)
-> <cons $a_1 <cons $a_2 ... <cons $a_n _>...>>
(define $four-queen
(match-all {1 2 3 4} (Multiset Integer)
[<cons $a_1
<cons (& ^,(- a_1 1) ^,(+ a_1 1)
$a_2)
<cons (& ^,(- a_1 2) ^,(+ a_1 2)
^,(- a_2 1) ^,(+ a_2 1)
$a_3)
<cons (& ^,(- a_1 3) ^,(+ a_1 3)
^,(- a_2 2) ^,(+ a_2 2)
^,(- a_3 1) ^,(+ a_3 1)
$a_4)
<nil>>>>>
[a_1 a_2 a_3 a_4]]))
-> {[2 4 1 3] [3 1 4 2]}
(define $four-queen
(match-all {1 2 3 4} (Multiset Integer)
[<cons $a_1
(loop $l $i {2 3 4}
<cons (loop $l1 $i1 (between 1 (- i 1))
(& ^,(- a_i1 (- i i1))
^,(+ a_i1 (- i i1))
l1)
$a_i)
l>
<nil>)>
[a_1 a_2 a_3 a_4]]))
(define $n-queen
(lambda [$n]
(match-all (between 1 n) (Multiset Integer)
[<cons $a_1
(loop $l $i (between 2 n)
<cons (loop $l1 $i1 (between 1 (- i 1))
(& ^,(- a_i1 (- i i1))
^,(+ a_i1 (- i i1))
l1)
$a_i)
l>
<nil>)>
(loop $l $i (between 1 n) {a_i @l} {})])))
(define $junshi
(macro [$s $pat]
<cons $`s <cons ,(+ `s 1) <cons ,(+ `s 2) pat>>>))
(test (match-all {1 3 5 6 2 4} (Multiset Integer)
[(junshi %m (junshi %n _)) [m n]]))
-> {[1 4] [4 1]}
(define $kokushi
(macro [$s $pat]
<cons $`s <cons ,`s <cons ,`s pat>>>))
(test (match-all {1 3 5 5 2 5} (Multiset Integer)
[(junshi %m (kokushi %n _)) [m n]]))
-> {[1 5]}
(test (match-all {1 1 1} (Multiset Integer)
[<cons $x $xs> [x xs]]))
(test (match {undefined 2} (List Integer) {[<cons $y $x> x]}))
(test (let {[[$x $y] [undefined 2]]} y))
(define $f (lambda [$x $y $z] [x y z]))
(test (f 1))
(test (let {[[$x $y $z] [1 2]]} x))