Fading Coder

One Final Commit for the Last Sprint

Home > Tools > Content

Common Lisp Core Language Reference

Tools May 9 3
Command Description Example
T Represents logical true. (eq T T) evaluates to T
NIL Represents logical false, also the empty list. (eq NIL NIL) evaluates to T
+ Adds numbers. Can take multiple arguments. (+ 2 3 4) evaluates to 9
- Subtracts or negates numbers. (- 9 5) evaluates to 4
* Multiplies numbers. Can take multiple arguments. (* 2 3 4) evaluates to 24
/ Divides numbers. (/ 10 2) evaluates to 5
1+ Increments its argument by 1. Equivalent to (+ n 1). (1+ 5) evaluates to 6
1- Decrements its argument by 1. Equivalent to (- n 1). (1- 5) evaluates to 4
> Checks if the first argument is greater than the second. (> 5 3) evaluates to T
< Checks if the first argument is less than the second. (< 3 5) evaluates to T
CHAR< Compares characters for less-than. (char< #\a #\b) evaluates to T
CHAR> Compares characters for greater-than. (char> #\b #\a) evaluates to T
Global Variable Naming Conventionally, global variables are enclosed in asterisks. (setf *print-array* T)
ZEROP Returns true if the argument is zero. (zerop 0) evaluates to T
CHARACTERP Returns true if the argument is a character. (characterp #\x) evaluates to T
/= Checks if arguments are not numerically equal. (/= 1 2) evaluates to T
= Numerically compares arguments for equality (for numbers only). (= 4 4.0) evaluates to T
EQ Tests for identity (same object in memory). Not for numbers. (eq (cons 'a 'b) (cons 'a 'b)) is NIL; (eq 'a 'a) is T
EQL Similar to EQ, but also compares numbers with the same value and type. (eql 5 5) is T; (eql 5 5.0) is NIL
EQUAL Tests for structural equality of objects, case-sensitive for strings. (equal (list 1 (cons 2 3)) (list 1 (cons 2 (+ 2 1)))) is T
EQUALP Tests for structural equality, case-insensitive for strings, handles numeric types flexibly. (equalp "Hello" "hello") is T
AND Logical AND. Evaluates arguments until one is NIL or all are T. (and (> 5 3) (< 10 20)) is T
OR Logical OR. Evaluates arguments until one is T or all are NIL. (or (zerop 1) (oddp 3)) is T
NOT Logical NOT. Returns T for NIL, NIL for anything else. (not T) is NIL
ODDP Returns true if the number is odd. (oddp 7) evaluates to T
EVENP Returns true if the number is even. (evenp 6) evaluates to T
QUOTE (`) Prevents evaluation of its argument. '(+ 1 2) evaluates to (+ 1 2)
CONS Constructs a cons cell, forming the basis of lists. (cons 'a '(b c d)) evaluates to (A B C D)
CONSP Checks if an object is a cons cell. (consp '(a b)) is T
LIST Creates a list from its arguments. (list 1 2 3) evaluates to (1 2 3)
GETF Retrieves a property value from a property list (represented as a list). (getf (list :a 1 :b 2) :a) evaluates to 1
CAR Returns the first element of a list (or cons cell). (car '(a b c)) evaluates to A
CDR Returns rest of the list (everything after the first element). (cdr '(a b c)) evaluates to (B C)
NTH Accesses the Nth element of a list (0-indexed). (nth 2 '(a b c d)) evaluates to C
NTHCDR Returns the Nth cdr of a list. (nthcdr 2 '(a b c d e)) evaluates to (C D E)
LAST Returns the last cons cell of a list. (last '(a b c)) evaluates to (C)
CAR (LAST ...) Retrieves the last element of a list. (car (last '(a b c))) evaluates to C
SECOND Returns the second element of a list. (second '(a b c)) evaluates to B
CADR Equivalent to (car (cdr x)), returns the second element. (cadr '(a b c)) evaluates to B
CADDR Equivalent to (car (cdr (cdr x))), returns the third element. (caddr '(a b c)) evaluates to C
LISTP Checks if an object is a list. (listp '(1 2 3)) is T
NULL Checks if an object is NIL (empty list or false). (null '()) is T
IF Conditional expression: (if test then-form else-form). (if (> 5 3) "yes" "no") evaluates to "yes"
DEFUN Defines a global function. (defun add-two (x) (+ x 2))
&rest Function parameter qualifier: collects remaining arguments into a list. (defun sum-all (&rest nums) (apply #'+ nums))
&optional Function parameter qualifier: declares optional parameters with default values. (defun greet (name &optional (salutation "Hello")) (format nil "~A, ~A!" salutation name))
&key Function parameter qualifier: declares keyword arguments. (defun print-info (&key (name "Anon") (age 0)) (format nil "Name: ~A, Age: ~A" name age))
FORMAT Formats and prints output to a stream. T for standard output. (format T "~A + ~A = ~A.~%" 2 3 (+ 2 3)) prints "2 + 3 = 5."
~A FORMAT directive: prints an argument in PRINC style. See FORMAT example.
~% FORMAT directive: prints a newline. See FORMAT example.
READ Reads an S-expression from standard input or a stream. (let ((val (read))) val)
LET Defines local variables for a block of code. Bindings are parallel. (let ((x 1) (y 2)) (+ x y)) evaluates to 3
DEFPARAMETER Defines a global variable, declaring it special. (defparameter *app-name* "My App")
DEFCONSTANT Defines a global constant. (defconstant +max-items+ 100)
BOUNDP Checks if a symbol has a value bound to it. (boundp '*app-name*) is T
DEFVAR Defines a global variable, potentially with an initial value. (defvar *database-connection* NIL)
SETQ Assigns a value to a variable (variable name is quoted implicitly). (setq x 10)
SETF A generalized assignment operator, can assign to many places. (setf (nth 0 my-list) 'new-val)
SET Assigns a value to the value cell of a symbol. (set 'my-symbol '(1 2 3))
INCF Increments a place by a specified amount (default 1). (let ((x 5)) (incf x 2) x) evaluates to 7
DECF Decrements a place by a specified amount (default 1). (let ((x 5)) (decf x 2) x) evaluates to 3
REMOVE Returns a new list with specified elements removed, non-destructive. (remove 'a '(c a r a t)) evaluates to (C R T)
DELETE Removes specified elements from a list, destructive (modifies original). (let ((lst '(a r a b i a))) (delete 'a lst) lst) might be (R B I A) or (A R B I) depending on implementation
DO A general looping construct with initialization, termination, and step forms. (do ((i 1 (1+ i))) ((> i 5) 'done) (format T "~A " i)) prints 1 2 3 4 5
DOTIMES Iterates a fixed number of times. (dotimes (x 3) (princ x)) prints 012
LOOP A powerful iterative macro. (loop for i from 1 to 5 collect i) evaluates to (1 2 3 4 5)
PROGN Evaluates forms sequentially and returns the value of the last form. (progn (format T "First.") (format T "Second.") 10) prints First.Second. and returns 10
DOLIST Iterates over elements of a list. (dolist (item '(a b c)) (princ item)) prints abc
FUNCTION (#') Returns the function object associated with a symbol or lambda. (funcall #'list 1 2)
APPLY Calls a function with its arguments provided in a list. (apply #'+ '(1 2 3)) evaluates to 6
FUNCALL Calls a function with its arguments provided individually. (funcall #'+ 1 2 3) evaluates to 6
LAMBDA Defines an anonymous function. (funcall #'(lambda (x) (+ x 100)) 5) evaluates to 105
TYPEP Checks if an object is of a specific type. (typep 27 'integer) is T
COPY-LIST Returns a shallow copy of a list. (copy-list '(1 2 3)) evaluates to (1 2 3)
ATOM Returns true if the object is an atom (not a cons cell). (atom 'symbol) is T; (atom '(1 2)) is NIL
APPEND Concatenates lists, creating a new list. (append '(a b) '(c d)) evaluates to (A B C D)
NCONC Destructively concatenates lists by modifying cdr pointers. (let ((l1 (list 1 2)) (l2 (list 3 4))) (nconc l1 l2) l1) evaluates to (1 2 3 4)
COPY-SEQ Returns a copy of a sequence (list, vector, string). (copy-seq "hello") evaluates to "hello"
ELT Accesses an element of any sequence by index. (elt "hello" 1) evaluates to #\e
POSITION Returns the index of the first occurrence of an item in a sequence. (position #\a "banana") evaluates to 1
POSITION-IF Returns the index of the first element satisfying a predicate. (position-if #'oddp '(2 4 1 3)) evaluates to 2
FIND Returns the first item equal to the specified item in a sequence. (find #\a "cat") evaluates to #\a
FIND-IF Returns the first item satisfying a predicate in a sequence. (find-if #'characterp "hello") evaluates to #\h
REMOVE-DUPLICATES Returns a new sequence with duplicate elements removed. (remove-duplicates '(1 2 1 3 2)) evaluates to (1 2 3)
REDUCE Combines elements of a sequence using a binary function. (reduce #'+ '(1 2 3 4)) evaluates to 10
PUSH Adds an element to the front of a list, modifying the list. (let ((x '(b c))) (push 'a x) x) evaluates to (A B C)
POP Removes and returns the first element of a list, modifying the list. (let ((x '(a b c))) (pop x) x) evaluates to (B C)
ASSOC Searches an association list for an entry whose car matches a key. (assoc 'a '((a 1) (b 2))) evaluates to (A 1)
TAILP Checks if the first list is a tail of the second list. (tailp '(b c) '(a b c)) is T
ADJOIN Adds an element to a list if not already a member, non-destructive. (adjoin 'z '(a b c)) evaluates to (Z A B C)
UNION Computes the union of two lists. (union '(a b) '(b c)) evaluates to (A B C)
INTERSECTION Computes the intersection of two lists. (intersection '(a b c) '(b c d)) evaluates to (B C)
SET-DIFFERENCE Computes the set difference (elements in first list not in second). (set-difference '(a b c d e) '(b e)) evaluates to (A C D)
LENGTH Returns the number of elements in a sequence. (length '(a b c)) evaluates to 3
SUBSEQ Extracts a subsequence from a sequence. (subseq '(a b c d) 1 3) evaluates to (B C)
REVERSE Returns a new sequence with elements in reverse order. (reverse '(1 2 3)) evaluates to (3 2 1)
NREVERSE Reverses a list destructively. (let ((l '(1 2 3))) (nreverse l) l) evaluates to (3 2 1)
EVERY Checks if a predicate is true for every element in a sequence. (every #'oddp '(1 3 5)) is T
SOME Checks if a predicate is true for at least one element in a sequence. (some #'evenp '(1 2 3)) is T
MAPCAR Applies a function to elements of one or more lists, collecting results into a new list. (mapcar #'(lambda (x) (+ x 10)) '(1 2 3)) evaluates to (11 12 13)
MAPCAN Similar to MAPCAR, but NCONCs the results. (mapcan #'(lambda (x) (if (evenp x) (list x) nil)) '(1 2 3 4)) evaluates to (2 4)
MAPC Applies a function to elements of one or more lists, returning the original list. (mapc #'print '(a b)) prints A B and returns (A B)
MAP Applies a function to elements of one or more sequences, returning a new sequence of a specified type. (map 'list #'(lambda (x) (+ x 10)) '(1 2 3)) evaluates to (11 12 13)
MAP-INTO Applies a function, storing results destructively into a pre-existing sequence. (let ((v (vector 1 2))) (map-into v #'1+ v) v) evaluates to #(2 3)
MAPLIST Applies a function to successive cdrs of a list. (maplist #'(lambda (x) x) '(a b c)) evaluates to ((A B C) (B C) (C))
MAKE-ARRAY Creates an array with specified dimensions. (make-array '(2 3) :initial-element NIL) creates a 2x3 array
AREF Accesses an element of an array by its indices. (let ((arr (make-array '(1 1)))) (setf (aref arr 0 0) 'val) (aref arr 0 0)) evaluates to VAL
#nA Syntax Represents a literal constant array. n is the number of dimensions. #2a((1 2) (3 4)) is a 2x2 aray
VECTOR Creates a simple one-dimensional array (vector). (vector 1 2 3) evaluates to #(1 2 3)
VECTOR-PUSH Pushes an element onto the end of an adjustable vector. (let ((v (make-array 5 :fill-pointer 0 :adjustable T))) (vector-push 'a v) v) evaluates to #(A)
VECTOR-POP Pops an element from the end of an adjustable vector. (let ((v (vector 'a 'b))) (setf (fill-pointer v) 2) (vector-pop v)) evaluates to B
SVREF Accesses an element of a simple vector by index. (svref #(10 20) 0) evaluates to 10
WRITE-TO-STRING Converts an object to its string representation. (write-to-string 123) evaluates to "123"
SORT Sorts a sequence using a predicate. Destructive. (sort (copy-seq "example") #'char<) evaluates to "aeelmpx"
CHAR Accesses a character in a string by index. (char "hello" 0) evaluates to #\h
CONCATENATE Creates a new sequence by concatenating other sequences. (concatenate 'string "foo" "bar") evaluates to "foobar"
MAKE-HASH-TABLE Creates a hash table. (make-hash-table)
GETHASH Retrieves a value associated with a key in a hash table. (let ((ht (make-hash-table))) (setf (gethash 'color ht) 'red) (gethash 'color ht)) evaluates to RED
REMHASH Removes an entry from a hash table. (let ((ht (make-hash-table))) (setf (gethash 'color ht) 'red) (remhash 'color ht)) is T if removed
BLOCK Establishes a block name for control flow with RETURN-FROM. (block my-block (format T "Start.") (return-from my-block :done) (format T "End."))
RETURN-FROM Exits a named block, optionally returning a value. See BLOCK example.
RETURN Exits the current function or a NIL named block. (loop for i from 1 to 10 do (when (> i 5) (return i))) returns 6
TAGBODY Defines a sequence of forms with GO tags. (tagbody (setf x 0) start (incf x) (princ x) (when (< x 3) (go start))) prints 123
GO Transfers control to a TAGBODY tag. See TAGBODY example.
LET* Defines local variables sequentially, allowing later bindings to use earlier ones. (let* ((x 1) (y (+ x 1))) y) evaluates to 2
WHEN Evaluates a body of forms if a test expression is true. (when (> 5 3) (print 'hello)) prints HELLO
UNLESS Evaluates a body of forms if a test expression is false. (unless (< 5 3) (print 'world)) prints WORLD
CASE Multi-way conditional based on an equality test. (case 'red ((blue green) 'cold) ((red yellow) 'warm) (T 'unknown)) evaluates to WARM
TYPECASE Multi-way conditional based on the type of an object. (typecase "str" (string 's) (number 'n)) evaluates to S
VALUES Returns multiple values from a function. (values 1 2 3)
MULTIPLE-VALUE-BIND Binds multiple values returned by a form to variables. (multiple-value-bind (q r) (floor 10 3) (list q r)) evaluates to (3 1)
CATCH Establishes a catch tag for non-local exits with THROW. (catch 'abort (sub-func)) where sub-func contains (throw 'abort 99)
THROW Performs a non-local exit to a matching CATCH tag. (throw 'abort 99)
ERROR Signals an unrecoverable error. (error "Something went wrong.")
UNWIND-PROTECT Ensures certain forms are evaluated regardless of how the protected form exits. (unwind-protect (error "fail") (print 'cleanup)) prints CLEANUP before erroring
FBOUNDP Checks if a symbol is globally bound to a function. (fboundp #'+) is T
SYMBOL-FUNCTION Returns the function object associated with a symbol. (setf (symbol-function 'my-add) #'+) (my-add 1 2) evaluates to 3
DOCUMENTATION Retrieves the documentation string for a symbol (function, variable, etc.). (documentation #'+ 'function) returns "Sum of its arguments..."
LABELS Defines local functions with in a scope. (labels ((inc (x) (1+ x))) (inc 5)) evaluates to 6
COND A general conditional form, similar to a series of if-then-else. (cond ((> 3 4) "first") ((> 4 2) "second") (T "default")) evaluates to "second"
DECLARE Provides declarations to the Common Lisp compiler. (declare (optimize (speed 3)))
COMPILED-FUNCTION-P Checks if a function object is a compiled function. (compiled-function-p #'+) is T
COMPILE Compiles a function. (compile 'my-function)
COMPILE-FILE Compiles a Lisp source file into a fasl file. (compile-file "my-code.lisp")
MAKE-PATHNAME Constructs a pathname object. (make-pathname :name "data" :type "txt")
OPEN Opens a file stream. (open "file.txt" :direction :output :if-exists :supersede)
CLOSE Closes a file stream. (close my-stream)
READ-LINE Reads a line of text from a stream. (read-line) from standard input
WITH-OPEN-FILE Macro to open a file, execute forms, and ensure the file is closed. (with-open-file (s "out.txt" :direction :output) (format s "Hello!"))
PRIN1 Prints an S-expression representation of an object (with quotes for strings). (prin1 "abc") prints "abc"
PRINC Prints a human-readable representation of an object (without quotes for strings). (princ "abc") prints abc
SYMBOL-NAME Returns the string name of a symbol. (symbol-name 'foo) evaluates to "FOO"
SYMBOL-PLIST Returns the property list of a symbol. (setf (get 'apple 'color) 'red) (symbol-plist 'apple) evaluates to (COLOR RED)
DEFPACKAGE Defines a Common Lisp package. (defpackage "APP" (:use "COMMON-LISP") (:export "RUN"))
IN-PACKAGE Switches the current package context. (in-package :app)
FLOAT Converts a real number to a floating-point number. (float 3/2) evaluates to 1.5
TRUNCATE Returns the integer part of a number towards zero. (truncate 3.7) evaluates to 3
FLOOR Returns the largest integer less than or equal to a number. (floor 3.7) is 3; (floor -3.7) is -4
CEILING Returns the smallest integer greater than or equal to a number. (ceiling 3.7) is 4; (ceiling -3.7) is -3
ROUND Returns the nearest integer; rounds to the nearest even integer on ties. (round 2.5) is 2; (round 3.5) is 4
RANDOM Returns a pseudo-random number less than its argument. (random 10) returns an integer in [0, 9]
SIGNUM Returns -1, 0, or 1 indicating the sign of the argument. (signum -5) evaluates to -1
EXPT Raises a base to a power (x^n). (expt 2 3) evaluates to 8
LOG Computes the logarithm of x in base n. If n is omitted, base e is used. (log 8 2) evaluates to 3.0
EXP Computes e^x. (exp 1) evaluates to 2.718...
SQRT Computes the square root of x. (sqrt 9) evaluates to 3.0
SIN, COS, TAN Trigonometric functions. (sin 0) evaluates to 0.0
ASIN, ACOS, ATAN Inverse trigonometric functions. (atan 1) evaluates to 0.785... (pi/4)
SINH, COSH, TANH Hyperbolic functions. (sinh 0) evaluates to 0.0
ASINH, ACOSH, ATANH Inverse hyperbolic functions.
PI The mathematical constant pi. PI
EVAL Evaluates a Lisp form. (eval '(+ 1 2)) evaluates to 3
DEFMACRO Defines a macro. (defmacro square (x) (* ,x ,x))`
MACROEXPAND-1 Expands a macro form once (useful for testing macros). (macroexpand-1 '(square 5)) evaluates to (* 5 5)
, (unquote) Within backquoted forms, evaluates the following form. `(a is ,(+ 1 2)) evaluates to (A IS 3)
,@ (unquote-splicing) Within backquoted forms, evaluates and splices a list into the surrounding list. `(elements are ,@'(1 2)) evaluates to (ELEMENTS ARE 1 2)
GENSYM Generates a new, uninterned symbol, ensuring uniqueness in macros. (let ((g (gensym))) (let ((,g 0)) ,g)))`
DEFSTRUCT Defines a structure type (like a simple record or struct). (defstruct point x y)
MAKE-INSTANCE Creates an instance of a CLOS class. (make-instance 'circle)
SLOT-VALUE Accesses the value of a slot in a CLOS instance. (setf (slot-value my-circle 'radius) 10)
:accessor DEFCLASS slot option: creates a reader and writer function for a slot. (radius :accessor circle-radius)
:reader DEFCLASS slot option: creates a reader function for a slot. (radius :reader get-radius)
:writer DEFCLASS slot option: creates a writer function for a slot. (radius :writer set-radius)
:initarg DEFCLASS slot option: specifies a keyword argument for MAKE-INSTANCE. (radius :initarg :initial-radius)
:initform DEFCLASS slot option: specifies a default value for a slot. (radius :initform 1.0)
:allocation :class DEFCLASS slot option: creates a class-allocated slot (shared by all instances). (story :allocation :class)
DEFMETHOD Defines a method for a generic function (CLOS). (defmethod area ((c circle)) (* PI (expt (circle-radius c) 2)))
:before, :after, :around Method combination options for generic functions. (defmethod process :before ((obj my-class)) ...)
DECLAIM Makes global declarations, such as optimization or type declarations. (declaim (optimize (speed 3) (safety 0)))
(OPTIMIZE ...) DECLARE/DECLAIM option for compiler optimization settings. (optimize (speed 3) (debug 0))
(INLINE x) DECLARE/DECLAIM option: suggests the compiler inline a function. (declaim (inline square-val))
(FIXNUM x) DECLARE/DECLAIM option: declares a variable or function return as a fixnum. (declare (fixnum count))
(TYPE ... v) DECLARE/DECLAIM option: specifies the type of a variable or place. (declare (type (vector fixnum 20) my-vector))
DEFTYPE Defines a new type specifier. (deftype non-empty-list () '(and list (not null)))
PACKAGE-NAME Returns the name of a package. (package-name (find-package :cl)) evaluates to "COMMON-LISP"
FIND-PACKAGE Returns the package object for a given name. (find-package "COMMON-LISP-USER")
SYMBOL-PACKAGE Returns the package in which a symbol is interned. (symbol-package 'cl:t)
EXPORT Makes symbols accessible from other packages. (export 'my-function :my-app-package)
IMPORT Makes symbols from other packages directly accessible in the current package. (import 'cl:format)
USE-PACKAGE Makes all exported symbols of another package accessible. (use-package :my-utils)
TRACE Enables tracing of function calls for debugging. (trace my-function)
UNTRACE Disables tracing for functions. (untrace my-function)

Destructive vs. Non-Destructive Operations

Common Lisp often provides both non-destructive versions (returning a new object) and destructive versions (modifying the original object) for sequence manipulation functions. Destructive operations typically start with N (for

Related Articles

Efficient Usage of HTTP Client in IntelliJ IDEA

IntelliJ IDEA incorporates a versatile HTTP client tool, enabling developres to interact with RESTful services and APIs effectively with in the editor. This functionality streamlines workflows, replac...

Installing CocoaPods on macOS Catalina (10.15) Using a User-Managed Ruby

System Ruby on macOS 10.15 frequently fails to build native gems required by CocoaPods (for example, ffi), leading to errors like: ERROR: Failed to build gem native extension checking for ffi.h... no...

Resolve PhpStorm "Interpreter is not specified or invalid" on WAMP (Windows)

Symptom PhpStorm displays: "Interpreter is not specified or invalid. Press ‘Fix’ to edit your project configuration." This occurs when the IDE cannot locate a valid PHP CLI executable or when the debu...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.