[Haskell-cafe] Why is Haskell flagging this?
miguelimo38 at yandex.ru
Fri Dec 17 23:44:54 CET 2010
On 17 Dec 2010, at 20:04, michael rice wrote:
> I don't understand this error message. Haskell appears not to understand that 1 is a Num.
As it clearly states in the error message, it doesn't understand that [Int] is a Num - and it's not.
"No instance for Num something" usually indicates that you're trying to use an integer literal - in this case, "1" - as this "something".
The problem is that your "lst" has the type "IO [Int]" (which is the same as "IO ( Int)"). "fmap" has the type "(a -> b) -> f a -> f b", so, it tries to unify the type of "(+1)" with "[Int] -> something" - which, probably, isn't what you've meant. In fact, I'm pretty sure you wanted "lst" to have the type "[Int]" (= " Int"), without "IO". You can do that using "<-" instead of "let":
do lst <- fst [1,2,3,4,5]
return (fmap (+1) lst)
> Prelude> :t 1
> 1 :: (Num t) => t
> Prelude> :t [1,2,3,4,5]
> [1,2,3,4,5] :: (Num t) => [t]
> f :: [Int] -> IO [Int]
> f lst = do return lst
> main = do let lst = f [1,2,3,4,5]
> fmap (+1) lst
> Prelude> :l test
> [1 of 1] Compiling Main ( test.hs, interpreted )
> No instance for (Num [Int])
> arising from the literal `1' at test.hs:5:17
> Possible fix: add an instance declaration for (Num [Int])
> In the second argument of `(+)', namely `1'
> In the first argument of `fmap', namely `(+ 1)'
> In the expression: fmap (+ 1) lst
> Failed, modules loaded: none.
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
More information about the Haskell-Cafe