<html><head><meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body 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">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 class="hs-varid">    abs</span> <span class="hs-varid">n</span>  <span class="hs-keyglyph">=</span> <span class="hs-keyword">if</span> <span class="hs-varid">n</span> <span class="hs-varop">`geInt`</span> <span class="hs-num">0</span> <span class="hs-keyword">then</span> <span class="hs-varid">n</span> <span class="hs-keyword">else</span> <span class="hs-varid">negate</span> <span class="hs-varid">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">hengruo.z@gmail.com</a>> のメッセージ:<br><br></div><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><blockquote type="cite"><div><span>_______________________________________________</span><br><span>Beginners mailing list</span><br><span><a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a></span><br><span><a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a></span><br></div></blockquote></body></html>