26 lines
784 B
Common Lisp
26 lines
784 B
Common Lisp
;;; LOOPS 4: LOOP'S OTHER CLAUSES (solution)
|
|
|
|
(defun doublings (n)
|
|
(loop repeat n
|
|
for x = 1 then (* x 2)
|
|
collect x))
|
|
|
|
(defun cart-until-cap (xs cap)
|
|
(loop with total = 0
|
|
for p in xs
|
|
until (>= (+ total p) cap)
|
|
do (setf total (+ total p))
|
|
collect p))
|
|
|
|
;;; ------------------------------------------------------------------
|
|
;;; Checks. Read them - they tell you what to aim for - but leave
|
|
;;; them unchanged.
|
|
;;; ------------------------------------------------------------------
|
|
|
|
(check-equal '(1 2 4 8) (doublings 4))
|
|
(check-equal '(1) (doublings 1))
|
|
(check-equal '() (doublings 0))
|
|
(check-equal '(3 5) (cart-until-cap '(3 5 2 8) 10))
|
|
(check-equal '() (cart-until-cap '(20 1) 10))
|
|
(check-equal '(1 2) (cart-until-cap '(1 2 3 4) 6))
|