
- 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 - Class
Common LISP predated the advance of object-oriented programming by couple of decades. However, it object-orientation was incorporated into it at a later stage.
Defining Classes
The defclass macro allows creating user-defined classes. It establishes a class as a data type. It has the following syntax −
(defclass class-name (superclass-name*) (slot-description*) class-option*))
The slots are variables that store data, or fields.
A slot-description has the form (slot-name slot-option*), where each option is a keyword followed by a name, expression and other options. Most commonly used slot options are −
:accessor function-name
:initform expression
:initarg symbol
For example, let us define a Box class, with three slots length, breadth, and height.
(defclass Box () (length breadth height) )
Providing Access and Read/Write Control to a Slot
Unless the slots have values that can be accessed, read or written to, classes are pretty useless.
You can specify accessors for each slot when you define a class. For example, take our Box class −
(defclass Box () ((length :accessor length) (breadth :accessor breadth) (height :accessor height) ) )
You can also specify separate accessor names for reading and writing a slot.
(defclass Box () ((length :reader get-length :writer set-length) (breadth :reader get-breadth :writer set-breadth) (height :reader get-height :writer set-height) ) )
Creating Instance of a Class
The generic function make-instance creates and returns a new instance of a class.
It has the following syntax −
(make-instance class {initarg value}*)
Example
Let us create a Box class, with three slots, length, breadth and height. We will use three slot accessors to set the values in these fields.
Create a new source code file named main.lisp and type the following code in it.
main.lisp
; define a class box with accessor methods for length, breadth and height (defclass box () ((length :accessor box-length) (breadth :accessor box-breadth) (height :accessor box-height) ) ) ; create an instance of class box and assign to an item (setf item (make-instance 'box)) ; set length of an item as 10 (setf (box-length item) 10) ; set breadth of an item as 10 (setf (box-breadth item) 10) ; set height of an item as 5 (setf (box-height item) 5) ; print length of the box (format t "Length of the Box is ~d~%" (box-length item)) ; print breadth of the box (format t "Breadth of the Box is ~d~%" (box-breadth item)) ; print height of the box (format t "Height of the Box is ~d~%" (box-height item))
Output
When you execute the code, it returns the following result −
Length of the Box is 10 Breadth of the Box is 10 Height of the Box is 5
Defining a Class Method
The defmethod macro allows you to define a method inside the class. The following example extends our Box class to include a method named volume.
Update the source code file named main.lisp and type the following code in it.
main.lisp
; define a class box with accessor methods for length, breadth and height ; add a method reader (defclass box () ((length :accessor box-length) (breadth :accessor box-breadth) (height :accessor box-height) (volume :reader volume) ) ) ; method calculating volume (defmethod volume ((object box)) (* (box-length object) (box-breadth object)(box-height object)) ) ;setting the values (setf item (make-instance 'box)) (setf (box-length item) 10) (setf (box-breadth item) 10) (setf (box-height item) 5) ; displaying values (format t "Length of the Box is ~d~%" (box-length item)) (format t "Breadth of the Box is ~d~%" (box-breadth item)) (format t "Height of the Box is ~d~%" (box-height item)) (format t "Volume of the Box is ~d~%" (volume item))
Output
When you execute the code, it returns the following result −
Length of the Box is 10 Breadth of the Box is 10 Height of the Box is 5 Volume of the Box is 500
Inheritance
LISP allows you to define an object in terms of another object. This is called inheritance. You can create a derived class by adding features that are new or different. The derived class inherits the functionalities of the parent class.
The following example explains this −
Example
Update the source code file named main.lisp and type the following code in it.
main.lisp
; define a class box with accessor methods for length, breadth and height (defclass box () ((length :accessor box-length) (breadth :accessor box-breadth) (height :accessor box-height) (volume :reader volume) ) ) ; method calculating volume (defmethod volume ((object box)) (* (box-length object) (box-breadth object)(box-height object)) ) ;wooden-box class inherits the box class (defclass wooden-box (box) ((price :accessor box-price))) ;setting the values (setf item (make-instance 'wooden-box)) (setf (box-length item) 10) (setf (box-breadth item) 10) (setf (box-height item) 5) (setf (box-price item) 1000) ; displaying values (format t "Length of the Wooden Box is ~d~%" (box-length item)) (format t "Breadth of the Wooden Box is ~d~%" (box-breadth item)) (format t "Height of the Wooden Box is ~d~%" (box-height item)) (format t "Volume of the Wooden Box is ~d~%" (volume item)) (format t "Price of the Wooden Box is ~d~%" (box-price item))
Output
When you execute the code, it returns the following result −
Length of the Wooden Box is 10 Breadth of the Wooden Box is 10 Height of the Wooden Box is 5 Volume of the Wooden Box is 500 Price of the Wooden Box is 1000