This is the Utopian Tree in F# (from HackerRank)
The Utopian tree goes through 2 cycles of growth every year. The first growth cycle of the tree occurs during the monsoon, when it doubles in height. The second growth cycle of the tree occurs during the summer, when its height increases by 1 meter. Now, a new Utopian tree sapling is planted at the onset of the monsoon. Its height is 1 meter. Can you find the height of the tree after N growth cycles?
This helped me understand what to do:
//N = 0 ret initial height //N = 1 ret double height //N = 2 ret double height, +1 //N = 4 ret double, +1, double, + 1
Anywhere that I could improve this F# code? I'm trying to stay within the parameters of functional language programming. Per the doc on HackerRank, I had to take in n parameters, 0 being the number of test cases followed by the number of growth cycles:
[<EntryPoint>] let main argv = let tests = System.Int32.Parse(System.Console.ReadLine()) let cycles = [for i in 1..tests -> System.Int32.Parse(System.Console.ReadLine())] let even h = h * 2 let odd h = h + 1 let rec height acc cycle n = match cycle - cycle + n with | c when c = cycle -> acc | c when c % 2 = 0 -> height (even acc) cycle (n+1) | _ -> height (odd acc) cycle (n+1) cycles |> List.map (fun c -> height 1 c 0) |> List.map (fun x -> System.Console.WriteLine x) 0 // return an integer exit code
3 0 1 4 1 2 7