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
)
- In the above declaration, we have defined four named components for the structure student.
- The named components take the argument in them through named functions.
- 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.
- 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.
- To read or print the instances of 'student' we can use #S syntax which itself refers to a structure.
- To alter the components of the structure 'student' we can use self as we would use below.
Example:
; 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:
