I was inspired by this video by Matt Parker to use the Farey Sequence to convert a float into a string representation of a fraction, how did I do? How well did I follow F# workflow and patterns (I'm new to the language)? Can it be done better, or rather, how would a "professional" F# developer write this function? Thanks in advance!
let toFraction num = let rec farey n0 d0 n1 d1 = let n2 = n0 + n1 let d2 = d0 + d1 match (n2 / d2) with | x when abs (x - num) < 1e-5 -> string n2 + " / " + string d2 | x when x < num -> farey n2 d2 n1 d1 | x when x > num -> farey n0 d0 n2 d2 | _ -> "" // Compiler detects "incomplete pattern matches on this expression without this wildcard, // maybe this is where my method can be improved? farey 0. 1. 1. 1.
Additionally, this problem assumes the input
num is in the set
0 <= num < 1