Below is my code for building the collatz sequence so a given number. I’m trying to make the function tail recursive and I believe that this impelemtation is tail recursive but I’m not yet so comfortable with F# that I can convince myself 100%.
let collatzSeq n = let collatz m = match m%2 with | 0 -> m/2 | _ -> 3*m + 1 let rec loop (l,acc) = match l with | 1 -> acc | _ -> let l' = (collatz l) in loop (l',l'::acc) loop (n,[n]) |> List.rev [<EntryPoint>] let main argv = let col12 = collatzSeq 12 printfn "%A %d" col12 col12.Length 0 // return an integer exit code
My concern is does the let .. in loop construct stop this being tail recursive and if it does how do I improve the code to make it tail recursive?