A lisp function to compute compound interest and unit tests
This commit is contained in:
parent
ea57fc8bae
commit
f76c587338
|
@ -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)
|
||||
)
|
||||
|
||||
)
|
||||
)
|
||||
|
|
@ -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)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
|
@ -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)
|
||||
)
|
||||
)
|
||||
|
||||
|
|
Loading…
Reference in New Issue