swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 1 | Lisp Standard Library |
| 2 | ===================== |
| 3 | |
| 4 | This provides documentation for every built-in function in the Lisp standard |
| 5 | library. It is not auto-generated, please update this documentation if you |
| 6 | change the API in any way. |
| 7 | |
| 8 | In general every user-facing API in the standard library should be documented |
| 9 | here. |
| 10 | |
swissChili | 61ced3b | 2021-07-02 14:52:05 -0700 | [diff] [blame] | 11 | - ``(x ...)`` represents a list ``x``. |
| 12 | - ``& body`` means that the rest of the list is represented by ``body``. |
| 13 | - ``[something]`` means that ``something`` is optional. |
| 14 | |
| 15 | Top-level primitives |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 16 | -------------------- |
| 17 | |
swissChili | 61ced3b | 2021-07-02 14:52:05 -0700 | [diff] [blame] | 18 | These are "functions" that can only appear at the top-level of the program. This |
| 19 | means they can't be nested in any other expressions. |
swissChili | c3b56c1 | 2021-07-02 20:52:09 -0700 | [diff] [blame] | 20 | |
swissChili | 61ced3b | 2021-07-02 14:52:05 -0700 | [diff] [blame] | 21 | .. function:: (defun function-name (args ...) & body) |
| 22 | |
| 23 | Defines a function ``function-name`` that takes ``args`` and evaluates |
| 24 | ``body``. ``function-name`` is quoted, not evaluated. |
swissChili | c3b56c1 | 2021-07-02 20:52:09 -0700 | [diff] [blame] | 25 | |
| 26 | .. code-block:: lisp |
swissChili | 61ced3b | 2021-07-02 14:52:05 -0700 | [diff] [blame] | 27 | |
| 28 | (defun say-hi (name) |
| 29 | (print "Hi, ") |
| 30 | (print name)) |
| 31 | |
| 32 | (say-hi "Joe") |
| 33 | ; "Hi," |
| 34 | ; "Joe" |
| 35 | |
| 36 | .. function:: (defmacro macro-name (args ...) & body) |
| 37 | |
| 38 | ``defmacro`` is to macros as ``defun`` is to functions. When ``macro-name`` |
| 39 | is called, whatever it evaluates to will be compiled. |
| 40 | |
| 41 | Note that internally this compiles a function the same way all other |
| 42 | functions are compiled, meaning you can call **any** lisp function from a |
| 43 | macro definition and it will work as expected. |
| 44 | |
| 45 | .. code-block:: Lisp |
| 46 | |
| 47 | (defun double (n) |
| 48 | (+ n n)) |
swissChili | c3b56c1 | 2021-07-02 20:52:09 -0700 | [diff] [blame] | 49 | |
swissChili | 61ced3b | 2021-07-02 14:52:05 -0700 | [diff] [blame] | 50 | (defmacro call-with-4 (whatever) |
| 51 | (print "this was run at **compile time**") |
| 52 | (print whatever) |
| 53 | ;; ``whatever`` expands to the form passed to this macro, in this case |
| 54 | ;; ``double``. |
| 55 | (list whatever 4)) |
| 56 | |
| 57 | (print (call-with-4 double)) |
| 58 | ; "this was run at **compile time**" |
| 59 | ; 'double |
| 60 | ; 8 |
| 61 | |
swissChili | 5500f70 | 2021-06-30 22:11:17 -0700 | [diff] [blame] | 62 | Functions |
| 63 | --------- |
swissChili | c3b56c1 | 2021-07-02 20:52:09 -0700 | [diff] [blame] | 64 | |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 65 | .. function:: (if condition true-condition [false-condition]) |
| 66 | |
swissChili | c3b56c1 | 2021-07-02 20:52:09 -0700 | [diff] [blame] | 67 | Evaluates ``condition``, if it is truthy (non-``nil``) ``true-condition`` is |
| 68 | evaluated. Otherwise ``false-condition`` is evaluated. If |
| 69 | ``false-condition`` is not provided and ``condition`` is ``nil``, ``if`` |
| 70 | will evaluate to ``nil``. |
| 71 | |
| 72 | .. code-block:: lisp |
| 73 | |
| 74 | (print (if (= 2 3) |
| 75 | "2 = 3" |
| 76 | "2 /= 3")) |
| 77 | ; 2 /= 3 |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 78 | |
| 79 | .. function:: (let1 (variable binding) & body) |
| 80 | |
swissChili | c3b56c1 | 2021-07-02 20:52:09 -0700 | [diff] [blame] | 81 | Evaluates ``binding`` and binds it to ``variable``, then evaluates ``body``. |
| 82 | After ``body`` is evaluated ``variable`` is unbound. |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 83 | |
swissChili | c3b56c1 | 2021-07-02 20:52:09 -0700 | [diff] [blame] | 84 | .. code-block:: lisp |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 85 | |
swissChili | c3b56c1 | 2021-07-02 20:52:09 -0700 | [diff] [blame] | 86 | (let1 (greeting (greet "John")) |
| 87 | (do-something greeting) |
| 88 | (print greeting)) |
| 89 | ; greeting is no longer bound |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 90 | |
| 91 | .. function:: (gc) |
| 92 | |
swissChili | c3b56c1 | 2021-07-02 20:52:09 -0700 | [diff] [blame] | 93 | Force the garbage collector (GC) to run. |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 94 | |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 95 | .. function:: (car pair) |
| 96 | |
swissChili | c3b56c1 | 2021-07-02 20:52:09 -0700 | [diff] [blame] | 97 | Return the first item in ``pair``. |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 98 | |
swissChili | 61ced3b | 2021-07-02 14:52:05 -0700 | [diff] [blame] | 99 | .. code-block:: lisp |
| 100 | |
| 101 | (car (cons 'a 'b)) ;=> 'a |
| 102 | |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 103 | .. function:: (cdr pair) |
| 104 | |
swissChili | c3b56c1 | 2021-07-02 20:52:09 -0700 | [diff] [blame] | 105 | Return the second (last) item in ``pair``. |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 106 | |
swissChili | 61ced3b | 2021-07-02 14:52:05 -0700 | [diff] [blame] | 107 | .. code-block:: lisp |
| 108 | |
| 109 | (cdr (cons 'a 'b)) ;=> 'b |
| 110 | |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 111 | .. function:: (cons a b) |
| 112 | |
swissChili | c3b56c1 | 2021-07-02 20:52:09 -0700 | [diff] [blame] | 113 | Return a cons-pair containing ``a`` and ``b``. |
swissChili | e9fec8b | 2021-06-22 13:59:33 -0700 | [diff] [blame] | 114 | |
| 115 | .. function:: (print val) |
| 116 | |
swissChili | c3b56c1 | 2021-07-02 20:52:09 -0700 | [diff] [blame] | 117 | Print out ``val`` to standard output. This will not be formatted as an |
| 118 | s-expression, but in a manner more similar to the internal representation. |
| 119 | |
swissChili | 5500f70 | 2021-06-30 22:11:17 -0700 | [diff] [blame] | 120 | .. function:: (list & items) |
| 121 | |
| 122 | Returns a cons-list of items. |
| 123 | |
swissChili | 61ced3b | 2021-07-02 14:52:05 -0700 | [diff] [blame] | 124 | .. code-block:: lisp |
| 125 | |
| 126 | (list 1 2 3) |
| 127 | ; is the same as |
| 128 | (cons 1 (cons 2 (cons 3 nil))) |
| 129 | |
swissChili | 5500f70 | 2021-06-30 22:11:17 -0700 | [diff] [blame] | 130 | .. function:: (quote form) |
| 131 | |
| 132 | Returns form without evaluating it. |
| 133 | |
| 134 | .. code-block:: lisp |
swissChili | 5500f70 | 2021-06-30 22:11:17 -0700 | [diff] [blame] | 135 | |
| 136 | '(cons a b) |
| 137 | ; or |
| 138 | (quote cons a b) |
| 139 | ; is the same as |
| 140 | (list 'cons 'a 'b) |