transcendental-lisp/lisp/finance/interest-compounder.lisp

50 lines
1.3 KiB
Common Lisp
Raw Normal View History

(load "../lang/dlambda.lisp")
(defun interest-compounder (initial-principal initial-interest-rate)
(let ((private) (public)
(principal initial-principal)
2017-03-07 11:37:27 -05:00
(interest-rate initial-interest-rate)
(years-passed 0))
2017-03-07 13:15:40 -05:00
(setq private
(eval
(dlambda
2017-03-07 11:37:27 -05:00
(:add-years (years)
(if (> years 0)
2017-03-07 13:15:40 -05:00
(setq years-passed (+ years-passed years))))
2017-03-07 11:37:27 -05:00
(:percent-of-number (n percentage)
(if (> percentage 0)
(/ (+ (* n percentage) 50) 100)
(/ (- (* n percentage) 50) 100)))
(:compound-interest (years)
(if (> years 0)
2017-03-07 13:15:40 -05:00
(begin
(setq principal
(+ principal
(call private :percent-of-number principal interest-rate)))
(call private :compound-interest (- years 1))))))))
2017-03-07 13:15:40 -05:00
(setq public
(eval
(dlambda
2017-03-07 11:37:27 -05:00
(:get-years-passed ()
years-passed)
(:get-principal ()
principal)
(:get-interest-rate ()
interest-rate)
2017-03-07 11:37:27 -05:00
(:set-interest-rate (new-interest-rate)
2017-03-07 13:15:40 -05:00
(setq interest-rate new-interest-rate))
(:move-forward-years (years)
2017-03-07 13:15:40 -05:00
(begin
2017-03-07 11:37:27 -05:00
(call private :add-years years)
(call private :compound-interest years))))))))