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

Norbert Melzer timmelzer at gmail.com
Thu Feb 5 17:16:37 UTC 2015


Am 05.02.2015 17:29 schrieb "Marcin Mrotek" <marcin.jan.mrotek at gmail.com>:
>
> 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)

This doesn't feel right:

toDigits 10 = 0 : toDigits 1 = 0 : 1 : toDigits 0 = 0 : 1 : [] = [0,1]

I'd expect the result to be [1,0]...

>
> 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
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20150205/3032db7c/attachment.html>


More information about the Beginners mailing list