[Haskell-beginners] Couldn't match expected type ‘IO ()’ with actual type [Integer]

Marcin Mrotek marcin.jan.mrotek at gmail.com
Thu Feb 5 16:29:08 UTC 2015


Well, toDigits is always finishing its work after dividing the input number
once, and always returns lists of at most one element. You need it to call
itself recursively until it's done, and append the calculated digit to the
result:

toDigits :: Integer -> [Integer]
toDigits n
  | n <= 0 = []
  | otherwise = n `mod` 10 : toDigits (n `div` 10)

The <= is necessary, otherwise the function would loop infinitely on 0.
This version will print the digits in reverse, so you might move the code
to a local function in toDigits, and make toDigit call it and then reverse
the result.

I'd strongly recommend reading a Haskell tutorial (for example
http://learnyouahaskell.com ) to learn the basics.

Kind regards,
Marcin Mrotek
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20150205/86184ea4/attachment.html>


More information about the Beginners mailing list