5

自从我接触到 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,每次返回调用都会不断扩展函数。

(如果这种担心不正确,请纠正我!)

4

1 回答 1

17

首先,你不需要继续。按照标准,Scheme 会一直进行尾调用优化。尾调用是一个函数调用,它位于函数的最后位置;运行该呼叫后,不会发生任何其他事情。在这种情况下,我们不需要保留我们当前所在的激活记录;一旦我们调用的函数返回,我们就会弹出它。因此,尾调用会重用当前的激活记录。例如,考虑一下:

(define (some-function x y)
  (preprocess x)
  (combine (modified x) y))
(some-function alpha beta)

当我们调用 时some-function,我们为其在堆栈上的激活记录分配空间:局部变量、参数等。然后我们调用(preprocess x). 由于我们需要返回some-function并继续处理,我们必须保留some-function的激活记录,因此我们在 for 上推送新的激活记录preprocess。一旦返回,我们 poppreprocess的堆栈帧并继续前进。接下来,我们需要评估modified; 同样的事情必须发生,当modified返回时,它的结果被传递给combine. 有人会认为我们需要创建一个新的激活记录,运行combine,然后将其返回给some-function——但some-function不需要对结果做任何事情,只需返回它!因此,我们覆盖了当前的激活记录,但不理会返回地址;什么时候combine返回,然后,它将其值返回到等待它的确切值。这里(combine (modified x) y)是一个尾调用,评估它不需要额外的激活记录。

这是在 Scheme 中实现循环的方法,例如:

(define (my-while cond body)
  (when (cond)
    (body)
    (my-while cond body)))

(let ((i 0))
  (my-while (lambda () (< i 10))
            (lambda () (display i) (newline) (set! i (+ i 1)))))

如果没有尾调用优化,这将是低效的,并且可能会因长时间运行的循环而溢出,从而构建大量对my-while. 然而,由于尾调用优化,递归调用my-while cond body是一个跳转,并且不分配内存,使其与迭代一样高效。

其次,这里不需要任何宏。虽然您可以抽象出display块,但您可以使用普通函数来做到这一点。宏允许你在某种程度上改变语言的语法——添加你自己的类型define,实现一些不评估其所有分支的类型案例构造,等等。当然,它仍然是 s 表达式,但是语义不再只是“评估参数并调用函数”。然而,这里只需要函数调用语义。

话虽如此,我认为这就是我实现代码的方式:

(require (lib "string.ss"))

(define (print-report width . nvs)
  (if (null? nvs)
    (void)
    (let ((name  (car  nvs))
          (value (cadr nvs)))
      (display (format "~a:~a $~a~n"
                       name
                       (make-string (- width (string-length name) 2) #\-)
                       (real->decimal-string value 2)))
      (apply print-report width (cddr nvs)))))

(define (ab-income)
  (display "Income: ")
  (let ((income (string->number (read-line))))
    (if (or (not income) (<= income 600)) 
      (begin (display "Please enter an amount greater than $600.00\n\n")
             (ab-income))
      (begin (newline)
             (print-report 40 "Deduct for bills"       (* 3/10 income)
                              "Deduct for taxes"       (* 2/10 income)
                              "Deduct for savings"     (* 1/10 income)
                              "Remainder for checking" (* 4/10 income))))))

首先,至少在我的版本中mzscheme,我需要(require (lib "string.ss"))一行来导入real->decimal-string。接下来,我抽象出display你所说的块。我们看到的是,每一行都想在第 40 列以相同的格式打印钱,打印一个标签名称和前面的一行破折号。因此,我写了print-report. 第一个参数是初始宽度;在这种情况下,40。其余参数是字段值对。从宽度中减去每个字段的长度(冒号和空格加上两个),我们生成一个由那么多破折号组成的字符串。我们使用format以正确的顺序排列字段,并display打印字符串。该函数对所有对进行递归(使用尾递归,因此我们不会破坏堆栈)。

在 main 函数中,我将 ; 移到(display "Income: ")let; 你忽略了它的结果,那么为什么将它分配给一个变量呢?然后我增加了if条件以测试是否input为假,这在string->number无法解析输入时发生。最后,我删除了你的局部变量,因为你所做的只是打印它们,并使用 Scheme 的分数语法而不是除法。(当然,我使用print-report而不是displays 和formats。)

我认为仅此而已;如果您对我所做的事情有任何其他问题,请随时提问。

于 2010-04-17T18:44:39.457 回答