swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 1 | (defun mapcar (fun list) |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 2 | "Maps over the cars of `list` by running `fun` on each one. |
| 3 | List must be a cons-list, other sequences are not supported." |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 4 | (if list |
| 5 | (cons (funcall fun (car list)) |
| 6 | (mapcar fun (cdr list))) |
| 7 | nil)) |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 8 | |
| 9 | (defun remove-if (predicate list) |
| 10 | "Returns a copy of `list` with elements satisfying `predicate` |
| 11 | removed." |
| 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` |
| 21 | removed." |
| 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 |
| 28 | accept two arguments and return the result of combining those |
| 29 | arguments. The first argument will be the result so far and the second |
| 30 | will be the n-th item of the list. For the first item of the list, the |
| 31 | result so far will be `initial-value`, or `nil` by default." |
swissChili | 53e7cd1 | 2021-08-02 21:55:53 -0700 | [diff] [blame] | 32 | (if (nilp list) |
| 33 | initial-value |
| 34 | (reduce fun (cdr list) |
| 35 | (funcall fun initial-value |
| 36 | (car list))))) |