<div dir="ltr">Oh! Thank you very much! I took notice of instances of Num just now... You two are so kind!</div><div class="gmail_extra"><br><div class="gmail_quote">2015-03-11 21:30 GMT+08:00 Daniel P. Wright <span dir="ltr"><<a href="mailto:dani@dpwright.com" target="_blank">dani@dpwright.com</a>></span>:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="auto"><div>One nice thing about haskell is that the source to most things is available on hackage. If you look at abs, here:</div><div><br></div><div><a href="https://hackage.haskell.org/package/base-4.7.0.2/docs/src/GHC-Num.html#abs" target="_blank">https://hackage.haskell.org/package/base-4.7.0.2/docs/src/GHC-Num.html#abs</a></div><div><br></div><div>You will see two things:</div><div><br></div><div>1) abs is actually part of the Num typeclass, so can be defined differently for the different numerical types</div><div>2) the definition for Int is similar to yours:</div><div><pre><font face="UICTFontTextStyleBody"><span style="white-space:normal;background-color:rgba(255,255,255,0)"><span> abs</span> <span>n</span> <span>=</span> <span>if</span> <span>n</span> <span>`geInt`</span> <span>0</span> <span>then</span> <span>n</span> <span>else</span> <span>negate</span> <span>n</span></span></font></pre></div><div><br></div><div>Notice `geInt` is just an int-specific (>=) operator. Because we're defining an instance we know the concrete type we're dealing with (the type is Int -> Int by this point, rather than a -> a), so we don't need to make use of Ord in this case.<br><br><br></div><div><br>11 Mar 2015 22:21、Zhang Hengruo <<a href="mailto:hengruo.z@gmail.com" target="_blank">hengruo.z@gmail.com</a>> のメッセージ:<br><br></div><div><div class="h5"><blockquote type="cite"><div><div dir="ltr"><div>I'm a newbie, knowing a little about Haskell, and hope my question isn't very silly...<br></div><div>=====================================================================</div><div>My absolute value function looks like this:<br></div><div><br></div><div>abs2 :: Num a => a -> a</div><div>abs2 n = if n >= 0 then n else 0 - n</div><div><br></div><div>GHCi tells me that I should add Ord type class to its definition. Well, it's true. It has used relational operators and Num isn't a subclass of Ord.</div><div>However, when I input ":t abs" in GHCi, the interpreter shows "abs :: Num a => a -> a". I read GHC/Num.lhs and find that abs is defined as "abs :: a -> a" and has no concrete content. So I think the abs function is written in C as a module to implement directly and the type of abs just follows its class GHC.Num. Is it right? Or there are any other reasons? </div><div><br></div><div>Thanks,</div><div><br></div><div>Hengruo<br></div></div>
</div></blockquote></div></div><blockquote type="cite"><div><span>_______________________________________________</span><span class=""><br><span>Beginners mailing list</span><br><span><a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a></span><br><span><a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a></span><br></span></div></blockquote></div><br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br></div>