26 lines
989 B
Common Lisp
26 lines
989 B
Common Lisp
;;; FUNCTIONS 4: MULTIPLE VALUES
|
|
;;;
|
|
;;; A function may return several values at once:
|
|
;;; (values 1 2) => 1 and 2
|
|
;;; Everything except the first value rides along, waiting for
|
|
;;; MULTIPLE-VALUE-BIND or NTH-VALUE to pick it up. The checks
|
|
;;; below gather the values with MULTIPLE-VALUE-LIST, turning them
|
|
;;; into a list. FLOOR is a classic: (floor 10 3) returns 3 AND 1.
|
|
|
|
;; TODO: Return TWO values: the whole part of A/B and the rest.
|
|
(defun divide (a b)
|
|
(/ a b))
|
|
|
|
;; TODO: Return TWO values: whole minutes, and leftover seconds.
|
|
(defun split-seconds (seconds)
|
|
seconds)
|
|
|
|
;;; ------------------------------------------------------------------
|
|
;;; Checks. Read them - they tell you what to aim for - but leave
|
|
;;; them unchanged.
|
|
;;; ------------------------------------------------------------------
|
|
|
|
(check-equal '(3 1) (multiple-value-list (divide 10 3)))
|
|
(check-equal '(3 1) (multiple-value-list (divide 7 2)))
|
|
(check-equal '(2 5) (multiple-value-list (split-seconds 125)))
|