2

Clojure Overtone ライブラリの関数については、metronome私が調べたすべての例で次のように使用されているようです: (例はhttps://github.com/overtone/overtone/wiki/Live-codingから取得)

(defn player [beat]
  (at (metro beat) (kick))
  (at (metro (+ 0.5 beat)) (c-hat))
  (apply-by (metro (inc beat)) #'player (inc beat) []))

(player (metro))

(コンテキスト: metro はメトロノームのインスタンスです。kick と c-hat はサウンドを再生します) ご覧のとおり、再帰は関数自体を呼び出すことによって処理されます。倍音に関する記事は別として、Clojure の他のほとんどの記事では、このタイプの再帰を推奨せず、効率を高めるために recur 関数を使用することを推奨しています。だから私の質問は:上記の関数を書くより良い方法はありますか?

ありがとう、ナイル

4

1 に答える 1

2

As far as I can see, this isn't really recursion. Instead, evaluation of the player function causes, as a side effect, a future evaluation of the function in the #'player var to be scheduled. The return value of this evaluation doesn't depend on the next evaluation, and each evaluation unspools from the stack before the next one starts.
So there isn't actually a stack of self calls there that recur could collapse for us. Every call after the first comes from the same scheduler functions. If you did use recur then you would lose the ability to re-bind the var to different functions for live coding, so in the framework this seems to be the most versatile way of writing it.

于 2015-02-17T08:55:58.310 に答える