(def throws 10)
(defn r-squared [x y]
(+ (* (- 0.5 x) (- 0.5 x))
(* (- 0.5 y) (- 0.5 y))))
(loop [hits 0]
(let [x (rand)
y (rand)]
; still inside the let
(if (< (r-squared x y) 0.25) ;is it a hit or not? if not, try again
(recur (inc hits))
(* 4 (/ hits throws)))))
我得到了代码工作和运行,直到if条件为真。我该如何重写它,以便将X作为参数并运行X次?把clojure代码放在一个循环中
我基本上想打电话给(r-squared 100),并得到多少点击作为返回值。
我怎么及时把这个包来获得exeuction时间? – peter 2010-09-19 22:56:02
包装你的电话及时测试。 '(时间(测试100))' – Rayne 2010-09-19 23:05:41
代码永远不会改变n的值。这相当于海报初始代码,对于任何大于0的n。 – 2010-09-20 09:52:34