29 lines
1.1 KiB
Common Lisp
29 lines
1.1 KiB
Common Lisp
;;; FUNCTIONS 5: PRODUCING VALUES
|
|
;;;
|
|
;;; FUNCTIONS 4 let values ride along by passing FLOOR through. This
|
|
;;; time you MAKE them: (values a b) returns two values at once.
|
|
;;;
|
|
;;; MOD gives a remainder: (mod 265 100) => 65. FLOOR gives the
|
|
;;; whole part: (floor 265 100) => 2.
|
|
;;;
|
|
;;; The checks gather the values with MULTIPLE-VALUE-LIST again.
|
|
|
|
;; TODO: CENTS as two values: whole DOLLARS and leftover cents.
|
|
(defun split-money (cents)
|
|
(list (floor cents 100) (mod cents 100)))
|
|
|
|
;; TODO: N as two values: half of it, and double it. (/ 7 2) is the
|
|
;; exact rational 7/2 - fine.
|
|
(defun half-and-double (n)
|
|
(* n 2))
|
|
|
|
;;; ------------------------------------------------------------------
|
|
;;; Checks. Read them - they tell you what to aim for - but leave
|
|
;;; them unchanged.
|
|
;;; ------------------------------------------------------------------
|
|
|
|
(check-equal '(2 65) (multiple-value-list (split-money 265)))
|
|
(check-equal '(0 5) (multiple-value-list (split-money 5)))
|
|
(check-equal '(3 12) (multiple-value-list (half-and-double 6)))
|
|
(check-equal '(7/2 14) (multiple-value-list (half-and-double 7)))
|