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 | |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 22 | (defun not (val) |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 23 | "Identical to NILP, returns T if VAL is NIL, NIL otherwise." |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 24 | (nilp val)) |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 25 | |
| 26 | ;; TODO: make tail recursive (for this `flet` would be nice) |
| 27 | (defun length (list) |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 28 | "Returns the length of LIST, or NIL if it is not a list" |
swissChili | f68671f | 2021-07-05 14:14:44 -0700 | [diff] [blame] | 29 | (if (nilp list) |
| 30 | 0 |
| 31 | (+ 1 (length (cdr list))))) |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 32 | |
| 33 | (defmacro when (cond & body) |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 34 | (list 'if cond |
| 35 | (cons 'progn body))) |
| 36 | |
| 37 | (defmacro unless (cond & body) |
swissChili | 15f1cae | 2021-07-05 19:08:47 -0700 | [diff] [blame] | 38 | (list 'if cond |
| 39 | nil |
| 40 | (cons 'progn body))) |
swissChili | 7e1393c | 2021-07-07 12:59:12 -0700 | [diff] [blame] | 41 | |
| 42 | (defun read ((stream nil)) |
| 43 | (if (not stream) |
| 44 | (read-stdin))) |
| 45 | |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 46 | (defun funcall (fun & list) |
swissChili | 484295d | 2021-07-09 21:25:55 -0700 | [diff] [blame] | 47 | (apply fun list)) |
| 48 | |
swissChili | fc5c941 | 2021-08-08 19:08:26 -0700 | [diff] [blame] | 49 | (defmacro flet1 (func & body) |
| 50 | `(let1 (,(car func) |
| 51 | (lambda ,@(cdr func))) |
| 52 | ,@body)) |
| 53 | |
swissChili | fc5c941 | 2021-08-08 19:08:26 -0700 | [diff] [blame] | 54 | (defmacro flet (funcs & body) |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 55 | (let1 (flet- |
| 56 | (lambda (funcs body) |
| 57 | (if funcs |
| 58 | `(flet1 ,(car funcs) |
| 59 | ,(recurse (cdr funcs) |
| 60 | body)) |
| 61 | `(progn ,@body)))) |
| 62 | (funcall flet- funcs body))) |
swissChili | 9d428a8 | 2022-08-01 20:47:40 -0700 | [diff] [blame] | 63 | |
| 64 | (defmacro let (bindings & body) |
| 65 | (flet ((let- (bindings body) |
| 66 | (if bindings |
| 67 | `(let1 ,(car bindings) |
| 68 | ,(recurse (cdr bindings) |
| 69 | body)) |
| 70 | `(progn ,@body))))) |
| 71 | (funcall let- bindings body)) |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 72 | |
swissChili | 1e8b756 | 2021-12-22 21:22:57 -0800 | [diff] [blame] | 73 | (load "list-functions.lisp") |
swissChili | 8b5ec7a | 2022-08-05 22:26:17 -0700 | [diff] [blame] | 74 | (load "oop.lisp") |