<br><br><div class="gmail_quote">On Fri, Dec 17, 2010 at 9:04 AM, michael rice <span dir="ltr"><<a href="mailto:nowgate@yahoo.com">nowgate@yahoo.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<table cellspacing="0" cellpadding="0" border="0"><tbody><tr><td valign="top" style="font:inherit"><span style="font-family:courier,monaco,monospace,sans-serif">I don't understand this error message. Haskell appears not to understand that 1 is a Num.<br>
<br>Prelude> :t 1<br>1 :: (Num t) => t<br>Prelude> :t [1,2,3,4,5]<br>[1,2,3,4,5] :: (Num t) => [t]<br>Prelude> <br><br>Michael<br><br>===================<br><br>f :: [Int] -> IO [Int]</span><br style="font-family:courier,monaco,monospace,sans-serif">
<span style="font-family:courier,monaco,monospace,sans-serif">f lst = do return lst</span><br style="font-family:courier,monaco,monospace,sans-serif"><br style="font-family:courier,monaco,monospace,sans-serif"><span style="font-family:courier,monaco,monospace,sans-serif">main = do let lst = f [1,2,3,4,5]</span><br style="font-family:courier,monaco,monospace,sans-serif">
<span style="font-family:courier,monaco,monospace,sans-serif"> fmap (+1) lst</span></td></tr></tbody></table></blockquote><div><br></div><div>f takes [Int] and returns IO [Int]</div><div><br></div><div>fmap is </div>
<div><br></div><div><div>fmap :: (Functor f) => (a -> b) -> f a -> f b</div></div><div><br></div><div>That is it takes a function of a's to b's, a functor of a, and returns you a functor of b.</div><div>
<br></div><div>So when you fmap (+1) to an IO [Int], it's trying to add 1 to a [Int], and [Int] is not an instance of Num, so the + does not work.</div><div><br></div><div>Luckily you can use function composition here</div>
<div><br></div><div>(fmap . fmap) (+1) $ f [1..10]</div><div><div>[2,3,4,5,6,7,8,9,10,11]</div></div><div><br></div><div>fmap . fmap is the type I think you wanted:</div><div><br></div><div><div>Prelude> :t fmap . fmap</div>
<div>fmap . fmap</div><div> :: (Functor f, Functor f1) => (a -> b) -> f (f1 a) -> f (f1 b)</div></div><div><br></div><div><br></div><div>With IO as the f Functor, and [] as the f1 Functor.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">
<table cellspacing="0" cellpadding="0" border="0"><tbody><tr><td valign="top" style="font:inherit"><br style="font-family:courier,monaco,monospace,sans-serif"><br>===============================<br><br>Prelude> :l test<br>
[1 of 1] Compiling Main ( test.hs, interpreted )<br><br>test.hs:5:17:<br> No instance for (Num [Int])<br> arising from the literal `1' at test.hs:5:17<br> Possible fix: add an instance declaration for (Num [Int])<br>
In the second argument of `(+)', namely `1'<br> In the first argument of `fmap', namely `(+ 1)'<br> In the expression: fmap (+ 1) lst<br>Failed, modules loaded: none.<br>Prelude> <br></td></tr>
</tbody></table><br>
<br>_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
<br></blockquote></div><br>