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

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
12 messages Options
Reply | Threaded
Open this post in threaded view
|

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

Roelof Wobben
Reply | Threaded
Open this post in threaded view
|

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

Max Voit-2
Hi Roelof,

On Thu, 05 Feb 2015 12:37:12 +0100
Roelof Wobben <r.wobben at home.nl> wrote:

> main :: IO ()
> main = do
> ??? toDigits 123

> src/Main.hs at 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

Reply | Threaded
Open this post in threaded view
|

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

Roelof Wobben
Max Voit schreef op 5-2-2015 om 13:01:

> Hi Roelof,
>
> On Thu, 05 Feb 2015 12:37:12 +0100
> Roelof Wobben <r.wobben at home.nl> wrote:
>
>> main :: IO ()
>> main = do
>>      toDigits 123
>> src/Main.hs at 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 at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners

Thanks,

Another question :  how can I display the array ?

putStrln works only for strings,

Roelof


Reply | Threaded
Open this post in threaded view
|

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

Nadir Sampaoli
In reply to this post by Roelof Wobben
"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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20150205/d59faff0/attachment.html>

Reply | Threaded
Open this post in threaded view
|

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

Max Voit-2
In reply to this post by Roelof Wobben
On Thu, 05 Feb 2015 13:25:16 +0100
Roelof Wobben <r.wobben at home.nl> 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

Reply | Threaded
Open this post in threaded view
|

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

Roelof Wobben
Max Voit schreef op 5-2-2015 om 13:35:

> On Thu, 05 Feb 2015 13:25:16 +0100
> Roelof Wobben <r.wobben at home.nl> 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 at 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


Reply | Threaded
Open this post in threaded view
|

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

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)

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>

Reply | Threaded
Open this post in threaded view
|

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

Norbert Melzer
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>

Reply | Threaded
Open this post in threaded view
|

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

Roelof Wobben
Reply | Threaded
Open this post in threaded view
|

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

Marcin Mrotek
@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

Reply | Threaded
Open this post in threaded view
|

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

Roelof Wobben
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 at 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


Reply | Threaded
Open this post in threaded view
|

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

Dontdie YCH
In reply to this post by Roelof Wobben
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