this is my first F#
program, very simple concept but took me a few minutes to get it. I'm new to functional programming so I'd appreciate if you could have a look at and comment the code.
Suggest changes or any code smells?
module FSharpFun.PrintSentenceInLines
open System
let printWord w = printfn "%s" w
let printEveryWord = Array.map printWord
let splitIntoWords (s:String) = s.Split ()
// Read a line and print each word in a new line
[<EntryPoint>]
let main _ =
Console.ReadLine () |> splitIntoWords |> printEveryWord |> ignore
0
Array.iter
instead ofArray.map
is a more appropriate here. Then we can eventually get rid of the last|> ignore
. Finally and less important, we could inlineprintEveryWord
becauseArray.iter printWord
reads well enough. \$\endgroup\$|>
ignore. Now that I've made the change this program looks even more basic, but that's ok. Baby steps :) Thanks a lot \$\endgroup\$Array.map
the result is another array, ofUnit
s, one per each word you printed. That array needs to beignore
-ed since it's a value and the F# compiler likes its values to be "attached" to something. Opting forArray.iter
gives you a singleUnit
as output, andUnit
is a sort of "no data value", so the compiler ignores it on its own. (This is, of course, very figurative, but it's "correct enough" to explain whyiter
is better, more correct even). And, as Romain said, just go with... |> Array.iter (fun w -> printfn $"%s{w}")
\$\endgroup\$