swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 1 | ;;;; std.lisp -- Lisp standard library |
| 2 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 3 | ;; Boring utilities |
| 4 | |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 5 | (defun caar (val) |
| 6 | (car (car val))) |
| 7 | |
| 8 | (defun cadr (val) |
| 9 | (car (cdr val))) |
| 10 | |
| 11 | (defun caddr (val) |
| 12 | (car (cdr (cdr val)))) |
| 13 | |
| 14 | (defun cadar (val) |
| 15 | (car (cdr (car val)))) |
| 16 | |
| 17 | (defun caddar (val) |
| 18 | (car (cdr (cdr (car val))))) |
| 19 | |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 20 | ;; /Boring utilitites |
| 21 | |
| 22 | |
| 23 | |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 24 | (defun not (val) |
| 25 | (nilp val)) |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 26 | |
| 27 | ;; TODO: make tail recursive (for this `flet` would be nice) |
| 28 | (defun length (list) |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 29 | "Returns the length of `list`, or `nil` if it is not a list" |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 30 | (if (nilp list) |
| 31 | 0 |
| 32 | (+ 1 (length (cdr list))))) |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 33 | |
| 34 | (defmacro when (cond & body) |
| 35 | "Evaluate `body` when `cond` is truthy. |
| 36 | When `cond` is truthy, evaluates `body` in order, finally evaluating to |
| 37 | the final item." |
| 38 | (list 'if cond |
| 39 | (cons 'progn body))) |
| 40 | |
| 41 | (defmacro unless (cond & body) |
| 42 | "Evaluate `body` unless `cond` is truthy. |
| 43 | When `cond` is nil, evaluates `body` in order, finally evaluating to the |
| 44 | final item." |
| 45 | (list 'if cond |
| 46 | nil |
| 47 | (cons 'progn body))) |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 48 | |
| 49 | (defun read ((stream nil)) |
| 50 | (if (not stream) |
| 51 | (read-stdin))) |
| 52 | |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 53 | (defun funcall (fun & list) |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 54 | (apply fun list)) |
| 55 | |
| 56 | (load "list-functions.lisp") |