;;; NUMBERS 4: TYPE DECLARATIONS ;;; ;;; DECLARE is a promise to the compiler - no check, just knowledge ;;; it may use for faster code: ;;; ;;; (defun twice (n) ;;; (declare (type integer n)) ;;; (* 2 n)) ;;; ;;; DECLAIM makes a global promise; THE wraps one expression: ;;; (the integer n). CHECK-TYPE is the live gate. It checks, and ;;; lies are TYPE-ERRORs: ;;; ;;; (check-type n integer) ;;; ;;; Compound types work too: (integer 0 100) is a whole number ;;; from 0 to 100. ;; TODO: Double N - but only when N really is an integer; other ;; values are type-errors. (defun counted-double (n) (check-type n string) (* 2 n)) ;; TODO: 100 minus N, for a whole N from 0 to 100 - anything ;; outside is a type-error. (defun shrink (n) (check-type n integer) (- 100 n)) ;;; ------------------------------------------------------------------ ;;; Checks. Read them - they tell you what to aim for - but leave ;;; them unchanged. ;;; ------------------------------------------------------------------ (check-equal 10 (counted-double 5)) (check-equal 0 (counted-double 0)) (check-error (counted-double "five") 'type-error) (check-equal 60 (shrink 40)) (check-equal 100 (shrink 0)) (check-error (shrink 200) 'type-error) (check-error (shrink -3) 'type-error)