|
|
|
|
|
by daxfohl
4536 days ago
|
|
You could use `Seq.scan` to get rid of the manual recursion: let n = 42.
Seq.initInfinite float
|> Seq.scan (fun x _ -> 0.5 * (x + n/x)) 1.0
|> Seq.pairwise
|> Seq.pick (fun (x, y) -> if abs <| x-y < 1E-10 then Some y else None)
|> printf "%f"
printf "%f" <| sqrt n
|
|
When I saw the original version in F# I thought "I think I can do better" after a long absence from using F#. When I got back to my laptop and searched for the to-be-improved snippet I saw yours.
Now I'm hooked again.