[Haskell-cafe] Why is Haskell flagging this?
mads.lindstroem at gmail.com
Fri Dec 17 22:45:48 CET 2010
The type of lst is "IO [Int]" and therefore "fmap (+1)" applies (+1) to
the hole lists of integers, and not to each member of the list. That is:
fmap (+1) lst <=>
fmap (+1) (return [1,2,3,4,5]) <=>
return ([1,2,3,4,5] + 1)
and you cannot say [1,2,3,4,5] + 1.
Does that make sense?
Maybe you want to say:
main = do let lst = [1,2,3,4,5]
print $ map (+1) lst
On Fri, 2010-12-17 at 09:04 -0800, michael rice 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]
> 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