diff --git a/lisp/compound-interest-test.lisp b/lisp/compound-interest-test.lisp new file mode 100644 index 0000000..e9f8de7 --- /dev/null +++ b/lisp/compound-interest-test.lisp @@ -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) + ) + + ) +) + diff --git a/lisp/compound-interest.lisp b/lisp/compound-interest.lisp new file mode 100644 index 0000000..61ca082 --- /dev/null +++ b/lisp/compound-interest.lisp @@ -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) + ) + ) + ) +)