I know almost nothing about haskell and I’m reading lots of tutorials and code samples to understand Haskell‘s IO, but it is still not clear to me.
All examples are like:
main = do
putStrLn "Hello, what's your name?"
name <- getLine
putStrLn ("Hey " ++ name ++ ", welcome !")
They read a string from IO and there are many other examples that do some customization on input that I don’t understand.
My question is simply: How to read Int, Float, Double or Char like using scanf in C with a format string?
And can anyone help me with a clear explanation to IO customization or tricks?
Hint: if the code comes with a main function that would
be great – it would make testing easier.
I have very good coding experience in C, C++, Java, C#, Matlab and PHP.
isturdy’s answer is valid, but in case you want something a bit simpler, the easiest approach is to do it step-by-step:
The type of
getLineisIO String– it reads input as aString. You can then change the type of this string withread(and indicating what type you want). As for ascanf-like experience, you can try something like this: