blob: 7bb22a7b8e6e08d35c9e591167742d3ecf6dab5b [file] [log] [blame]
swissChili7e1393c2021-07-07 12:59:12 -07001(defun mapcar (fun list)
swissChili484295d2021-07-09 21:25:55 -07002 "Maps over the cars of `list` by running `fun` on each one.
3List must be a cons-list, other sequences are not supported."
swissChili7e1393c2021-07-07 12:59:12 -07004 (if list
5 (cons (funcall fun (car list))
6 (mapcar fun (cdr list)))
7 nil))
swissChili484295d2021-07-09 21:25:55 -07008
9(defun remove-if (predicate list)
10 "Returns a copy of `list` with elements satisfying `predicate`
11removed."
12 (if (not list)
13 nil
14 (if (funcall predicate (car list))
15 (remove-if predicate (cdr list))
16 (cons (car list)
17 (remove-if predicate (cdr list))))))
18
19(defun remove-if-not (predicate list)
20 "Returns a copy of `list` with elements not satisfying `predicate`
21removed."
22 (remove-if (lambda (val)
23 (not (funcall predicate val)))
24 list))
25
26(defun reduce (fun list (initial-value nil))
27 "Combines elements of `list` into one element using `fun`. `fun` must
28accept two arguments and return the result of combining those
29arguments. The first argument will be the result so far and the second
30will be the n-th item of the list. For the first item of the list, the
31result so far will be `initial-value`, or `nil` by default."
32 (if (not list)
33 initial-value
34 (reduce fun (cdr list)
35 (funcall fun initial-value (car list)))))