0

I was reading the SICP and encountered with a problem, in chapter 1 there is an example named counting change, I need to write a program in scheme to calculate the possible number of ways to make a change of any given number given half-dollars, quarters, dimes, nickles and pennies. the book shows a substitution model of program and I tried to change it to a nesting one but failed, could anyone give me a favor?

(define (count_change total_amount)

    (define (denomination kinds_of_coins)
        (cond ((= kinds_of_coins 5) 50)
              ((= kinds_of_coins 4) 25)
              ((= kinds_of_coins 3) 10)
              ((= kinds_of_coins 2) 5)
              ((= kinds_of_coins 1) 1)))

    (define (cc amount kinds_of_coins)
        (cond (= amount 0) 1)
              ((or (< amount 0) (= kinds_of_coins 0)) 0)
              (else (+ (cc amount (- kinds_of_coins 1))
                       (cc (- amount (denomination kinds_of_coins)) kinds_of_coins))))

    (cc total_amount 5))

the execution result is as follows:

;Ill-formed clause: 1
  • You're misssing some parentheses - `(cond (= amount 0) 1)` is not a valid `cond`. (You have the correct syntax in `denomination`.) – molbdnilo Jan 24 '17 at 09:42

1 Answers1

1

A cond expression generally takes the form (cond (predicate expr) ... (else expr)) or (cond (predicate expr) ... (#t expr)). Whenever a predicate evaluates to true, the correspondent expression in the clause is the result of the conditional expression. If you do not have an else/#t predicate at the end and all of the predicates on the conditional expression are false, generally the expression returns a void value.

Here, you have a syntax error because you did not started a clause properly. So, instead of having (cond (= amount 0) 1) you should have (cond ((= amount 0) 1) ...).