31 lines
909 B
Common Lisp
31 lines
909 B
Common Lisp
;;; S-EXPRESSIONS 2: NESTING AND EXACT MATH
|
|
;;;
|
|
;;; Inner expressions evaluate first: (* 2 (+ 3 4)) => (* 2 7) => 14.
|
|
;;;
|
|
;;; A Lisp delight: division is exact. (/ 1 2) is the rational 1/2,
|
|
;;; not 0.5. Integers, rationals and floats form one number tower.
|
|
;;;
|
|
;;; (defun name () ...) just names the expression; chapter 02
|
|
;;; explains it.
|
|
|
|
;; TODO: (2 + 3) multiplied by (4 - 1). Should be 15.
|
|
(defun calc-a ()
|
|
(+ (* 2 3) (- 4 1)))
|
|
|
|
;; TODO: 100 divided by the sum of 2 and 3. Should be 20.
|
|
(defun calc-b ()
|
|
(/ 100 2))
|
|
|
|
;; TODO: One half, as an exact rational.
|
|
(defun one-half ()
|
|
(/ 1 2.0))
|
|
|
|
;;; ------------------------------------------------------------------
|
|
;;; Checks. Read them - they tell you what to aim for - but leave
|
|
;;; them unchanged.
|
|
;;; ------------------------------------------------------------------
|
|
|
|
(check-equal 15 (calc-a))
|
|
(check-equal 20 (calc-b))
|
|
(check-equal 1/2 (one-half))
|