<div dir="ltr">Dear all,<div><br></div><div>I just started to learn Haskell with <a href="http://learnyouahaskell.com">learnyouahaskell.com</a> and at the very beginning, I met a strange issue with following simple function:</div><div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div><pre style="line-height:16.25px;color:rgb(0,0,0)"><span style="color:rgb(64,128,128);font-style:italic">-- why does work with "toZero 10" but not for "toZero -10"?</span>
<span style="color:rgb(0,0,255)">toZero</span> <span style="color:rgb(170,34,255);font-weight:bold">::</span> (<span style="color:rgb(176,0,64)">Integral</span> t) <span style="color:rgb(170,34,255);font-weight:bold">=></span> t <span style="color:rgb(170,34,255);font-weight:bold">-></span> [t]
<span style="color:rgb(0,0,255)">toZero</span> <span style="color:rgb(102,102,102)">0</span> <span style="color:rgb(170,34,255);font-weight:bold">=</span> [<span style="color:rgb(102,102,102)">0</span>]
<span style="color:rgb(0,0,255)">toZero</span> x <span style="color:rgb(170,34,255);font-weight:bold">=</span> <span style="color:rgb(0,128,0);font-weight:bold">if</span> x <span style="color:rgb(102,102,102)">></span> <span style="color:rgb(102,102,102)">0</span> <span style="color:rgb(0,128,0);font-weight:bold">then</span> x <span style="color:rgb(176,0,64)">:</span> toZero (x <span style="color:rgb(102,102,102)">-</span> <span style="color:rgb(102,102,102)">1</span>)
<span style="color:rgb(0,128,0);font-weight:bold">else</span> x <span style="color:rgb(176,0,64)">:</span> toZero (x <span style="color:rgb(102,102,102)">+</span> <span style="color:rgb(102,102,102)">1</span>)</pre></div></div></blockquote><div><br></div><div>This function works as expected for positive arguments, e.g., "toZero 10" gives me [10,9,8,7,6,5,4,3,2,1,0]. However, GHCI will raise following error if I give it a negative argument, e.g., "toZero -10":</div><div><br></div><blockquote style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div><font face="monospace, monospace">*Main> toZero -10</font></div></div><div><div><font face="monospace, monospace"><br></font></div></div><div><div><font face="monospace, monospace"><interactive>:12:1:</font></div></div><div><div><font face="monospace, monospace"> Non type-variable argument in the constraint: Num (t -> [t])</font></div></div><div><div><font face="monospace, monospace"> (Use FlexibleContexts to permit this)</font></div></div><div><div><font face="monospace, monospace"> When checking that ‘it’ has the inferred type</font></div></div><div><div><font face="monospace, monospace"> it :: forall t. (Integral t, Num (t -> [t])) => t -> [t]</font></div></div></blockquote><div><br></div><div>This seems strange to me as 10 and -10 has exactly the same type "Num a => a". I've done with chapter 1~10 of <a href="http://learnyouahaskell.com/">learnyouahaskell.com</a> but still has no idea on why this error. Anybody can help to explain this?</div><div>Thanks a lot.</div><div><br></div><div>Regards</div><div>Zhiyi Xie</div></div>