自从我接触到 Scheme 并决定使用 Scheme 实现命令行收入分区器已经有几个月了。
我最初的实现在延续上使用了简单的递归,但我认为延续会更适合这种类型的程序。如果有人(比我更精通 Scheme)可以看看这个并提出改进建议,我将不胜感激。我认为多(display...
行也是使用宏的理想机会(我还没有接触到宏)。
(define (ab-income)
(call/cc
(lambda (cc)
(let
((out (display "Income: "))
(income (string->number (read-line))))
(cond
((<= income 600)
(display (format "Please enter an amount greater than $600.00~n~n"))
(cc (ab-income)))
(else
(let
((bills (* (/ 30 100) income))
(taxes (* (/ 20 100) income))
(savings (* (/ 10 100) income))
(checking (* (/ 40 100) income)))
(display (format "~nDeduct for bills:---------------------- $~a~n" (real->decimal-string bills 2)))
(display (format "Deduct for taxes:---------------------- $~a~n" (real->decimal-string taxes 2)))
(display (format "Deduct for savings:-------------------- $~a~n" (real->decimal-string savings 2)))
(display (format "Remainder for checking:---------------- $~a~n" (real->decimal-string checking 2))))))))))
调用(ab-income)
要求输入,如果提供低于 600 的任何内容(据我的理解),它会(ab-income)
返回current-continuation
. 我的第一个实现(正如我之前所说的)使用纯简递归。这也不错,但我认为(ab-income)
如果值低于 600,每次返回调用都会不断扩展函数。
(如果这种担心不正确,请纠正我!)