Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将clojure代码放入循环中 (def抛出10) (定义r平方[xy] (+(*(-0.5倍)(-0.5倍)) (*(-0.5 y)(-0.5 y))) (循环[点击0] (让[x(兰特) y(兰特)] ;仍在出租房内 (如果(_Clojure - Fatal编程技术网

将clojure代码放入循环中 (def抛出10) (定义r平方[xy] (+(*(-0.5倍)(-0.5倍)) (*(-0.5 y)(-0.5 y))) (循环[点击0] (让[x(兰特) y(兰特)] ;仍在出租房内 (如果(

将clojure代码放入循环中 (def抛出10) (定义r平方[xy] (+(*(-0.5倍)(-0.5倍)) (*(-0.5 y)(-0.5 y))) (循环[点击0] (让[x(兰特) y(兰特)] ;仍在出租房内 (如果(,clojure,Clojure,我让代码工作并运行,直到if条件为真。如何重写它,使其以X为参数并运行X次 我基本上想调用(r平方100)并得到我得到的点击次数作为返回值。如果正确理解这个问题,我想这就是你们想要的 (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)] ; st

我让代码工作并运行,直到if条件为真。如何重写它,使其以X为参数并运行X次


我基本上想调用(r平方100)并得到我得到的点击次数作为返回值。

如果正确理解这个问题,我想这就是你们想要的

(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)))))  
(defn测试[n]
(循环[点击0 n]
(让[x(兰特)
y(兰特)]
(如果(
没有评估它,所以parn可能有点错误

(defn test [n]
  (loop [hits 0 n n]
    (let [x (rand)
          y (rand)]
      (if (< n 0)
          hits ;// you can put (* 4 (/ hits throws)) here
          (if (< (r-squared x y) 0.25)
              (recur (inc hits) (dec n))
              (recur hits (dec n)))))))
(def抛出10)
(定义r平方[xy]
(+(*(-0.5倍)(-0.5倍))
(*(-0.5 y)(-0.5 y)))
(defn测试-r平方[n]
(循环[点击次数(整数0)n(整数n)]
(让[x(兰特)
y(兰特)]
;仍在出租房内
(如果(
(定义r平方[xy]
(+(*(-0.5倍)(-0.5倍))
(*(-0.5 y)(-0.5 y)))
(defn hit[]
(让[x(兰特)y(兰特)]
(<(r平方x y)0.25)))
(频率(针对[i(范围1000)](命中));{真787,假213}

我如何及时包装这个以获得执行时间?及时包装您的测试调用<代码>(时间(测试100))
代码从不更改n的值。对于任何大于0的n,它都相当于海报的初始代码。
(def throws 10)

(defn r-squared [x y] 
 (+ (* (- 0.5 x) (- 0.5 x)) 
    (* (- 0.5 y) (- 0.5 y))))


 (defn test-r-squared [n] 
  (loop [hits (int 0) n (int n)]
   (let [x (rand)
         y (rand)]
    ; still inside the let
    (if (< n 0)
       (* 4 (/ hits throws))
       (if (< (r-squared x y) 0.25) ;is it a hit or not? if not, try again
         (recur (inc hits) (dec n)) 
         (recur hits (dec n)))))))  
(defn r-squared [x y] 
 (+ (* (- 0.5 x) (- 0.5 x)) 
    (* (- 0.5 y) (- 0.5 y))))

(defn hit[]
  (let [x (rand) y (rand)]
    (< (r-squared x y) 0.25)))


(frequencies (for [i (range 1000)] (hit))) ; {true 787, false 213}