Lisp - Structure Accessors and Mutators



A Structure in LISP is a user defined data type which allows us to create new data types. Accessor and Mutators are fundamental players assisting in how we can interact with a struture. In this chapter, we're discussing structure accessor and mutators with examples.

Accessor

An accessor is a function that allows to retrieve value stored in the slot of the structure. An accessor provides a controlled way to get data.

When a structure is created using defstruct, LISP automatically creates accessor for each slot. These auto-generated functions have names derived from the slot-name.

Example - Accessor

Let's first create a structure book with slots for title, author, subject and book-id.

(defstruct book 
   title 
   author 
   subject 
   book-id 
)

Now LISP automatically generates accessor functions like book-title to get value of title. We will use following syntax to get value of title of a book from an instance book1 using following syntax.

(book-title book1)

Following is a complete code of getting value of title using its accessor function.

main.lisp

; define a structure book
(defstruct book 
   title 
   author 
   subject 
   book-id 
)
; create and assign a structure to book1
( setq book1 (make-book :title "C Programming"
   :author "Nuha Ali" 
   :subject "C-Programming Tutorial"
   :book-id "478")
)
; get and print the value of title
(write-line(book-title book1))

Output

When you execute the code, it returns the following result −

C Programming

Mutator

A mutator is a function that allows to modify value stored in the slot of the structure. An accessor provides a controlled way to set data.

We are not required to generate mutators. We can use setf, a general LISP macro to modify the slot. We will use following syntax to set value of title of a book of an instance book1 using following syntax.

(setf (book-title book1) "LISP Programming")

Following is a complete code of setting value of title using its accessor function.

main.lisp

; define a structure book
(defstruct book 
   title 
   author 
   subject 
   book-id 
)
; create and assign a structure to book1
( setq book1 (make-book :title "C Programming"
   :author "Nuha Ali" 
   :subject "C-Programming Tutorial"
   :book-id "478")
)
; get and print the value of title
(write-line(book-title book1))

; modify the title
(setf (book-title book1) "LISP Programming")

; get and print the updated value of title 
(write-line(book-title book1))

Output

When you execute the code, it returns the following result −

C Programming
LISP Programming

Key Considerations

  • Encapsulation − Accessors and Mutators supports encapsulation by providing a controlled interface to access and modify values of a structure. This improves code readability and prevents unintended changes to values.

  • Consistency − We can handle values consistently using accessors and mutators.

  • Flexibility − A mutator or accessor can be customized as well to implement custom validation logic before setting the values.

Advertisements