Search code examples
schemelispsicp

Cann't run "count-change" code in SICP


I'm reading SICP, and in chapter one mentioned code like below:

#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)

I typed the code to DrRacket, but I can't run it successfully, I have been tried for ont hour, don't know what's wrong.

enter image description here


Solution

  • You've got a problem with first-denominations, the = needs two operands resulting in the arity error. So, it should look like

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