Added a tail recursive compound interest function
This commit is contained in:
parent
f76c587338
commit
fa45434228
|
@ -0,0 +1,49 @@
|
|||
(load "unit-test.lisp")
|
||||
(load "compound-interest.lisp")
|
||||
|
||||
(unit
|
||||
(list
|
||||
|
||||
(defun many-years-with-no-interest-rate ()
|
||||
(eq (compound-interest 100000 0 10) 100000)
|
||||
)
|
||||
|
||||
(defun no-years-with-positive-interest-rate ()
|
||||
(eq (compound-interest 100000 10 0) 100000)
|
||||
)
|
||||
|
||||
(defun one-year-with-positive-interest-rate ()
|
||||
(eq (compound-interest 100000 5 1) 105000)
|
||||
)
|
||||
|
||||
(defun two-years-with-positive-interest-rate ()
|
||||
(eq (compound-interest 100000 5 2) 110250)
|
||||
)
|
||||
|
||||
(defun three-years-with-positive-interest-rate ()
|
||||
(eq (compound-interest 100000 5 3) 115763)
|
||||
)
|
||||
|
||||
(defun four-years-with-positive-interest-rate ()
|
||||
(eq (compound-interest 100000 5 4) 121551)
|
||||
)
|
||||
|
||||
(defun one-year-with-negative-interest-rate ()
|
||||
(eq (compound-interest 100000 (- 5) 1) 95000)
|
||||
)
|
||||
|
||||
(defun two-years-with-negative-interest-rate ()
|
||||
(eq (compound-interest 100000 (- 5) 2) 90250)
|
||||
)
|
||||
|
||||
(defun three-years-with-negative-interest-rate ()
|
||||
(eq (compound-interest 100000 (- 5) 3) 85737)
|
||||
)
|
||||
|
||||
(defun four-years-with-negative-interest-rate ()
|
||||
(eq (compound-interest 100000 (- 5) 4) 81450)
|
||||
)
|
||||
|
||||
)
|
||||
)
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
(defun decrement (n) (- n 1))
|
||||
|
||||
(defun percent (n percent)
|
||||
(cond
|
||||
((> percent 0) (/ (+ (* n percent) 50) 100))
|
||||
(t (/ (- (* n percent) 50) 100))
|
||||
)
|
||||
)
|
||||
|
||||
(defun compound-interest (principal rate years)
|
||||
(cond
|
||||
((= years 0) principal)
|
||||
(t (compound-interest (+ principal (percent principal rate))
|
||||
rate
|
||||
(decrement years)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
Loading…
Reference in New Issue