25 lines
685 B
Common Lisp
25 lines
685 B
Common Lisp
;;; LOOPS 5: DO AND DO* (solution)
|
|
|
|
(defun triangle-do (n)
|
|
(do ((i 1 (1+ i))
|
|
(total 0 (+ total i)))
|
|
((> i n) total)))
|
|
|
|
(defun factorial-of (n)
|
|
(do* ((a 1 (1+ a))
|
|
(b 1 (* b a)))
|
|
((>= a n) b)))
|
|
|
|
;;; ------------------------------------------------------------------
|
|
;;; Checks. Read them - they tell you what to aim for - but leave
|
|
;;; them unchanged.
|
|
;;; ------------------------------------------------------------------
|
|
|
|
(check-equal 6 (triangle-do 3))
|
|
(check-equal 55 (triangle-do 10))
|
|
(check-equal 0 (triangle-do 0))
|
|
(check-equal 1 (factorial-of 0))
|
|
(check-equal 1 (factorial-of 1))
|
|
(check-equal 6 (factorial-of 3))
|
|
(check-equal 24 (factorial-of 4))
|