Currency: Difference between revisions

Content added Content deleted
imported>GoulashAmateur
(Add Common Lisp version)
imported>GoulashAmateur
m (Common Lisp syntax tweaks)
Line 554: Line 554:


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
Let us just use the built-in and convenient rationals (which uses two bignums for numerator and denominator).
Let us just use the built-in and convenient rationals (which use two bignums for numerator and denominator).


<syntaxhighlight lang="lisp">(defun print-$ (rat &key (prefix "") (stream t))
<syntaxhighlight lang="lisp">(defun print-$ (rat &key (prefix "") (stream t))
Line 562: Line 562:
(defun compute-check (order-alist tax-rate)
(defun compute-check (order-alist tax-rate)
(let* ((total-before-tax
(let* ((total-before-tax
(loop for (amount . price) in order-alist
(loop :for (amount . price) in order-alist
sum (* (rationalize price) amount)))
:sum (* (rationalize price) amount)))
(tax (* (rationalize tax-rate) total-before-tax)))
(tax (* (rationalize tax-rate) total-before-tax)))
(print-$ total-before-tax :prefix "Total before tax: ")
(print-$ total-before-tax :prefix "Total before tax: ")
Line 599: Line 599:
(defun compute-check (order-alist tax-rate)
(defun compute-check (order-alist tax-rate)
(let* ((total-before-tax
(let* ((total-before-tax
(loop for (amount . price) in order-alist
(loop :for (amount . price) in order-alist
sum (* price amount)))
:sum (* price amount)))
(tax (* (rationalize tax-rate) total-before-tax)))
(tax (* (rationalize tax-rate) total-before-tax)))
(print-$ total-before-tax :prefix "Total before tax: ")
(print-$ total-before-tax :prefix "Total before tax: ")