27 lines
856 B
Common Lisp
27 lines
856 B
Common Lisp
;;; CLOS 5: WITH-SLOTS AND WITH-ACCESSORS (solution)
|
|
|
|
(defclass point ()
|
|
((x :initarg :x :accessor point-x)
|
|
(y :initarg :y :accessor point-y)))
|
|
|
|
(defun double-point (p)
|
|
(with-slots (x y) p
|
|
(make-instance 'point :x (* x 2) :y (* y 2))))
|
|
|
|
(defun shift-x (p)
|
|
(with-accessors ((x point-x)) p
|
|
(setf x (1+ x)))
|
|
p)
|
|
|
|
;;; ------------------------------------------------------------------
|
|
;;; Checks. Read them - they tell you what to aim for - but leave
|
|
;;; them unchanged.
|
|
;;; ------------------------------------------------------------------
|
|
|
|
(check (let ((q (double-point (make-instance 'point :x 3 :y 4))))
|
|
(and (eql 6 (point-x q)) (eql 8 (point-y q)))))
|
|
(check (let ((p (shift-x (make-instance 'point :x 1))))
|
|
(eql 2 (point-x p))))
|
|
(check (let ((p (shift-x (make-instance 'point :x 10))))
|
|
(eql 11 (point-x p))))
|