无法运行"计数变化" SICP中的代码

时间:2017-01-21 04:02:36

标签: scheme lisp sicp

我正在阅读SICP,第一章提到的代码如下:

#lang scheme
(define (count-change amount)
  (cc amount 5))

(define (cc amount kind-of-coins)
  (cond ((= amount 0) 1)
        ((or (< amount 0) (= kind-of-coins 0)) 0)
        (else (+ (cc amount
                     (- kind-of-coins 1))
                 (cc (- amount
                        (first-denomination kind-of-coins))
                     kind-of-coins)))))

(define (first-denomination kinds-of-icons)
  (cond ((= kinds-of-icons) 1)
        ((= kinds-of-icons) 5)
        ((= kinds-of-icons) 10)
        ((= kinds-of-icons) 25)
        ((= kinds-of-icons) 50)))

(count-change 100)

我将代码输入DrRacket,但我无法成功运行,我已经尝试了一小时,不知道什么是错的。

enter image description here

1 个答案:

答案 0 :(得分:2)

您遇到first-denominations问题,=需要两个操作数导致arity错误。所以,它应该看起来像

(define (first-denomination kinds-of-icons)
  (cond ((= kinds-of-icons 1) 1)
        ...)