blob: 1d00ce0e33cfd9b23a6334b72ed2b3722f2f9bef [file] [log] [blame]
swissChilie9fec8b2021-06-22 13:59:33 -07001Lisp Standard Library
2=====================
3
4This provides documentation for every built-in function in the Lisp standard
5library. It is not auto-generated, please update this documentation if you
6change the API in any way.
7
8In general every user-facing API in the standard library should be documented
9here.
10
swissChili61ced3b2021-07-02 14:52:05 -070011- ``(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
15Top-level primitives
16--------------------
17
18These are "functions" that can only appear at the top-level of the program. This
19means they can't be nested in any other expressions.
20
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.
25
26 .. code-block:: lisp
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))
49
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
swissChili5500f702021-06-30 22:11:17 -070062Functions
63---------
swissChilie9fec8b2021-06-22 13:59:33 -070064
65.. function:: (if condition true-condition [false-condition])
66
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, ``if`` will evaluate to ``nil``.
70
71.. function:: (let1 (variable binding) & body)
72
73 Evaluates ``binding`` and binds it to ``variable``, then evaluates ``body``.
74 After ``body`` is evaluated ``variable`` is unbound.
75
76 .. code-block:: lisp
77
78 (let1 (greeting (greet "John"))
79 (do-something greeting)
80 (print greeting))
81 ; greeting is no longer bound
82
83.. function:: (gc)
84
85 Force the garbage collector (GC) to run.
86
swissChilie9fec8b2021-06-22 13:59:33 -070087.. function:: (car pair)
88
89 Return the first item in ``pair``.
90
swissChili61ced3b2021-07-02 14:52:05 -070091 .. code-block:: lisp
92
93 (car (cons 'a 'b)) ;=> 'a
94
swissChilie9fec8b2021-06-22 13:59:33 -070095.. function:: (cdr pair)
96
97 Return the second (last) item in ``pair``.
98
swissChili61ced3b2021-07-02 14:52:05 -070099 .. code-block:: lisp
100
101 (cdr (cons 'a 'b)) ;=> 'b
102
swissChilie9fec8b2021-06-22 13:59:33 -0700103.. function:: (cons a b)
104
105 Return a cons-pair containing ``a`` and ``b``.
106
107.. function:: (print val)
108
109 Print out ``val`` to standard output. This will not be formatted as an
swissChili5500f702021-06-30 22:11:17 -0700110 s-expression, but in a manner more similar to the internal representation.
111
112.. function:: (list & items)
113
114 Returns a cons-list of items.
115
swissChili61ced3b2021-07-02 14:52:05 -0700116 .. code-block:: lisp
117
118 (list 1 2 3)
119 ; is the same as
120 (cons 1 (cons 2 (cons 3 nil)))
121
swissChili5500f702021-06-30 22:11:17 -0700122.. function:: (quote form)
123
124 Returns form without evaluating it.
125
126 .. code-block:: lisp
swissChili5500f702021-06-30 22:11:17 -0700127
128 '(cons a b)
129 ; or
130 (quote cons a b)
131 ; is the same as
132 (list 'cons 'a 'b)