トレースの仕方

DrSchemeでトレースの仕方がわからなかったので調べてみた。
http://d.hatena.ne.jp/chelan/20090508/1241832039

こんな風に使うらしい

(require (lib "trace.ss"))
(trace トレースしたい関数)

問題1.14ならこんな感じ。

(define (count-change amount)
  (cc amount 5))

(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
                        (first-denomination kinds-of-coins))
                     kinds-of-coins)))))

(define (first-denomination kinds-of-coins)
  (cond ((= kinds-of-coins 1) 1)
        ((= kinds-of-coins 2) 5)
        ((= kinds-of-coins 3) 10)
        ((= kinds-of-coins 4) 25)
        ((= kinds-of-coins 5) 50)))

(require (lib "trace.ss"))
(trace cc)

(count-change 11)


こんな感じで出力される。

Welcome to DrScheme, version 4.2.5 [3m].
Language: CustomSwindle; memory limit: 512 MB.
>(cc 11 5)
> (cc 11 4)
> >(cc 11 3)
> > (cc 11 2)
> > >(cc 11 1)
> > > (cc 11 0)
< < < 0
> > > (cc 10 1)
> > > >(cc 10 0)
< < < <0
> > > >(cc 9 1)
> > > > (cc 9 0)
< < < < 0
> > > > (cc 8 1)
> > > > >(cc 8 0)
< < < < <0
> > > > >(cc 7 1)
> > > > > (cc 7 0)
< < < < < 0
> > > > > (cc 6 1)
> > > >[10] (cc 6 0)
< < < <[10] 0
> > > >[10] (cc 5 1)
> > > >[11] (cc 5 0)
< < < <[11] 0
> > > >[11] (cc 4 1)
> > > >[12] (cc 4 0)
< < < <[12] 0
> > > >[12] (cc 3 1)
> > > >[13] (cc 3 0)
< < < <[13] 0
> > > >[13] (cc 2 1)
> > > >[14] (cc 2 0)
< < < <[14] 0
> > > >[14] (cc 1 1)
> > > >[15] (cc 1 0)
< < < <[15] 0
> > > >[15] (cc 0 1)
< < < <[15] 1
< < < <[14] 1
< < < <[13] 1
< < < <[12] 1
< < < <[11] 1
< < < <[10] 1
< < < < < 1
< < < < <1
< < < < 1
< < < <1
< < < 1
< < <1
> > >(cc 6 2)
> > > (cc 6 1)
> > > >(cc 6 0)
< < < <0
> > > >(cc 5 1)
> > > > (cc 5 0)
< < < < 0
> > > > (cc 4 1)
> > > > >(cc 4 0)
< < < < <0
> > > > >(cc 3 1)
> > > > > (cc 3 0)
< < < < < 0
> > > > > (cc 2 1)
> > > >[10] (cc 2 0)
< < < <[10] 0
> > > >[10] (cc 1 1)
> > > >[11] (cc 1 0)
< < < <[11] 0
> > > >[11] (cc 0 1)
< < < <[11] 1
< < < <[10] 1
< < < < < 1
< < < < <1
< < < < 1
< < < <1
< < < 1
> > > (cc 1 2)
> > > >(cc 1 1)
> > > > (cc 1 0)
< < < < 0
> > > > (cc 0 1)
< < < < 1
< < < <1
> > > >(cc -4 2)
< < < <0
< < < 1
< < <2
< < 3
> > (cc 1 3)
> > >(cc 1 2)
> > > (cc 1 1)
> > > >(cc 1 0)
< < < <0
> > > >(cc 0 1)
< < < <1
< < < 1
> > > (cc -4 2)
< < < 0
< < <1
> > >(cc -9 3)
< < <0
< < 1
< <4
> >(cc -14 4)
< <0
< 4
> (cc -39 5)
< 0
<4
4
> 

なんだか、木構造になのがわかる。。。気がするw
最初っからこれ使えばよかったなぁ