---
title: "换零钱"
author: "yangbb"
date: "2009-07-29T21:58:34Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2009-07-29T21:59:11Z"
id: 435788
url: "https://yangbb.iteye.com/blog/435788"
---

书是07年买的，看了开头两章就没有进展了，现在又拾起。
计划在这段时间好好学习一下。

第一章的实例换零钱，本来想能不能写出个迭代的实现，想了一天，没有想出来。

中间写了这么一个实现，还是递归的，运行缺出错，没有想明白错在哪里。


```scheme


(define (count-chg2 amount)
   (define (first-of-coins kinds)
    (cond ((= kinds 1) 1)
          ((= kinds 2) 2)
          ((= kinds 3) 5)
          ((= kinds 4) 10)
          ((= kinds 5) 20)
          ((= kinds 6) 50)
          ((= kinds 7) 100)
          ((= kinds 8) 200)
          ((= kinds 9) 500)
          ((= kinds 10) 1000)
          ((= kinds 11) 2000)
          ((= kinds 12) 5000)
          ((= kinds 13) 10000)
          ))
  (define (dec-amount amount kinds)
    (- amount (first-of-coins kinds)))
  (define (dec-kinds kinds)
    (- kinds 1))
  (define (inc-amount amount kinds)
    (+ amount (first-of-coins kinds)))
  (define (inc-kinds kinds)
    (+ kinds 1))
  (define (cc-iter amount kinds count)
    (cond ((= 0 amount) (+ 1 count))
          ((or (< amount 0) (< kinds 0)) count)
          (else 
           (+ (cc-iter (dec-amount amount kinds) kinds count) (cc-iter amount (dec-kinds kinds) count))
           )
     )
   )
  (cc-iter amount 4 0)
 )

```


在else的地方，运行报错
-: expects type <number> as 2nd argument, given: #<void>; other arguments were: 1

现在还不明白怎么解决，先记下来，看到后面再回头来看