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 | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 24 | ;; Instead of a function this is a macro for a slight performance increase |
| 25 | (defmacro not (val) |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 26 | (list 'nilp val)) |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 27 | |
| 28 | ;; TODO: make tail recursive (for this `flet` would be nice) |
| 29 | (defun length (list) |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 30 | "Returns the length of `list`, or `nil` if it is not a list" |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 31 | (if (nilp list) |
| 32 | 0 |
| 33 | (+ 1 (length (cdr list))))) |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 34 | |
| 35 | (defmacro when (cond & body) |
| 36 | "Evaluate `body` when `cond` is truthy. |
| 37 | When `cond` is truthy, evaluates `body` in order, finally evaluating to |
| 38 | the final item." |
| 39 | (list 'if cond |
| 40 | (cons 'progn body))) |
| 41 | |
| 42 | (defmacro unless (cond & body) |
| 43 | "Evaluate `body` unless `cond` is truthy. |
| 44 | When `cond` is nil, evaluates `body` in order, finally evaluating to the |
| 45 | final item." |
| 46 | (list 'if cond |
| 47 | nil |
| 48 | (cons 'progn body))) |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame^] | 49 | |
| 50 | (defun read ((stream nil)) |
| 51 | (if (not stream) |
| 52 | (read-stdin))) |
| 53 | |
| 54 | (print "Loading list functions") |
| 55 | (print (load "list-functions.lisp")) |