{
Eons ago I'd posted on how I’d rewritten how I would solve FizzBuzz to have a more functional style (here is an imperative version), inspired by Tomas Petricek and John Skeet’s Functional Programming for the Real World. Shortly thereafter I had written a version in F#, I’m not sure what prevented me from posting it but here it is:
let numbers = [1..100]
let fizz num =
let res = if (num % 3 = 0) then "fizz" else ""
res
let buzz num =
let res = if (num % 5 = 0) then "buzz" else ""
res
let outform num fizCalc =
let res = if(fizCalc.ToString().Length > 0) then fizCalc.ToString() else num.ToString()
res
let rec printer nums =
match nums with
| [] -> printfn "-"
| h::t ->
printfn "%s" (outform h ((fizz h) + (buzz h)))
printer t
printer numbers
I can already see now areas that are still influenced by the C# - most notably the use of if rather than using match for everything. I will clean it up, hopefully demonstrating an increased facility with F#. If you are looking at the above and know F#, what are some idioms or language constructs that I am neglecting in the above code? Type annotations for one... what else?
No comments:
Post a Comment