blob: 388637bbdff724d77b8c04b00f145caff755921d [file] [log] [blame]
swissChilif68671f2021-07-05 14:14:44 -07001;;;; std.lisp -- Lisp standard library
2
swissChili15f1cae2021-07-05 19:08:47 -07003;; Boring utilities
4
swissChilif68671f2021-07-05 14:14:44 -07005(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
swissChili15f1cae2021-07-05 19:08:47 -070020;; /Boring utilitites
21
22
23
swissChilif68671f2021-07-05 14:14:44 -070024;; Instead of a function this is a macro for a slight performance increase
25(defmacro not (val)
26 (nilp val))
27
28;; TODO: make tail recursive (for this `flet` would be nice)
29(defun length (list)
swissChili15f1cae2021-07-05 19:08:47 -070030 "Returns the length of `list`, or `nil` if it is not a list"
swissChilif68671f2021-07-05 14:14:44 -070031 (if (nilp list)
32 0
33 (+ 1 (length (cdr list)))))
swissChili15f1cae2021-07-05 19:08:47 -070034
35(defmacro when (cond & body)
36 "Evaluate `body` when `cond` is truthy.
37When `cond` is truthy, evaluates `body` in order, finally evaluating to
38the final item."
39 (list 'if cond
40 (cons 'progn body)))
41
42(defmacro unless (cond & body)
43 "Evaluate `body` unless `cond` is truthy.
44When `cond` is nil, evaluates `body` in order, finally evaluating to the
45final item."
46 (list 'if cond
47 nil
48 (cons 'progn body)))