[Haskell-beginners] function not working as expected, type issues
sickhadas at gmail.com
Wed Apr 9 06:03:54 UTC 2014
K, so I have a function that I’m writing that takes a *string* and a *count* and prints the *string* to STDOUT *count* times:
displayD :: IO () -> String -> Int
displayD string count = do
(count > 0) && hPutStr stdout string
(count > 0) && displayD string (count - 1)
However, when I try to compile the file, I get several errors; here’s a pastebin: http://pastebin.com/DEsuAvfz
What I’m trying to do here is check to see if *count* is greater than 0, and then if it is, then I’d like to print *string* to STDOUT until *count* equals 0. I tried doing it via pattern matching, like so:
displayD string (count > 0) = do
But I haven’t seen any examples of how to do pattern matching in functions using *do*, that is *IO*, so I’m unsure if the above is even anywhere near correct.
Still very uncomfortable with declaring function types: I have a hard time figuring out which type is returned and what type is expected as an arg. My initial guess is that the *first* type specified is the one returned, but I’m not sure.
Sent with Airmail
More information about the Beginners