A lisp function to compute compound interest and unit tests

This commit is contained in:
Mike Cifelli 2017-02-14 15:24:38 -05:00
parent ea57fc8bae
commit f76c587338
3 changed files with 49 additions and 2 deletions

33
lisp/compound-test.lisp Normal file
View File

@ -0,0 +1,33 @@
(load "unit-test.lisp")
(load "compound.lisp")
(unit
(list
(defun many-years-with-no-interest-rate ()
(eq (compound 100000 0 10) 100000)
)
(defun no-years-with-positive-interest-rate ()
(eq (compound 100000 10 0) 100000)
)
(defun one-year-with-positive-interest-rate ()
(eq (compound 100000 5 1) 105000)
)
(defun two-years-with-positive-interest-rate ()
(eq (compound 100000 5 2) 110250)
)
(defun three-years-with-positive-interest-rate ()
(eq (compound 100000 5 3) 115763)
)
(defun four-years-with-positive-interest-rate ()
(eq (compound 100000 5 4) 121551)
)
)
)

14
lisp/compound.lisp Normal file
View File

@ -0,0 +1,14 @@
(defun compound (principal interest-rate years)
(cond
((= years 0) principal)
(t (let ( (previous-principal (compound principal interest-rate (- years 1))) )
(+ previous-principal
(/ (+ (* previous-principal
interest-rate)
50)
100)
)
)
)
)
)

View File

@ -1,7 +1,7 @@
(defun run-unit-test (unit-test)
(cond
((funcall unit-test) (print (cons (symbol-function unit-test) T)) T)
(T (print (cons (symbol-function unit-test) 'F)) NIL)
((funcall unit-test) (print (cons T unit-test)) T)
(T (print (cons 'F unit-test)) NIL)
)
)