|
|
|
|
|
by null_driver
1327 days ago
|
|
Ocaml also has `@@` which gives `'a -> ('a -> 'b) -> 'b = <fun>` and `|>` for `('a -> 'b) -> 'a -> 'b = <fun>` so you can also do: ```ocaml type point = { x: float; y: float};;
let euclidean a a' =
let xd = (a'.x -. a.x) in
let yd = (a'.y -. a.y) in
sqrt ((xd*.xd) +. (yd*.yd))
;;
let is_in_unit_circle x =
x <= 1.
let quadsolve iterations hits =
((4. *. hits) /. Float.of_int iterations)
let estimate iters =
let origin = { x = 0.; y = 0. } in
Array.init iters (fun _ -> { x = (Random.float 1.); y = (Random.float 1.) })
|> Array.map (euclidean origin)
|> Array.map is_in_unit_circle
|> Array.fold_left (fun x y -> if y then x +. 1. else x ) 0.
|> quadsolve iters
;;``` utop # estimate 1000000;;
- : float = 3.141432
Edit: formatting |
|