Guest User

Untitled

a guest
Sep 19th, 2010
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.70 KB | None | 0 0
  1. (def throws 10)
  2.  
  3. (defn r-squared [x y]
  4. (+ (* (- 0.5 x) (- 0.5 x))
  5. (* (- 0.5 y) (- 0.5 y))))
  6.  
  7. (loop [hits 0]
  8. (let [x (rand)
  9. y (rand)]
  10. ; still inside the let
  11. (if (< (r-squared x y) 0.25)
  12. (recur (inc hits)) ; then expression, restarts the loop
  13. (* 4 (/ hits throws))))) ; else-expression, the return value of the loop
  14.  
  15. ; compare
  16. (defn calculate-pi [hits]
  17. (let [x (rand)
  18. y (rand)]
  19. ; still inside the let
  20. (if (< (r-squared x y) 0.25)
  21. (recur (inc hits)) ; then expression, restarts the function
  22. (* 4 (/ hits throws))))) ; else-expression, the return value of the function
  23.  
  24. (println (calculate-pi 0)) ; probably not working :P
Advertisement
Add Comment
Please, Sign In to add comment