
- LISP Tutorial
- LISP - Home
- LISP - Overview
- LISP - Environment
- LISP - REPL
- LISP - Program Structure
- LISP - Basic Syntax
- LISP - Data Types
- Lisp Macros
- LISP - Macros
- LISP - Backquote and Comma
- LISP - Code Generation Using Macro
- LISP - Variable Capture and Hygienic macro
- LISP - Scope and Binding
- LISP - Macro Writing Style
- LISP - Macro Characters
- LISP - Read-Time Macros
- LISP - Compiler Macros
- LISP - Uses of Macros
- Lisp Functions
- LISP - Functions
- LISP - Functions vs Macros
- LISP - Calling Function using funcall
- LISP - Calling Function using apply
- LISP - Closures
- LISP - Functions as Arguments
- LISP - Functions as Return Values
- LISP - Recursion
- LISP - Built-in Functions
- Lisp Predicates
- LISP - Predicates
- LISP - Generic Data Type Predicates
- LISP - Specific Data Type Predicates
- LISP - Equality Predicates
- LISP - Numeric Predicates
- LISP - Comparison Predicates
- LISP - Logical Predicates
- LISP - List Predicates
- LISP - Custom Predicates
- LISP - Chaining Predicates
- Lisp Arrays
- LISP - Arrays
- LISP - Adjustable Arrays
- LISP - Fill Pointers in Arrays
- LISP - Specialized Arrays
- LISP - Arrays Properties
- LISP - Iterating over Arrays
- LISP - Multidimensional Arrays
- LISP - Row-Major Order
- Lisp Strings
- LISP - Strings
- LISP - String Concatenation
- LISP - String Comparison
- LISP - String Case Conversion
- LISP - String Trimmimg
- LISP - String Searching
- LISP - Getting Substring
- LISP - String Replacement
- LISP - Sorting Strings
- LISP - Merging Strings
- LISP - Accessing Characters of String
- LISP - String length
- LISP - Escape Sequences
- Lisp Sequences
- LISP - Sequences
- LISP - Accessing Element of Sequence
- LISP - Sequence length
- LISP - Getting Subsequence
- LISP - Search Element in Sequence
- LISP - Sequence Concatenation
- LISP - Reversing a Sequence
- LISP - Mapping Sequence Element
- LISP - position of Element
- LISP - Remove an Element
- LISP - Sort Sequence
- LISP - Merge Sequences
- LISP - every function
- LISP - some function
- LISP - notany function
- LISP - notevery function
- Lisp Lists
- LISP - Lists
- LISP - Accessing Elements of Lists
- LISP - Modifications to Lists
- LISP - Using mapcar on List
- LISP - Using mapc on List
- LISP - Using reduce on List
- LISP - Removing elements from List
- LISP - Reversing a List
- LISP - Sorting a List
- LISP - Searching a List
- LISP - List vs Vectors
- LISP - Matrix Multiplication
- Lisp Vectors
- LISP - Vectors
- LISP - Creating Vectors
- LISP - Accessing Elements of Vectors
- LISP - Modifications to Vectors
- LISP - Adjustable Vectors
- LISP - Specialized Vectors
- LISP - Vector Functions
- Lisp Set
- LISP - Set
- LISP - Adding elements to the Set
- LISP - Getting SubSet from a Set
- LISP - Set Difference
- LISP - Set Exclusive OR
- LISP - Set Intersection
- LISP - Set Union
- LISP - Representing Set with HashTable
- LISP - List as Set vs HashTable as Set
- Lisp Tree
- LISP - Tree
- LISP - Recursive Traversal
- LISP - Inorder Traversal
- LISP - Preorder Traversal
- LISP - Postorder Traversal
- LISP - Depth First Traversal
- LISP - Modifying Tree
- LISP - Search Tree
- LISP - Binary Tree
- Lisp Hash Table
- LISP - Hash Table
- Adding Values to Hash Table
- Removing Values from Hash Table
- Updating Values of Hash Table
- Iterating Hash Table Entries
- Searching key in HashTable
- Checking Size of HashTable
- Using Custom Equality Check
- Lisp - Input − Output
- LISP - Input − Output
- LISP - Streams
- LISP - Reading Data from Streams
- LISP - Writing Data to Streams
- LISP - File I/O
- LISP - String I/O
- LISP - Formatting with Format
- LISP - Interactive I/O
- LISP - Error Handling
- LISP - Binary I/O
- Lisp - Structures
- LISP - Structures
- LISP - Accessors and Mutators
- LISP - Structure Options
- LISP - Structure Types
- LISP - Applications and Best Practices
- Lisp - CLOS
- LISP - CLOS
- Lisp - Objects
- LISP - Class
- LISP - Slots and Accessors
- LISP - Generic Functions
- LISP - Class Precedence
- LISP - Metaobject Protocol
- LISP - Multimethods
- LISP - Multiple Inheritance
- LISP - Method Combinations
- LISP - Method Combinations
- LISP - :before Method Combination
- LISP - :primary Method Combination
- LISP - :after Method Combination
- LISP - :around Method Combination
- LISP - + Method Combination
- LISP - and Method Combination
- LISP - append Method Combination
- LISP Useful Resources
- Lisp - Quick Guide
- Lisp - Useful Resources
- Lisp - Discussion
Lisp Cheatsheet
Lisp Cheatsheet provides the basic concepts of all the fundamental topics. Lisp stands for List Processing and is known as the second high-level programming language after Fortran. Also, Lisp has been historically significant in the development of artificial intelligence. Go through this cheat sheet and learn the concepts of Lispprogramming language.
- Basic Overview of Lisp
- Data Types in Lisp
- Comments in Lisp
- Lisp − Macros
- Lisp − Variables and Constants
- Lisp − Numbers Data Types
- Lisp − Characters
- Operators of Lisp
- Decision Making Structure in Lisp
- Lisp − Loops
- Function in Lisp
- Predicates
- Lisp − Strings
- Lisp − Arrays
- Set and List
- Lisp − Sequence
- Symbol and Vector
- Lisp − Tree
- Hash Table in Lisp
- Packages in Lisp
- Lisp − File Handling
- Lisp − Error Handling
1. Basic Overview of Lisp
In the basic overview of Lisp, we provide the basic syntax to start the Lisp programming language. Here, we display the text to the output.
(write-line "Tutorialspoint!")
2. Data Types in Lisp
The data types define the kinds of data that can be processed and changed within the language. These are categorized into two different parts −
- Scalar Types − Number Types, Characters, Symbol, etc.
- Data Structure − Lists, Vectors, Bit-vectors, and Strings.
3. Comments in Lisp
The lisp comments are created using semicolon (;). For multi-line comments it uses the semicolon for the multiple times.
; This is a single line comments ; This is ; multi-line ; comments
4. Lisp − Macros
Lisp macros are a core feature of this programming language. It allows the programmer to extend the language by defining the new syntaxes.
(defmacro macroName (arguments) expressions )
5. Lisp − Variables and Constants
In Lisp, variables are used to store the specific values. It support two types of variables −
- Local variables: These are variables whose scope is limited to the block of the function in which they are defined. They are not accessible outside of the function.
- Global variables: These are variables with a global scope that means they can be accessed from anywhere in the program.
Syntax of local variable in the lisp programming language −
(let (variable) body-of-expressions)
Syntax of global variable in the lisp programming language −
(defvar *name* initial-value "string") (defparameter *name* initial-value "string")
Constants are such types of variables whose value never changes.
(defconstant PI 3.141592)
6. Lisp − Numbers Data Types
In Lisp, number data types support the numeric value and categorize into four different types − integers, ratios, floating-point numbers, and complex numbers.
(defun display-number-types () (let ((int-example 42) (ratio-example 22/7) (float-example 3.14159) ; complex number 1 + 2i (complex-example #C(1 2))) (format t "Integer example: ~A~%" int-example) (format t "Ratio example: ~A~%" ratio-example) (format t "Floating-point example: ~A~%" float-example) (format t "Complex number example: ~A~%" complex-example))) ; Call the function (display-number-types)
7. Lisp − Characters
In Lisp, the characters are represent using data object which is denoted by #\(object preceeding).
(defun display-character () (let ((char-a #\A) (char-b #\B) (char-space #\Space) (char-newline #\Newline)) (format t "Character A: ~A~%" char-a) (format t "Character B: ~A~%" char-b) (format t "Space character: ~A~%" char-space) (format t "Newline character: ~A~%" char-newline))) ; Call the function (display-character)
8. Operators of Lisp
In Lisp, an operators are specific symbol to perform the mathematical or logical calculation/operations.
Operations | Description | Example |
---|---|---|
Arithmetic Operations | These are basic mathematical operations. | '(+ a b)', '(- a b)', '(* a b)', '(/ a b)', '(mod a b)' |
Comparison Operations | These compare two values. | '(= a b)', '(not (= a b))', '(> a b)', '(= a b)', '( |
Logical Operations | These combine the conditional statements. | '(and a b)', '(or a b)', '(not a)', '(xor a b)' |
Bitwise Operations | These perform operations at the bit level. | '(logand a b)', '(logior a b)', '(logxor a b)', '(lognot a)', '(ash a b)', '(lsh a b)' |
9. Decision Making Structure in Lisp
Decision-making is the process of evaluating one or more conditions based on logic. There are four types of decision-making structures −
i. cond Construct
The cond contruct evaluates multiple conditions and executes the first true clause.
(cond (test1 action1) (test2 action2) ... (testn actionn))
ii. if Construct
The if construct executes one of two expressions based on a true/false test.
(if (test-clause) (action1) (action2))
iii. when Construct
The when construct executes expressions if a condition is true; returns nil otherwise.
(when (test-clause) (<action1) )
iv. case Construct
The case construct matches a value against multiple option and executes the first match.
(case (keyform) ((key1) (action1 action2 ...) ) ((key2) (action1 action2 ...) ) ... ((keyn) (action1 action2 ...) ))
10. Lisp − Loops
In Lisp, loops are groups of statements that execute a set of instructions repeatedly for multiple times. There are various types of loops that used in this programming language −
- dotimes: This loop iterates a specified number of times.
Following is the illustration of dotimes loop in lisp −
(setq squares '()) (dotimes (i 5) ; Add the square of `i` to the list (setq squares (cons (* i i) squares))) ; reverse to get the correct order (format t "Squares of each number(0, 1, 2, 3, 4): ~A~%" (reverse squares))
- dolist: This loop iterates over a list, binding each element to a variable.
Following is the illustration of dolist loop in lisp −
(setq my-list '(1 2 3 4 5)) ; iterate over each element in the list (dolist (item my-list) (format t "Item: ~A~%" item))
- loop: This is generic loop macro that provides more control over the iteration process.
Following is the illustration of loop in lisp −
(setq a 1) ; Set a to 1 initially (loop ; increment a by 1 (setq a (+ a 1)) ; Print a (write a) ; Print a new line (terpri) ; Stop the loop when a exceeds 5 (when (> a 5) (return)) )
11. Function in Lisp
In Lisp, the function is a block of code that can be reused for specific tasks. Here, lisp functions are first-class objects, which means they can be passed as parameters, returned as results, or assigned to variables.
; defining the function to add two numbers (defun add-numbers (a b) "Add two numbers A and B." (+ a b)) (setq result (add-numbers 5 7)) (format t "The sum is: ~A~%" result)
12. Predicates
In Lisp, predicates are functions that test the condition based on a boolean return value either true or false.
; check if a list is empty (null '()) ; Returns T (true) (null '(1 2 3)) ; Returns NIL (false) ; check and print if 7 is an even number (write (evenp 7 )) ; terminate printing (terpri)
13. Lisp − Strings
In Lisp, strings are the set of characters that are represented using double quotes (" "). These are immutable, which means they cannot be changed in place. For example − "JAVA", "C", "Python", etc.
(defun simple-string () (let ((str1 "Welcome to") (str2 "Tutorialspoint")) (format t "Concatenated String: ~A~%" (concatenate 'string str1 " " str2)))) ; Call the function (simple-string)
14. Lisp − Arrays
Lisp arrays allows users to define single−dimensional or multi−dimensional arrays. The lips arrays are represented using the make-array function.
(defun simple-array () (let ((arr #(1 2 3 4 5))) (format t "Array: ~A~%" arr) (format t "Element at index 2: ~A~%" (aref arr 2)) (setf (aref arr 2) 10) (format t "Modified Array: ~A~%" arr) (format t "New element at index 2: ~A~%" (aref arr 2)))) ; Call the function (simple-array)
15. Set and List
The term "set" in Lisp refers to a collection of unique, immutable elements.
; Define a set (list) (setq my-set '(1 2 3 4 5 6)) ; Remove an element (3) from the set (setq my-set (remove 3 my-set)) ; Display the set (format t "Set after removal: ~A~%" my-set)
In Lisp, lists are singly linked lists, which are a part of the data structure. The function conc defines the list. It accepts two parameters − the first element is added to the list, and the second parameter is either null or added to the existing list.
(defun conc (first-element second-list) ; Concatenate the first element with the second list (cons first-element second-list)) ; Add 1 to the front of the list (2 3 4) (setq my-list (conc 1 '(2 3 4))) ; Display the new list (format t "New list: ~A~%" my-list)
16. Lisp − Sequence
In Lisp, sequence defines the ordered collection of elements. The sequence of Lisp is used in many forms, such as lists, arrays, vectors, strings, or other data structures.
; create a sequence of 10 floating numbers initilized with 1.0 (write (make-sequence '(vector float) 5 :initial-element 20.0))
17. Symbol and Vector
Symbol of lisp are the fundamental data types that represent the data objects(name and value).
In Lisp, the vector is a data type comparatively similar to an array in the other programming language. This is represented using the sequence of elements which is enclosed by the hash parenthesis [#()].
Here, we will see how to execute vectors and symbols in the lisp programming language.
; Define a symbol and a vector (defparameter *my-symbol* 'data) (defparameter *my-vector* #(10 20 30)) ; Display the symbol and vector elements (format t "Symbol: ~A~%" *my-symbol*) (format t "Vector elements: ~A ~A ~A~%" (aref *my-vector* 0) (aref *my-vector* 1) (aref *my-vector* 2))
18. Lisp − Tree
In Lisp, a tree defines the data structure which is commonly used to represent the hierarchical relationships between the elements.
; set a tree to tr (setq tr '((11 12 (13 14 15) ((17 18) (17 18 19))))) ; print tree (write tr) ; set trs as subtree (setq trs (subst 7 1 tr)) ; terminate printing (terpri) ; print tree (write trs)
19. Hash Table in Lisp
The hash table is a part of the data structure by representing the collection of key-value pairs. This can be organized based on hash code.
; Create the hash table (defparameter *my-hash-table* (make-hash-table)) ; Add key-value pairs (setf (gethash 'apple *my-hash-table*) 10) (setf (gethash 'banana *my-hash-table*) 20) ; Retrieve and print a value (format t "Quantity of apples: ~A~%" (gethash 'apple *my-hash-table*)) ; Display the contents of the hash table (format t "~%Contents of the hash table:~%") (maphash (lambda (key value) (format t "~A: ~A~%" key value)) *my-hash-table*)
20. Packages in Lisp
Lisp packages refer to the collection of related functions and variables.
(defpackage :package-name (:use :common-lisp ...) (:export :symbol1 :symbol2 ...) )
21. Lisp − File Handling
In Lisp, the file handling processes the various tasks over files, such as creating, opening, and closing text or binary files for data storage.
; Create and write to a file ; Open file for writing (let ((stream (open "example.txt" :direction :output :if-exists :supersede))) ; Write a line to the file (write-line "Hello, World!" stream) (write-line "This is a test file." stream) ; Closing the file (close stream)) ; Read from the file ; Open file for reading (let ((stream (open "example.txt" :direction :input))) (loop for line = (read-line stream nil) while line do (format t "~A~%" line)) (close stream))
22. Lisp − Error Handling
In Lisp, an error handling is the process to handling the error during the run time execution.
(defun divide (a b) (handler-case ; division (/ a b) ; catch the error (error () ;; Catch any error (format t "Error: Division by zero is not allowed.~%")))) ; call the function (divide 10 2) ; catch the division by zero error (divide 10 0)