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

Hi Roelof,
On Thu, 05 Feb 2015 12:37:12 +0100
Roelof Wobben
main :: IO () main = do toDigits 123
src/Main.hs@14:5-14:17 Couldn't match expected type ‘IO ()’ with actual type [Integer] … In a stmt of a 'do' block: toDigits 123 In the expression: do { toDigits 123 }
since your do block consists only of one expression, actually your main looks like: main :: IO () main = toDigits 123 Perhaps this explains where the problem lies. Considering, however, do-notation: The last statement of a do-block needs to be an expression of the type of the do-block. Translating do-notation to bind operator sequences[1] may be helpful in understanding this. best, Max [1] http://en.wikibooks.org/wiki/Haskell/do_notation

Max Voit schreef op 5-2-2015 om 13:01:
Hi Roelof,
On Thu, 05 Feb 2015 12:37:12 +0100 Roelof Wobben
wrote: main :: IO () main = do toDigits 123 src/Main.hs@14:5-14:17 Couldn't match expected type ‘IO ()’ with actual type [Integer] … In a stmt of a 'do' block: toDigits 123 In the expression: do { toDigits 123 } since your do block consists only of one expression, actually your main looks like:
main :: IO () main = toDigits 123
Perhaps this explains where the problem lies.
Considering, however, do-notation: The last statement of a do-block needs to be an expression of the type of the do-block. Translating do-notation to bind operator sequences[1] may be helpful in understanding this.
best, Max
[1] http://en.wikibooks.org/wiki/Haskell/do_notation _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
Thanks, Another question : how can I display the array ? putStrln works only for strings, Roelof

On Thu, 05 Feb 2015 13:25:16 +0100
Roelof Wobben
Another question : how can I display the array ?
putStrln works only for strings,
Yep, so you need to convert it to a string and putStrLn that. Kindly enough typing ":i []" into ghci tells you, that lists have an instance for "show". Anonther option would be to convert every single list element to a string and putStrLn that. "map" comes in handy here (or mapM_ if you directly want to use print instead of putStrLn . show). best, Max

Max Voit schreef op 5-2-2015 om 13:35:
On Thu, 05 Feb 2015 13:25:16 +0100 Roelof Wobben
wrote: Another question : how can I display the array ?
putStrln works only for strings, Yep, so you need to convert it to a string and putStrLn that. Kindly enough typing ":i []" into ghci tells you, that lists have an instance for "show".
Anonther option would be to convert every single list element to a string and putStrLn that. "map" comes in handy here (or mapM_ if you directly want to use print instead of putStrLn . show).
best, Max _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
Thanks that part worked fine. One part I cannot figure out is how to make this : -- | Main entry point to the application. module Main where -- | convert a number to a array in pieces where a negative number will be a empty array. toDigits :: Integer -> [Integer] toDigits n | n < 0 = [] | otherwise = n `mod` 10 : [] -- | The main entry point. main :: IO () main = do print $ toDigits 123 Now it showing only [1] or [23] I have to find out how I can do something like this toDigits 23 [1] But I cannot do this that way because it has to be Integer -> [Integer] Who has a tip for me ? Roelof

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

Am 05.02.2015 17:29 schrieb "Marcin Mrotek"
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@haskell.org http://www.haskell.org/mailman/listinfo/beginners

@Norbert Melzer - Yeah, I just wanted to provide a solution without shuffling Roelof's code too much, so he'd recognize how is it different from his version. I did write that this is incomplete and requires the list to be reversed later. @Roelof Wobben - Maybe try a different one? There are several popular choices other than LYAH: * FP Complete Shool of Haskellhttps://www.fpcomplete.com/school * Wikibooks: http://en.wikibooks.org/wiki/Haskell * Real World Haskell: http://book.realworldhaskell.org (Warning: while I remember learning from this book some time ago and it was fine, every now and then I see people saying that it's outdated and some examples don't compile) Kind regards, Marcin Mrotek

Marcin Mrotek schreef op 5-2-2015 om 21:59:
@Norbert Melzer - Yeah, I just wanted to provide a solution without shuffling Roelof's code too much, so he'd recognize how is it different from his version. I did write that this is incomplete and requires the list to be reversed later.
@Roelof Wobben - Maybe try a different one? There are several popular choices other than LYAH: * FP Complete Shool of Haskellhttps://www.fpcomplete.com/school * Wikibooks: http://en.wikibooks.org/wiki/Haskell * Real World Haskell: http://book.realworldhaskell.org (Warning: while I remember learning from this book some time ago and it was fine, every now and then I see people saying that it's outdated and some examples don't compile)
Kind regards, Marcin Mrotek _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
Thanks, Im followinh the CiS 194 course which can be found here : http://www.seas.upenn.edu/~cis194/spring13/index.html and not the LYAH. The last book is used on this course. Roelof

Hello, Roelof. Your function is done. But it should be named 'toDigitsReversed' or so. So if you could write another 'reverse' function then you can compose like this. toDigits = reverse . toDigitsReversed -- YCH

"Roelof Wobben":
I have this :
-- | Main entry point to the application. module Main where
-- | convert a number to a array in pieces where a negative number will
be a empty array.
toDigits :: Integer -> [Integer] toDigits n | n < 0 = [] | otherwise = n/10 : []
-- | The main entry point. main :: IO () main = do toDigits 123
First of all, what do you expect from your program? The problem with your code is that `main` declares type `IO ()` in its signatures but its body has another type, [Integer]. What do you think is wrong with the compiler's output? -- Nadir
participants (6)
-
Marcin Mrotek
-
Max Voit
-
Nadir Sampaoli
-
Norbert Melzer
-
Roelof Wobben
-
YCH