|
28 | 28 | (= (List.sum $xs) (List.foldr + 0 $xs)) |
29 | 29 |
|
30 | 30 | ; (: List.append (-> $a (List $a) (List $a))) |
31 | | -(= (List.append $val ()) (cons-atom $val ())) |
| 31 | +(= (List.append $val ()) ($val)) |
32 | 32 | (= (List.append $val (cons $head $tail)) |
33 | 33 | (cons-atom $head (List.append $val $tail))) |
34 | 34 |
|
|
40 | 40 | (index-atom $xs $idx))) |
41 | 41 |
|
42 | 42 | ; (: List.insert (-> $a (List $a) (List $a))) |
43 | | -(= (List.insert $x ()) (cons-atom $x ())) |
| 43 | +(= (List.insert $x ()) ($x)) |
44 | 44 | (= (List.insert $x (cons $head $tail)) |
45 | 45 | (if (< $x $head) |
46 | | - (cons-atom $x (cons-atom $head $tail)) |
47 | | - (cons-atom $head (List.insert $x $tail)))) |
| 46 | + (cons $x (cons $head $tail)) |
| 47 | + (cons $head (List.insert $x $tail)))) |
48 | 48 |
|
49 | 49 | ;; Sort a list in ascending order by default. |
50 | 50 | (= (List.sort ()) ()) |
51 | | -(= (List.sort $xs) (List.sort $xs <)) |
| 51 | +(= (List.sort (cons $x $xs)) (List.sort (cons $x $xs) <)) |
52 | 52 | (= (List.sort $xs $op) |
53 | 53 | (selectionSort $xs (size-atom $xs) $op)) |
54 | 54 |
|
|
64 | 64 | (let $new-head ($f $head $arg) |
65 | 65 | (if (== $new-head ()) |
66 | 66 | (List.mapOverArg $f $tail $arg) |
67 | | - (cons-atom $new-head (List.mapOverArg $f $tail $arg))))) |
| 67 | + (cons $new-head (List.mapOverArg $f $tail $arg))))) |
68 | 68 |
|
69 | 69 | ; (: List.filter (-> (-> $a Bool) (List $a) (List $a))) |
70 | 70 | (= (List.filter $p $xs) (filter-atom $xs $p)) |
|
86 | 86 | (= (List.max $xs) (List.max >= $xs)) |
87 | 87 |
|
88 | 88 | ; (: List.contains (-> $a (List $a) Bool)) |
89 | | -(= (List.contains $a ()) False) |
90 | | -(= (List.contains $a (cons $head $tail)) |
91 | | - (if (== $a $head) True (List.contains $a $tail))) |
| 89 | +(= (List.contains $a $list) (is-member $a $list)) |
92 | 90 |
|
93 | 91 | ; (: List.replaceAt (-> (List $a) Number $a (List $a))) |
94 | 92 | (= (List.replaceAt () $n $elem) ()) |
95 | 93 | (= (List.replaceAt (cons $head $tail) $n $elem) |
96 | 94 | (if (== $n 0) |
97 | | - (cons-atom $elem $tail) |
98 | | - (cons-atom $head (List.replaceAt $tail (- $n 1) $elem)))) |
| 95 | + (cons $elem $tail) |
| 96 | + (cons $head (List.replaceAt $tail (- $n 1) $elem)))) |
99 | 97 |
|
100 | 98 | ; (: List.prepend (-> $a (List $a) (List $a))) |
101 | | -(= (List.prepend $a $list) (cons-atom $a $list)) |
102 | | - |
| 99 | +(= (List.prepend $a $list) (cons $a $list)) |
| 100 | + |
| 101 | +;; Find an element in a list |
| 102 | +;; Params: |
| 103 | +;; $list: The list to search |
| 104 | +;; $target: The element to find |
| 105 | +;; Returns: |
| 106 | +;; The index of the first found element or |
| 107 | +;; -1 If the element can't be found |
103 | 108 | ; (: List.index (-> (List $a) $a Number)) |
104 | 109 | (= (List.index () $target) -1) |
105 | 110 | (= (List.index (cons $x $xs) $target) |
|
116 | 121 | (= (List.tail ()) ()) |
117 | 122 | (= (List.tail (cons $x $xs)) $xs) |
118 | 123 |
|
| 124 | +;; INFO: The previous overloaded version doesn't work here after changing to expression. |
119 | 125 | ; (: List.sub (-> (List Number) (List Number) (List Number))) |
120 | 126 | (= (List.sub $xs $ys) |
121 | 127 | (if (== $xs ()) |
|
138 | 144 | (= (List.zip () (cons $y $ys)) ()) |
139 | 145 | (= (List.zip (cons $x $xs) ()) ()) |
140 | 146 | (= (List.zip (cons $x $xs) (cons $y $ys)) |
141 | | - (cons-atom ($x $y) (List.zip $xs $ys))) |
| 147 | + (cons ($x $y) (List.zip $xs $ys))) |
142 | 148 |
|
143 | 149 | ; (: List.zipWith (-> (-> $a $b $c) (List $a) (List $b) (List $c))) |
144 | 150 | (= (List.zipWith $f () ()) ()) |
145 | 151 | (= (List.zipWith $f () (cons $y $ys)) ()) |
146 | 152 | (= (List.zipWith $f (cons $x $xs) ()) ()) |
147 | 153 | (= (List.zipWith $f (cons $x $xs) (cons $y $ys)) |
148 | | - (cons-atom ($f $x $y) (List.zipWith $f $xs $ys))) |
| 154 | + (cons ($f $x $y) (List.zipWith $f $xs $ys))) |
149 | 155 |
|
150 | 156 | ; (: List.drop (-> Number (List $a) (List $a))) |
151 | 157 | (= (List.drop $n ()) ()) |
152 | 158 | (= (List.drop $n (cons $x $xs)) |
153 | 159 | (if (== $n 0) |
154 | | - (cons-atom $x $xs) |
| 160 | + (cons $x $xs) |
155 | 161 | (List.drop (- $n 1) $xs))) |
156 | 162 |
|
| 163 | +;; INFO: Behavior has changed from previous List.flatten. Investigation needed if causes an error. |
157 | 164 | ; (: List.flatten (-> (List (List $a)) (List $a))) |
158 | 165 | (= (List.flatten ()) ()) |
159 | 166 | (= (List.flatten (cons $x $xs)) |
|
165 | 172 | (= (List.repeat $n $a) |
166 | 173 | (if (== $n 0) |
167 | 174 | () |
168 | | - (cons-atom $a (List.repeat (- $n 1) $a)))) |
| 175 | + (cons $a (List.repeat (- $n 1) $a)))) |
169 | 176 |
|
170 | 177 | ; (: List.concat (-> (List $a) (List $a) (List $a))) |
171 | 178 | (= (List.concat $list1 $list2) (append $list1 $list2)) |
|
174 | 181 | (= (List.removeAtIdx () $idx) ()) |
175 | 182 | (= (List.removeAtIdx (cons $head $tail) $idx) |
176 | 183 | (if (< $idx 0) |
177 | | - (cons-atom $head $tail) |
| 184 | + (cons $head $tail) |
178 | 185 | (if (== $idx 0) |
179 | 186 | $tail |
180 | | - (cons-atom $head (List.removeAtIdx $tail (- $idx 1)))))) |
| 187 | + (cons $head (List.removeAtIdx $tail (- $idx 1)))))) |
181 | 188 |
|
182 | 189 | ; (: List.isMember (-> $a (List $a) Bool)) |
183 | 190 | (= (List.isMember $elem $list) (List.contains $elem $list)) |
184 | 191 |
|
185 | 192 | ; (: List.partialSort (-> (-> $a $a Bool) (List $a) Number (List $a) (List $a))) |
186 | 193 | (= (List.partialSort $comparator () $n $acc) $acc) |
187 | | -(= (List.partialSort $comparator $xs $n $acc) |
188 | | - (let* (($max (List.max $comparator $xs)) |
189 | | - ($unsortedList (List.delete $max $xs)) |
| 194 | +(= (List.partialSort $comparator (cons $x $xs) $n $acc) |
| 195 | + (let* (($max (List.max $comparator (cons $x $xs))) |
| 196 | + ($unsortedList (List.delete $max (cons $x $xs))) |
190 | 197 | ($sortedList (List.append $max $acc))) |
191 | 198 | (if (<= (- $n 1) 0) |
192 | 199 | (List.appendList $unsortedList $sortedList) |
|
203 | 210 | (= (List.delete $a (cons $x $xs)) |
204 | 211 | (if (== $x $a) |
205 | 212 | $xs |
206 | | - (cons-atom $x (List.delete $a $xs)))) |
| 213 | + (cons $x (List.delete $a $xs)))) |
207 | 214 |
|
208 | 215 | ; (: List.takeN (-> Number (List $a) (List $a))) |
209 | 216 | (= (List.takeN $n ()) ()) |
|
222 | 229 | ; (: List.generate (-> Number Number (List Number))) |
223 | 230 | (= (List.generate $length $element) |
224 | 231 | (if (> $length 0) |
225 | | - (cons-atom $element (List.generate (- $length 1) $element)) |
| 232 | + (cons $element (List.generate (- $length 1) $element)) |
226 | 233 | ())) |
227 | 234 |
|
228 | 235 | ;; Remove an element from a list by index. |
|
231 | 238 | (if (== $i 0) |
232 | 239 | $xs |
233 | 240 | (chain (List.pop (- $i 1) $xs) $res |
234 | | - (if-error $res (cons-atom $x ()) (cons-atom $x $res))))) |
| 241 | + (if-error $res (cons $x ()) (cons $x $res))))) |
235 | 242 |
|
236 | 243 | (= (List.splitAt $i $xs) |
237 | 244 | ((List.takeN $i $xs) (List.drop $i $xs))) |
0 commit comments