[Haskell-cafe] Why is Haskell flagging this?
Daniel Peebles
pumpkingod at gmail.com
Fri Dec 17 22:15:02 CET 2010
Write out more types and it'll get more clear.
f is [Int] -> IO [Int]
lst is f applied to Num a => [a], so it is of type IO [Int]
fmap is applied to lst, which means it's "stepping inside" the IO. That
means it's applying +1 to [1,2,3,4,5], which doesn't make much sense unless
you have a Num instance for [Int]. That's what the error was saying.
What you probably want is fmap (fmap (+1)) lst.
Not sure why you're doing this stuff in the first place though, since the
return into IO is only restricting what you can do with it. Also, the do in
both cases is unnecessary (in the second case you can replace the let with a
let..in)
Hope this helps,
Dan
On Fri, Dec 17, 2010 at 12:04 PM, michael rice <nowgate at yahoo.com> wrote:
> I don't understand this error message. Haskell appears not to understand
> that 1 is a Num.
>
> Prelude> :t 1
> 1 :: (Num t) => t
> Prelude> :t [1,2,3,4,5]
> [1,2,3,4,5] :: (Num t) => [t]
> Prelude>
>
> Michael
>
> ===================
>
> 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 )
>
> test.hs:5:17:
> 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.
> Prelude>
>
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20101217/24bce582/attachment.htm>
More information about the Haskell-Cafe
mailing list