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

52 lines
1.4 KiB
Common Lisp
Raw Normal View History

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