45 lines
1.4 KiB
Common Lisp
45 lines
1.4 KiB
Common Lisp
;;; S-EXPRESSIONS 4: READER SHORTHAND
|
|
;;;
|
|
;;; The reader turns characters into objects before evaluation ever
|
|
;;; sees them. A few two-character shortcuts do real work:
|
|
;;;
|
|
;;; 'x (quote x) - the last exercise's trick
|
|
;;; #'f (function f) - the function itself
|
|
;;; #(...) a vector, read as one literal object
|
|
;;; #x10 hexadecimal 16
|
|
;;;
|
|
;;; The checks below ask with FUNCTIONP (T for a function object)
|
|
;;; and VECTORP (T for a vector); chapter 05 collects such
|
|
;;; questions. The DEFSTRUCT declares the POINT record - chapter
|
|
;;; 12 explains it, and here it only gives the reader a type to
|
|
;;; build.
|
|
|
|
(defstruct point x y)
|
|
|
|
;; TODO: The function that adds numbers - the function itself, not
|
|
;; its name.
|
|
(defun the-adder ()
|
|
'+)
|
|
|
|
;; TODO: The counting numbers 1, 2, 3 as ONE vector literal.
|
|
(defun counting ()
|
|
'(1 2 3))
|
|
|
|
;; TODO: The point at X 1, Y 2, read straight from text.
|
|
(defun origin ()
|
|
(list :x 1 :y 2))
|
|
|
|
;; TODO: The web color deadbeef - 3735928559 - in hex digits.
|
|
(defun magic-web-number ()
|
|
3735928558)
|
|
|
|
;;; ------------------------------------------------------------------
|
|
;;; Checks. Read them - they tell you what to aim for - but leave
|
|
;;; them unchanged.
|
|
;;; ------------------------------------------------------------------
|
|
|
|
(check (functionp (the-adder)))
|
|
(check (vectorp (counting)))
|
|
(check (point-p (origin)))
|
|
(check-equal 3735928559 (magic-web-number))
|