<div dir="ltr"><div>If you are wondering why you are having this problem it is because - can be interpretted as either a one argument negation or a two argument subtraction. If you put parenthesis around (-n) where n is an integer, it will interpret it as unary, something that will not happen in other operators.<br><br></div>>:t (-)<br>(-) :: Num a => a -> a -> a<br>>:t (+)<br>(+) :: Num a => a -> a -> a<br>>:t (-1)<br>(-1) :: Num a => a<br>>:t (+1)<br>(+1) :: Num a => a -> a<br>>:t (1-1)<br>(1-1) :: Num a => a<br>>:t (1+1)<br>(1+1) :: Num a => a<br><br></div><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Feb 8, 2016 at 11:24 AM, wizard <span dir="ltr"><<a href="mailto:xie.zhiyi@gmail.com" target="_blank">xie.zhiyi@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Dear all,<div><br></div><div>I just started to learn Haskell with <a href="http://learnyouahaskell.com" target="_blank">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/" target="_blank">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>
<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" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br></div>