Files
lisplings/exercises/18_dynamic/dyn4.lisp
T

36 lines
1.1 KiB
Common Lisp

;;; DYNAMIC 4: DEFVAR VS DEFPARAMETER
;;;
;;; Both define a special variable. The difference shows when their
;;; form runs AGAIN - a reload, say:
;;;
;;; DEFPARAMETER rebinds: the new value wins.
;;; DEFVAR only fills an EMPTY variable: what is there stays.
;;;
;;; So DEFVAR suits state the code fills in over time; DEFPARAMETER
;;; suits a constant you might retune. The file below runs both
;;; forms twice, the way a reload would.
(defvar *cache* 10)
(defparameter *config* 10)
;; The program grows the cache and retunes the config by hand:
(setf *cache* 99)
(setf *config* 99)
;; Re-evaluating the file, as a reload would:
(defvar *cache* 10)
(defparameter *config* 10)
;; TODO: The pair of values you see NOW.
(defun current-values ()
(list 10 10))
;;; ------------------------------------------------------------------
;;; Checks. Read them - they tell you what to aim for - but leave
;;; them unchanged.
;;; ------------------------------------------------------------------
(check-equal '(99 10) (current-values))
(check-equal 99 *cache*)
(check-equal 10 *config*)