Open In App

Defstruct in LISP

Last Updated : 24 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

A structure is a user-defined data type that helps us combine different data items of different types under the same name.  In Lisp, defstruct is used to define a structure of multiple data items of different data types.

Syntax:

(defstruct student
name
class
roll-no
birth-date
)
 

  1. In the above declaration, we have defined four named components for the structure student.
  2. The named components take the argument in them through named functions.
  3. An implicit function named copy-book of one argument is also defined which takes a student instance and creates another student instance, which is a clone or copy of the first one. This function is known as the copier function.
  4. Another implicit function named make-student will be created, a constructor, which will create a data structure with four components, suitable for use with the access functions.
  5. To read or print the instances of ‘student’ we can use #S syntax which itself refers to a structure.
  6. To alter the components of the structure ‘student’ we can use self as we would use below.

Example:

Lisp




; LISP program for destruct
(defstruct student
name
class
roll-no
birth-date
)
  
( setq student1 (make-student :name"Kishan Pandey"
   :class "12" 
   :roll-no "102016114"
   :birth-date "31-08-2002")
)
  
( setq student2 (make-student :name"Sakshi Tripathi"
   :class "12" 
   :roll-no "102016115"
   :birth-date "14-03-2000")
)
(write student1)
(terpri)
(write student2)
(setq student3( copy-student student1))
(setf (student-roll-no student3) 102016116
(terpri)
(write-line "A Copy of the first student is:")
(write student3)


Output:

 


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads