関数の呼び出しがどういう順序で起こっているのか知りたい
任意の関数が「どういう関数をどういった順序で、どういう引数を使って呼び出しているか」を知りたいケースというのは存外に多い。
例えば以下のようなコードがあるとする。
code:clojure
(declare myeven? myodd?)
(if (zero? x) true (myodd? (dec x))))
(if (zero? x) false (myeven? (dec x))))
(case n
0 0
1 1
(+ (fibo-rec (dec n))
(fibo-rec (- n 2)))))
この状態で例えばC-c M-t nとすると、その後この関数が実行されるタイミングでトレースが行なわれる。
実際にこの状態で適当にREPLで評価してみると以下の出力を得ることができる。 code:clojure
user> (myeven? 10)
TRACE t12730: (user/myeven? 10)
TRACE t12731: | (user/myodd? 9)
TRACE t12732: | | (user/myeven? 8)
TRACE t12733: | | | (user/myodd? 7)
TRACE t12734: | | | | (user/myeven? 6)
TRACE t12735: | | | | | (user/myodd? 5)
TRACE t12736: | | | | | | (user/myeven? 4)
TRACE t12737: | | | | | | | (user/myodd? 3)
TRACE t12738: | | | | | | | | (user/myeven? 2)
TRACE t12739: | | | | | | | | | (user/myodd? 1)
TRACE t12740: | | | | | | | | | | (user/myeven? 0)
TRACE t12740: | | | | | | | | | | => true
TRACE t12739: | | | | | | | | | => true
TRACE t12738: | | | | | | | | => true
TRACE t12737: | | | | | | | => true
TRACE t12736: | | | | | | => true
TRACE t12735: | | | | | => true
TRACE t12734: | | | | => true
TRACE t12733: | | | => true
TRACE t12732: | | => true
TRACE t12731: | => true
TRACE t12730: => true
true
user> (fibo-rec 5)
TRACE t12743: (user/fibo-rec 5)
TRACE t12744: | (user/fibo-rec 4)
TRACE t12745: | | (user/fibo-rec 3)
TRACE t12746: | | | (user/fibo-rec 2)
TRACE t12747: | | | | (user/fibo-rec 1)
TRACE t12747: | | | | => 1
TRACE t12748: | | | | (user/fibo-rec 0)
TRACE t12748: | | | | => 0
TRACE t12746: | | | => 1
TRACE t12749: | | | (user/fibo-rec 1)
TRACE t12749: | | | => 1
TRACE t12745: | | => 2
TRACE t12750: | | (user/fibo-rec 2)
TRACE t12751: | | | (user/fibo-rec 1)
TRACE t12751: | | | => 1
TRACE t12752: | | | (user/fibo-rec 0)
TRACE t12752: | | | => 0
TRACE t12750: | | => 1
TRACE t12744: | => 3
TRACE t12753: | (user/fibo-rec 3)
TRACE t12754: | | (user/fibo-rec 2)
TRACE t12755: | | | (user/fibo-rec 1)
TRACE t12755: | | | => 1
TRACE t12756: | | | (user/fibo-rec 0)
TRACE t12756: | | | => 0
TRACE t12754: | | => 1
TRACE t12757: | | (user/fibo-rec 1)
TRACE t12757: | | => 1
TRACE t12753: | => 2
TRACE t12743: => 5
5