Methods and Classes in Lisp
1. DEFMETHOD
Example: Suppose you define 3 structures for triangle, rectangle,
and circle:
(defstruct triangle
(base 0)
(altitude 0))
(defstruct rectangle
(width 0)
(height 0))
(defstruct circle
(radius 0))
A function for AREA could be defined as:
(defun area (figure)
(cond ((triangle-p figure) (triangleArea figure))
((rectangle-p figure) (rectangleArea figure))
((circle-p figure) (circleArea figure))))
With helper functions for the area of each type of figure:
(defun triangleArea (figure)
(* 1/2 (triangle-base figure) (triangle-height figure)))
OR you can define 3 area methods:
(defmethod area ((figure triangle)) ;;specifies figure must be
(* 1/2 ;;must be a triangle
(triangle-base figure)
(triangle-height)))
(defmethod area ((figure rectangle)) ;;specifies figure must be
(* (rectangle-width figure) ;;must be a rectangle
(rectangle-height figure)))
Etc.
2. DEFCLASS
Basic template:
(defclass <class-name> (<list of super classes>)
((<slot name 1> :accessor <accessor procedure 1>
:initform <initial value form 1>
:initarg <arg marking symbol 1>)
. . .
(<slot name n> :accessor <accessor procedure n>
:initform <initial value form n>
:initarg <arg marking symbol n>)
)
)
NOTE: classes use "make-instance" NOT "make-" in order to create
an instance of a class.
Examples:
(defclass article () ;; no superclasses
((title :accessor article-title :init-arg :title)
(author :accessor article-author :init-arg :author)))
3 subclasses of article:
(defclass computer-article (article) ())
(defclass business-article (article) ())
(defclass political-article (article) ())
All of the subclasses slots are inherited from the superclass article
Define a "friend" class:
(defclass friend () ;; no superclasses
((name :accessor friend-name :init-arg :name))) ;;1 slot
3 subclasses of friend:
(defclass hacker-friend (friend) ())
(defclass entrepenuer-friend (friend) ())
(defclass philosopher-friend (friend) ())
Make 3 articles of different types:
(setf articles
(list (make-instance 'business-article
:title "Memory Prices Down")
(make-instance 'computer-article
:title "Memory Speeds Things Up")
(make-instance 'political-article
:title "Memory Impugned")))
Make 4 friends:
(setf friends
(list (make-instance 'hacker-friend :name 'Dan)
(make-instance 'hacker-friend :name 'Gerry)
(make-instance 'entrepenuer-friend :name 'Philip)
(make-instance 'philosopher-friend :name 'David)))
Print articles:
(defun print-notification (article friend)
(format t "~%Tell ~a about \"~a.\""
(friend-name friend)
(article-title article))
t)
Print all articles read by everyone:
(dolist (friend friends)
(dolist (article articles)
(print-notification article friend)))
Print Specific articles read:
(dolist (friend friends)
(dolist (article articles)
(process article friend)))
If 4 "methods" named "process" are defined:
(defmethod process ((friend hacker-friend) ;;specific type of friend
(article computer-article)) ;; and article
(print-notification article friend))
(defmethod process ((friend entrepenuer-friend) ;;specific type of friend
(article business-article)) ;; and article
(print-notification article friend))
(defmethod process ((friend philosopher-friend) ;;specific type of friend
(article article)) ;; ANY article
(print-notification article friend))
(defmethod process ((friend friend) ;;Generic, "blank", function is needed
(article article)))