swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 1 | \documentclass[letterpaper,11pt,twocolumn]{article} |
| 2 | \usepackage[top=2cm, bottom=2cm, left=2cm, right=2cm]{geometry}\usepackage[T1]{fontenc} |
| 3 | \usepackage[utf8]{inputenc} |
| 4 | \usepackage{lmodern} |
| 5 | \usepackage{amsmath} |
| 6 | \usepackage{amsfonts} |
| 7 | \usepackage{amssymb} |
| 8 | \usepackage{amsthm} |
| 9 | \usepackage{graphicx} |
| 10 | \usepackage{color} |
| 11 | \usepackage{xcolor} |
| 12 | \usepackage{url} |
| 13 | \usepackage{textcomp} |
| 14 | \usepackage{listings} |
| 15 | \usepackage{glossaries} |
| 16 | \usepackage{parskip} |
| 17 | \usepackage{imakeidx} |
| 18 | \usepackage[normalem]{ulem} |
| 19 | |
| 20 | \title{Bluejay Lisp Reference} |
| 21 | \author{swissChili} |
| 22 | \date{Development Version: \today} |
| 23 | |
| 24 | \newcommand{\startexplanation}{$\triangleright$\hskip1.4ex} |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 25 | \newenvironment{defin}[1] |
| 26 | {#1 |
| 27 | \begin{quote}\startexplanation} |
| 28 | {\end{quote}} |
| 29 | |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 30 | \newcommand{\plus}{\texttt{+}} |
| 31 | \newcommand{\pluses}[1]{\plus{}#1\plus{}} |
| 32 | \newcommand{\earmuff}{\texttt{*}} |
| 33 | \newcommand{\earmuffs}[1]{\earmuff{}#1\earmuff{}} |
| 34 | \newcommand{\func}[1]{\text{$_f$\textbf{#1}}} |
| 35 | \newcommand{\mac}[1]{\text{$_m$\textbf{#1}}} |
| 36 | \newcommand{\reader}[1]{\text{$_r$\textbf{#1}}} |
| 37 | \newcommand{\const}[1]{\text{$_c$\textbf{#1}}} |
| 38 | \newcommand{\var}[1]{\text{$_v$\textbf{#1}}} |
| 39 | \newcommand{\param}[1]{\textit{#1}} |
| 40 | \newcommand{\ret}[1]{\uline{#1}} |
| 41 | \newcommand{\type}[1]{\text{$_t$\textbf{#1}}} |
| 42 | \newcommand{\more}{ \ldots} |
| 43 | \newcommand{\T}{\texttt{t}} |
| 44 | \newcommand{\nil}{\texttt{nil}} |
| 45 | \newcommand{\default}[1]{\text{\textsubscript{ |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 46 | \setlength{\fboxsep}{1pt}\setlength{\fboxrule}{0.2bp}% |
| 47 | \fbox{#1}}}} |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 48 | \newcommand{\opt}[2]{\text{$[$}\param{#1}\default{#2}\text{$]$}} |
| 49 | \newcommand{\mut}[1]{\text{$\widetilde{#1}$}} |
swissChili | a89ee44 | 2021-08-04 20:54:51 -0700 | [diff] [blame] | 50 | \newcommand{\mighteval}[1]{\text{$\widehat{#1}$}} |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 51 | \newcommand{\super}[1]{\text{$ ^{#1} $}} |
| 52 | |
| 53 | \newcommand{\optlist}[1]{\text{\( |
| 54 | \left\{ |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 55 | \begin{array}{l} |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 56 | #1 |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 57 | \end{array} |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 58 | \right\} |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 59 | \)}} |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 60 | |
| 61 | \makeindex |
| 62 | \makeglossaries |
| 63 | |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 64 | \newglossaryentry{closure}{name={closure},description={A |
| 65 | \type{function-object} that captures certain variables from its |
| 66 | defining context}} |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 67 | |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 68 | \newglossaryentry{lexical-scope}{name={lexical scope},description={A |
| 69 | method of scoping where the values in scope at a given time are |
| 70 | determined by the static content of the source program, not by the |
| 71 | state of the callstack or other execution details. This allows for |
| 72 | example closures to \gls{closure}s to capture some of the state of |
| 73 | their defining context}} |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 74 | |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 75 | \newglossaryentry{truthy}{name={truthy},description={A value that is |
| 76 | not \nil}} |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 77 | |
| 78 | \begin{document} |
| 79 | |
| 80 | \maketitle |
| 81 | \tableofcontents |
| 82 | |
| 83 | \section{Introduction} |
| 84 | |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 85 | This document provides a brief reference to the Bluejay Lisp language |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 86 | and standard library. It documents every currently included function, |
| 87 | macro, reader macro, and special form. |
| 88 | |
| 89 | \subsection{Running Bluejay Lisp} |
| 90 | |
| 91 | Assuming you have compiled Bluejay Lisp, you should have a |
| 92 | \texttt{lisp} executable in your \texttt{src/lisp} directory. Invoking |
| 93 | \texttt{lisp} without arguments will drop you into an interactive |
| 94 | read-evaluate-print loop, or REPL.\@ Here you can type Lisp code one |
| 95 | line at a time---the result will be immediately displayed. If you wish |
| 96 | to run a specific Lisp file, you may pass its path as the first |
| 97 | argument to \texttt{lisp}, e.g. \texttt{lisp hello-world.lisp}. |
| 98 | |
| 99 | Note that in order to load the standard library, the compiler must |
| 100 | know where Bluejay Lisp libraries are stored. When loading a library, |
| 101 | the compiler checks the \texttt{LISP\_LIBRARY\_PATH} environment |
| 102 | variable. If it is blank, it assumes the library path is |
| 103 | \texttt{/lib/lisp}. To use the standard library included with Bluejay, |
| 104 | set the environment variable to the path of the included |
| 105 | \texttt{lib/lisp} directory. |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 106 | |
| 107 | \subsection{Typography} |
| 108 | |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 109 | The following text styles and symbols are used within this document to |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 110 | indicate particular types, values, or meanings: |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 111 | |
swissChili | a89ee44 | 2021-08-04 20:54:51 -0700 | [diff] [blame] | 112 | \begin{tabular}[t]{p{0.2\linewidth} p{0.64\linewidth}} |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 113 | \func{cons} & A function \param{cons}. \\ |
| 114 | \const{\plus{}foo\plus} & A constant \param{\plus{}foo\plus}. \\ |
| 115 | \var{\earmuff{}bar\earmuff} & A global variable \param{\earmuff{}bar\earmuff}. \\ |
| 116 | \reader{baz} & A reader macro \param{baz}. \\ |
| 117 | \mac{quux} & A macro \param{quux}. \\ |
| 118 | \param{parameter} & A function argument \param{parameter} \\ |
| 119 | \opt{var}{123} & An optional function argument \param{var} with the default value \param{123}. \\ |
| 120 | \param{args}\more & \param{args} represents the rest of the items in the list. \\ |
| 121 | \mut{\param{mut}} & A function argument \param{mut} that might be mutated. \\ |
| 122 | \mighteval{\param{maybe}} & \param{maybe} may or may not be evaluated. \\ |
| 123 | \ret{value} & Indicates that a form will evaluate to \param{value}. \\ |
| 124 | \type{integer} & The type \param{integer}. \\ |
| 125 | \optlist{\text{a}\\\text{b}} & One of \param{a} or \param{b}. |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 126 | \end{tabular} |
| 127 | |
| 128 | \section{Primitives} |
| 129 | |
| 130 | \subsection{Type Predicates} |
| 131 | |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 132 | \begin{defin}{ |
| 133 | (\optlist{ |
| 134 | \func{nilp} \text{ or } \func{not} \\ |
| 135 | \func{closurep} \text{ or } \func{functionp} \\ |
| 136 | \func{integerp} \\ |
| 137 | \func{consp} \\ |
| 138 | \func{symbolp} |
| 139 | } \param{value})\index{nilp}\index{not} |
| 140 | } |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 141 | \ret{\T} if \param{value} is of the specified type, \ret{\nil} |
| 142 | otherwise. |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 143 | \end{defin} |
| 144 | |
| 145 | \begin{defin}{(\func{listp} \param{value})} |
| 146 | \ret{\T} if \param{value} is a well-formed \type{cons} list, |
| 147 | i.e.\ if (\func{consp} \param{value}) and the (\func{cdr} |
| 148 | \param{value}) is a well-formed \type{cons} list or \nil. |
| 149 | \end{defin} |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 150 | |
| 151 | \subsection{Definitions and Variables} |
| 152 | |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 153 | \begin{defin}{ |
| 154 | (\mac{defun} \param{name} (\param{args}\more) \param{body}\more)\index{defun} \\ |
| 155 | (\mac{defmacro} \param{name} (\param{args}\more) \param{body}\more)\index{defmacro} |
| 156 | } |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 157 | Define a function or macro respectively, taking \param{args} |
| 158 | arguments and evaluating \param{body} in turn, finally evaluating to |
| 159 | \ret{the final entry in \param{body}} or \ret{\nil} if \param{body} |
| 160 | is empty. |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 161 | \end{defin} |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 162 | |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 163 | \begin{defin}{ |
| 164 | (\mac{let1} (\param{variable} \param{form}) \param{body}\more)\index{let1} \\ |
| 165 | (\mac{let} ((\param{variable} \param{form})\more) \param{body}\more)\index{let} |
| 166 | } |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 167 | First evaluate \param{form}, binding it to \param{variable}. Then |
| 168 | evaluate \param{body}, finally evaluating to \ret{the final entry in |
| 169 | \param{body}} or \ret{\nil} if \param{body} is |
| 170 | empty. \param{variable} is no longer in scope after this form ends. |
swissChili | fc5c941 | 2021-08-08 19:08:26 -0700 | [diff] [blame] | 171 | \mac{let} is similar to \mac{let*} in other lisps, later variables |
| 172 | can reference previous ones. |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 173 | \end{defin} |
swissChili | fc5c941 | 2021-08-08 19:08:26 -0700 | [diff] [blame] | 174 | |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 175 | \begin{defin}{ |
| 176 | (\mac{flet1} (\param{name} (\param{args}\more) \param{body}\more)) \\ |
| 177 | (\mac{flet} ((\param{name} (\param{args}\more)\more) \param{body}\more)) |
| 178 | \index{flet}\index{flet1} |
| 179 | } |
swissChili | fc5c941 | 2021-08-08 19:08:26 -0700 | [diff] [blame] | 180 | Like \mac{let} and \mac{let1} but creates a lambda. Unlike other |
| 181 | lisps the defined function remains in the variable namespace as a |
| 182 | \type{function-object}. |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 183 | \end{defin} |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 184 | |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 185 | \begin{defin}{ |
| 186 | \reader{\textquotesingle}\param{value} \\ (\mac{quote} |
| 187 | \param{value})\index{quote} } \ret{Return \param{value}} as an |
| 188 | expression, without evaluating it. |
| 189 | \end{defin} |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 190 | |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 191 | \begin{defin}{ |
| 192 | \reader{\`}\param{value} \\ |
| 193 | (\mac{backquote} \param{value})\index{backquote}\index{\`} |
| 194 | } |
| 195 | Return \ret{\param{value}} unevaluated, except for any \mac{unquote} |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 196 | and \mac{unquote-splice} forms (and their reader-macro equivalents). |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 197 | \end{defin} |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 198 | |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 199 | \begin{defin}{ |
| 200 | \reader{,}\param{value} \\ |
| 201 | (\mac{unquote} \param{value}) \\ |
| 202 | \reader{,\texttt{@}}\param{value} \\ |
| 203 | (\mac{unquote-splice} \param{value}) |
| 204 | \index{unquote}\index{unquote-splice} |
| 205 | \index{,}\index{,@} |
| 206 | } |
swissChili | 3f7f584 | 2021-08-08 17:13:45 -0700 | [diff] [blame] | 207 | In the context of a \mac{backquote}, evaluate \param{value} instead of |
| 208 | using it as-is. \mac{unquote-splice} and \reader{,\texttt{@}} take a |
| 209 | list as their argument, and ``splice'' each element in to |
| 210 | the \mac{backquote} list so that each element of that argument corresponds |
| 211 | to an element of the \mac{backquote} list. |
| 212 | The other two macros insert their argument as a single element in the list. |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 213 | \end{defin} |
swissChili | 3f7f584 | 2021-08-08 17:13:45 -0700 | [diff] [blame] | 214 | |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 215 | \subsection{Control Flow} |
| 216 | |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 217 | \begin{defin}{ |
| 218 | (\mac{if} \param{predicate} \param{then} \opt{otherwise}{nil})\index{if} \\ |
| 219 | (\mac{when} \param{predicate} \param{then\more})\index{when} \\ |
| 220 | (\mac{unless} \param{predicate} \param{otherwise\more})\index{unless} |
| 221 | } |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 222 | First evaluate \param{predicate}. If it is \gls{truthy} evaluate and |
| 223 | return \ret{\param{then}}, otherwise \ret{\param{otherwise}}. If |
| 224 | either is not provided return \ret{\nil}. |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 225 | \end{defin} |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 226 | |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 227 | \begin{defin}{ |
| 228 | (\mac{progn} \opt{forms\more}{nil})\index{progn} |
| 229 | } |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 230 | Evaluate \param{forms} from first to last, finally returning |
| 231 | \ret{the last form}. |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 232 | \end{defin} |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 233 | |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 234 | \begin{defin}{ |
| 235 | (\mac{and} \param{first} \param{\mighteval{rest}}\more)\index{and} \\ |
| 236 | (\mac{or} \param{first} \param{\mighteval{rest}}\more)\index{or} |
| 237 | } |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 238 | Short circuiting $\land$ and $\lor$, respectively. Return the first |
| 239 | value that is \nil{} or truthy, respectively, or the last value if |
| 240 | all are truthy/\nil{}. |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 241 | \end{defin} |
swissChili | a89ee44 | 2021-08-04 20:54:51 -0700 | [diff] [blame] | 242 | |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 243 | \section{Numbers} |
| 244 | |
| 245 | \subsection{Integers} |
| 246 | |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 247 | \begin{defin}{ |
| 248 | (\optlist{ |
| 249 | \func{$+$} \\ |
| 250 | \func{$-$} \\ |
| 251 | \func{$*$} \\ |
| 252 | \func{$/$} |
| 253 | } \param{a b}) |
| 254 | } |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 255 | The \ret{sum, difference, product, or quotient} of \param{a} and |
| 256 | \param{b} as an \type{integer}. |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 257 | \end{defin} |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 258 | |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 259 | \begin{defin}{ |
| 260 | (\func{=} \param{a b}) |
| 261 | } |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 262 | \ret{\T} if \param{a} and \param{b} hold the same \type{integer} |
| 263 | value, \ret{\nil} otherwise. |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 264 | \end{defin} |
| 265 | |
| 266 | \begin{defin}{(\optlist{\func{$<$} \\ \func{$>$}} \param{a} \param{b})} |
| 267 | \ret{\T} if \param{a} is less than or greater than \param{b}, |
| 268 | respectively; \ret{\nil} otherwise. |
| 269 | \end{defin} |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 270 | |
| 271 | \section{Function Manipulation} |
| 272 | |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 273 | \begin{defin}{ |
| 274 | (\func{funcall} \param{args}\more)\index{funcall} \\ |
| 275 | (\func{apply} \param{function} \param{args})\index{apply} |
| 276 | } |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 277 | Call the \type{closure} or \type{function-object} \param{function} |
| 278 | with \param{args} and evaluate to \ret{its result}. An error occurs |
| 279 | if \param{args} are not acceptable. |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 280 | \end{defin} |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 281 | |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 282 | \begin{defin}{ |
| 283 | (\func{recurse} \param{args}\more)\index{recurse} } In a lambda |
| 284 | definition, call the current lambda with |
| 285 | \param{args}. \func{recurse} can also be used as an argument to |
| 286 | \func{function} and \reader{\#\textquotesingle} to refer to the |
| 287 | current lambda. |
| 288 | \end{defin} |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 289 | |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 290 | \begin{defin}{ |
| 291 | (\mac{function} \param{function-name})\index{function} \\ |
| 292 | \reader{\#\textquotesingle}\param{function-name}\index{\#\textquotesingle} |
| 293 | } |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 294 | Create a \ret{\type{function-object} from an existing function or |
| 295 | macro}. \param{function} must be a symbol literal at compile time. |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 296 | \end{defin} |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 297 | |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 298 | \begin{defin}{ |
| 299 | (\func{lambda} (\param{args}\more) \param{body}\more)\index{lambda} |
| 300 | } |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 301 | Create a \ret{lexically-scoped \type{\gls{closure}}} taking |
| 302 | \param{args} and evaluating to \param{body}. |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 303 | \end{defin} |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 304 | |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 305 | \begin{defin}{ |
| 306 | (\func{eval} \param{form})\index{eval} |
| 307 | } |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 308 | Evaluate and return \ret{\param{form}} in the current global |
| 309 | environment. The evaluated form does not use \gls{lexical-scope}. |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 310 | \end{defin} |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 311 | |
| 312 | \section{Lists} |
| 313 | |
| 314 | \subsection{Creating Lists} |
| 315 | |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 316 | \begin{defin}{ |
| 317 | (\func{cons} \param{a} \param{b})\index{cons} |
| 318 | } |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 319 | Join \param{a} and \param{b} into a \ret{\type{cons} pair}. |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 320 | \end{defin} |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 321 | |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 322 | \begin{defin}{ |
| 323 | (\func{list} \param{values}\more)\index{list} |
| 324 | } |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 325 | Construct a \ret{\type{cons}-list of \param{values}}. |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 326 | \end{defin} |
| 327 | |
| 328 | \begin{defin}{(\func{append} \param{first} \param{rest})} |
| 329 | If \param{first} is a list, return \ret{a list with all the elements |
| 330 | of \param{first} followed by the elements of |
| 331 | \param{rest}}. Otherwise, return \ret{(\func{cons} first rest)}. |
| 332 | \end{defin} |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 333 | |
| 334 | \subsection{Deconstructing Lists} |
| 335 | |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 336 | \begin{defin}{ |
| 337 | (\func{length} \param{list})\index{list} |
| 338 | } |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 339 | Return the \ret{length of \param{list}} if it is a \type{cons}-list, |
| 340 | \nil{} otherwise. |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 341 | \end{defin} |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 342 | |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 343 | \begin{defin}{ |
| 344 | (\func{car} \param{pair})\index{car} \\ |
| 345 | (\func{cdr} \param{pair})\index{cdr} |
| 346 | } |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 347 | Return the \ret{first or second item} of \type{cons} \param{pair}, |
| 348 | respectively. |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 349 | \end{defin} |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 350 | |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 351 | \begin{defin}{ |
| 352 | (\optlist{ |
| 353 | \func{caar}\\ |
| 354 | \func{cadr}\\ |
| 355 | \func{caddr}\\ |
| 356 | \func{cadar}\\ |
| 357 | \func{caddar} |
| 358 | } \param{val}) |
| 359 | } |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 360 | Behave like a combination of \func{car} and \func{cdr} would. |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 361 | \end{defin} |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 362 | |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 363 | \begin{defin}{ |
| 364 | (\func{elt} \param{list} \param{n})\index{elt} |
| 365 | } |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 366 | Return the \ret{\param{n}\super{th} element of \param{list}}, |
| 367 | starting from 0, or \ret{\nil} if \param{n} $ \ge $ (\func{length} |
| 368 | \param{list}) or \param{list} is not a \type{cons}-list or \param{n} |
| 369 | is not an \type{integer}. |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 370 | \end{defin} |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 371 | |
| 372 | \subsection{Operating on Lists} |
| 373 | |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 374 | \begin{defin}{ |
| 375 | (\func{mapcar} \param{fun} \param{list})\index{mapcar} |
| 376 | } |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 377 | Apply \param{fun} to each element of \param{list}, returning a |
| 378 | \ret{new \type{cons}-list} containing the results of the respective |
| 379 | applications of \param{fun}. |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 380 | \end{defin} |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 381 | |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 382 | \begin{defin}{ |
| 383 | (\optlist{\func{remove-if}\\\func{remove-if-not}} |
| 384 | \param{predicate} \param{list})\index{remove-if}\index{remove-if-not} |
| 385 | } |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 386 | Return a \ret{new \type{cons}-list} of all the items of \param{list} |
| 387 | that either do not or do satisfy \param{predicate}, respectively. |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 388 | \end{defin} |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 389 | |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 390 | \begin{defin}{ |
| 391 | (\func{reduce} \param{fun} \param{list} \opt{initial-value}{\nil}) |
| 392 | } |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 393 | Apply \param{fun} to two arguments at a time, starting with |
| 394 | \param{initial-value} and (\func{car} \param{list}) and continuing |
| 395 | with the result of the previous invocation and the successive |
| 396 | element of \param{list}. Return \ret{the result of the final |
| 397 | invocation}, or \ret{\param{initial-value}} if \param{list} is |
| 398 | empty. |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 399 | \end{defin} |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 400 | |
swissChili | 8b5ec7a | 2022-08-05 22:26:17 -0700 | [diff] [blame] | 401 | \section{Strings \& Symbols} |
| 402 | |
| 403 | \begin{defin}{(\optlist{\func{string->symbol}\\\func{symbol->string}} \param{value})}% |
| 404 | \index{string->symbol}\index{symbol->string} Return a new |
| 405 | \ret{symbol} or \ret{string} representing \param{value}, |
| 406 | respectively; or \ret{\nil} if \param{value} is not of the specified |
| 407 | type. |
| 408 | \end{defin} |
| 409 | |
| 410 | \begin{defin}{(\func{concat} \param{strings} \more)}\index{concat} |
| 411 | Return \ret{a new string} containing the content of each of the |
| 412 | strings \param{strings} one after the other. |
| 413 | \end{defin} |
| 414 | |
| 415 | \begin{defin}{(\func{gensym})}\index{gensym} |
| 416 | Return a \ret{new, unique symbol}. Useful in macros. |
| 417 | \end{defin} |
| 418 | |
| 419 | \section{Object Oriented Programming} |
| 420 | |
| 421 | \begin{defin}{(\func{make-class} \param{type} \param{num-members})}\index{make-class} |
| 422 | Create a \ret{new class instance} of type \param{type} with slots |
| 423 | for \param{num-members} members. This is a low-level function that |
| 424 | should not be often used. |
| 425 | \end{defin} |
| 426 | |
| 427 | \begin{defin}{(\func{class-member} \param{class} \param{index})}\index{class-member} |
| 428 | Get the \ret{member variable at index \param{index}} in the class |
| 429 | instance \param{class}, or \ret{\nil} if \param{index} out of range. |
| 430 | \end{defin} |
| 431 | |
| 432 | \begin{defin}{(\func{set-class-member} \param{class} \param{index} \param{value})}\index{set-class-member} |
| 433 | Set the member variable at index \param{index} in the class instance |
| 434 | \param{class} to \param{value}. |
| 435 | \end{defin} |
| 436 | |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 437 | \section{Input \& Output} |
| 438 | |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 439 | \begin{defin}{ |
| 440 | (\func{print} \param{value})\index{print} |
| 441 | } |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 442 | Print \param{value} to standard output. Return \ret{\nil}. |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 443 | \end{defin} |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 444 | |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 445 | \begin{defin}{ |
| 446 | (\func{read})\index{read} |
| 447 | } |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 448 | Read and return an \ret{S-expression} from standard input |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 449 | \end{defin} |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 450 | |
| 451 | \subsection{Loading Programs} |
| 452 | |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 453 | \begin{defin}{ |
| 454 | \const{\pluses{current-file}}\index{\pluses{current-file}} } |
| 455 | \ret{A string} containing the path of the file currently being |
| 456 | compiled, or \ret{\nil{}} if not compiling a file (e.g.\ when |
| 457 | compiling a string). |
| 458 | \end{defin} |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 459 | |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 460 | \begin{defin}{ |
| 461 | (\func{load} \param{lisp-file})\index{load} |
| 462 | } |
swissChili | 36f2c69 | 2021-08-08 14:31:44 -0700 | [diff] [blame] | 463 | Load and evaluate \type{string} \param{lisp-file} as a local path |
| 464 | relative to the current file, or the current working directory if |
| 465 | not compiling a file. Return \ret{\nil}. |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 466 | \end{defin} |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 467 | |
swissChili | 04d9416 | 2022-07-30 21:46:49 -0700 | [diff] [blame] | 468 | \section{Inspecting \& Modifying the Environment} |
| 469 | |
| 470 | \subsection{Managing Memory} |
| 471 | |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 472 | \begin{defin}{ |
| 473 | (\func{gc})\index{gc} |
| 474 | } |
swissChili | 04d9416 | 2022-07-30 21:46:49 -0700 | [diff] [blame] | 475 | Run the garbage collector. |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 476 | \end{defin} |
swissChili | 04d9416 | 2022-07-30 21:46:49 -0700 | [diff] [blame] | 477 | |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 478 | \begin{defin}{ |
| 479 | (\func{gc-stats})\index{gc-stats} |
| 480 | } |
swissChili | 04d9416 | 2022-07-30 21:46:49 -0700 | [diff] [blame] | 481 | Return some |
| 482 | statistics about the garbage collector, in the form of a list: |
| 483 | \ret{(\param{total-allocs} \param{gc-runs})}, where |
| 484 | \param{total-allocs} is the current number of active allocations, |
| 485 | and \param{gc-runs} is the total number of times the garbage |
| 486 | collector has been run. |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 487 | \end{defin} |
swissChili | 04d9416 | 2022-07-30 21:46:49 -0700 | [diff] [blame] | 488 | |
| 489 | \subsection{Introspection} |
| 490 | |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 491 | \begin{defin}{ \const{\pluses{current-env}} } |
| 492 | An opaque object representing the current environment. |
| 493 | \end{defin} |
swissChili | 04d9416 | 2022-07-30 21:46:49 -0700 | [diff] [blame] | 494 | |
swissChili | 3e57d7c | 2022-08-01 21:40:25 -0700 | [diff] [blame] | 495 | \begin{defin}{ (\func{env-functions} \param{current-env}) } |
| 496 | Returns a list of symbols, each of which is the name of a function |
| 497 | defined in the environment specified by \param{current-env}. |
| 498 | \end{defin} |
swissChili | 04d9416 | 2022-07-30 21:46:49 -0700 | [diff] [blame] | 499 | |
swissChili | 8a4b4ed | 2021-08-03 19:27:32 -0700 | [diff] [blame] | 500 | \printindex |
| 501 | |
| 502 | \printglossaries |
| 503 | |
| 504 | \end{document} |