Files
lisplings/exercises/01_sexps/sexps2.lisp
T
venomade 4df60ead93 Lisplings: Rustlings-style exercises for Common Lisp
70 exercises in 20 chapters, from first s-expressions to restarts,
macros, CLOS, and a tiny Lisp interpreter. Each exercise ships
broken with a solved twin and a hint. Includes a watch-mode runner,
a plain-CL check harness, a nix flake, and verification tooling.
All plain SBCL, no dependencies.
2026-11-22 03:30:19 +00:00

28 lines
826 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.
;; 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))