<div dir="ltr"><div dir="ltr"><br></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Tue, Jul 20, 2021 at 1:43 PM Galaxy Being <<a href="mailto:borgauf@gmail.com">borgauf@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">I'm investigating rational numbers with Haskell. This is the source I've found<div><br></div><div><font face="monospace">data Ratio a = !a :% !a deriving (Eq)<br></font></div><div><font face="monospace"><br></font></div><div><font face="monospace">reduce ::  (Integral a) => a -> a -> Ratio a<br>{-# SPECIALISE reduce :: Integer -> Integer -> Rational #-}<br>reduce _ 0              =  ratioZeroDenominatorError<br>reduce x y              =  (x `quot` d) :% (y `quot` d)<br>                           where d = gcd x y<br>(%) :: (Integral a) => a -> a -> Ratio a<br>x % y =  reduce (x * signum y) (abs y)</font></div><div><font face="monospace"><br></font></div><div><font face="arial, sans-serif">The </font><font face="monospace">Ratio</font><font face="arial, sans-serif"> data type would seem to be a parameterized type with two parameters of the same type that must be "settled" in that they're not to be lazily dealt with. Then the </font><font face="monospace">:% </font><font face="arial, sans-serif">is the data constructor, the </font><font face="monospace">:</font><font face="arial, sans-serif"> meaning it's a data constructor and not just an operation function. So this could have been</font></div></div></blockquote><div><br></div><div>The type has one parameter a, the (:%) constructor has two arguments (both of type a).</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div><font face="monospace">data Ratio a = :% !a !a deriving (Eq)</font></div></div></blockquote><div><br></div><div>You always need to use parentheses around an infix operator to use it in prefix, just like you need backticks around a function to use it as infix (like `quot` in the implementation of reduce).</div><div><br></div><div>data Ratio a = (:%) !a !a deriving (Eq)</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr"><div>correct? But then what confuses me is in reduce, why <br clear="all"><div><br></div><div><font face="monospace">reduce x y  =  (x `quot` d) :% (y `quot` d)</font><br></div><div><br></div><div>and not just <font face="monospace">%</font>? We have<font face="monospace"> :%</font> defined in the data type and then <font face="monospace">(%)</font> defined as a function. What is going on here?</div></div></div></blockquote><div><br></div><div>The function (%) uses reduce in its definition, the equation would never terminate due to infinite recursion if the function (%) was used instead of the constructor (:%).</div><div><br></div><div>-bob</div><div><br></div></div></div>