LISP - Built-in Functions
Lisp - A Tour of Key Built-in Functions
Section titled “Lisp - A Tour of Key Built-in Functions”Common Lisp has a famously rich standard library, often described as “batteries included.” Its vast set of built-in functions provides powerful tools for a wide range of tasks. This chapter offers a categorized overview of some of the most essential functions.
Pro Tip: To discover functions in an interactive Lisp environment (like SLIME or Sly), use (apropos "-string-"). For example, (apropos "list") will show all symbols containing “list”. Use (describe 'symbol-name) to get information about a specific function.
Sequence and List Manipulation
Section titled “Sequence and List Manipulation”These functions are the bread and butter of Lisp programming, used for creating and manipulating lists and other sequences (like vectors and strings).
cons: Constructs a newconscell, adding an element to the front of a list.car/cdr: Access the first element / the rest of a list.list: Creates a new list from its arguments.append: Concatenates two or more lists into a new list.reverse: Returns a new, reversed copy of a sequence.length: Returns the number of elements in a sequence.member: Checks if an element is a member of a list (usingeqlby default).mapcar: Applies a function to each element of a list, returning a new list of the results. A functional programming staple.
Numeric Operations
Section titled “Numeric Operations”Functions for performing arithmetic. Most can take multiple arguments.
+,-,*,/: Addition, subtraction, multiplication, division.1+,1-: Increment and decrement by one (e.g.,(1+ 5)is6).mod,rem: Modulo and remainder of a division.max,min: Return the maximum or minimum of their numeric arguments.abs: Returns the absolute value of a number.expt: Raises a number to a power (e.g.,(expt 2 3)is8).
Predicates (Comparison and Type Checking)
Section titled “Predicates (Comparison and Type Checking)”Functions that return t or nil.
=,<,>,<=,>=,/=: Numeric comparisons.eq,eql,equal,equalp: General equality checks with varying strictness.null: Checks if an object is the empty listnil.listp,numberp,stringp,symbolp: Check if an object is of a specific type.atom: Checks if an object is an atom (i.e., not aconscell).
Control Flow
Section titled “Control Flow”Special operators and macros for directing the flow of execution.
if: Basic two-way conditional:(if condition then-form else-form).when,unless: One-way conditionals (execute body only if true/false).cond: Multi-way conditional, like a series of if-else-if checks.case: Efficiently compares a value against several constants (useseql).let,let*: Create local variable bindings.loop: Lisp’s incredibly powerful and versatile iteration macro.dolist,dotimes: Simpler macros for iterating over lists and integers.
Input/Output (I/O)
Section titled “Input/Output (I/O)”Functions for interacting with streams, like the console or files.
print,prin1,princ: Print a Lisp object to a stream with different formatting.format: A powerful and flexible function for producing formatted text (similar toprintfin C).(format t "Hello, ~a!~%" "world").read: Reads one Lisp expression from a stream.with-open-file: A macro that safely opens a file and ensures it’s closed afterward.
Definitions
Section titled “Definitions”Macros used to define top-level constructs in your code.
defun: Defines a named function.defvar,defparameter: Defines a global (or “special”) variable.defmacro: Defines a macro, a powerful tool for extending Lisp’s syntax.defpackage: Defines a new package for code organization.
This list is just the beginning. The strength of Common Lisp lies not just in these functions, but in how they compose together, and how you can extend the language itself with macros to perfectly suit your problem domain.