28 lines
755 B
Common Lisp
28 lines
755 B
Common Lisp
;;; STRUCTS 1: DEFSTRUCT
|
|
;;;
|
|
;;; DEFSTRUCT defines a record type and generates, for free:
|
|
;;; a constructor (make-person), slot readers (person-name),
|
|
;;; and a predicate (person-p).
|
|
|
|
(defstruct person
|
|
name
|
|
age)
|
|
|
|
;; TODO: Make Ada: name "Ada", age 36.
|
|
(defun make-ada ()
|
|
(make-person :name "Ada"))
|
|
|
|
;; TODO: Ada's age.
|
|
(defun ada-age (ada)
|
|
(person-name ada))
|
|
|
|
;;; ------------------------------------------------------------------
|
|
;;; Checks. Read them - they tell you what to aim for - but leave
|
|
;;; them unchanged.
|
|
;;; ------------------------------------------------------------------
|
|
|
|
(check (let ((ada (make-ada)))
|
|
(and (equal "Ada" (person-name ada))
|
|
(eql 36 (person-age ada)))))
|
|
(check (eql 36 (ada-age (make-ada))))
|