[prev in list] [next in list] [prev in thread] [next in thread] 

List:       haskell-beginners
Subject:    Re: [Haskell-beginners] Got "Non type-variable argument in the constraint" error for a simple functi
From:       wizard <xie.zhiyi () gmail ! com>
Date:       2016-02-08 20:20:48
Message-ID: CA+RmsoUJWMP1QCg7nULcczjd5JjNK9WewLmNtcN3s+5jD_AbaA () mail ! gmail ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


Thanks Frerich and David! I got it, :-)

2016-02-09 0:43 GMT+08:00 David McBride <toad3k@gmail.com>:

> 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.
> 
> > > t (-)
> (-) :: Num a => a -> a -> a
> > > t (+)
> (+) :: Num a => a -> a -> a
> > > t (-1)
> (-1) :: Num a => a
> > > t (+1)
> (+1) :: Num a => a -> a
> > > t (1-1)
> (1-1) :: Num a => a
> > > t (1+1)
> (1+1) :: Num a => a
> 
> 
> On Mon, Feb 8, 2016 at 11:24 AM, wizard <xie.zhiyi@gmail.com> wrote:
> 
> > Dear all,
> > 
> > I just started to learn Haskell with learnyouahaskell.com and at the
> > very beginning, I met a strange issue with following simple function:
> > 
> > -- why does work with "toZero 10" but not for "toZero -10"?toZero :: (Integral t) \
> > => t -> [t]toZero 0 = [0]toZero x = if x > 0 then x : toZero (x - 1) else x : \
> > toZero (x + 1) 
> > 
> > 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":
> > 
> > *Main> toZero -10
> > 
> > <interactive>:12:1:
> > Non type-variable argument in the constraint: Num (t -> [t])
> > (Use FlexibleContexts to permit this)
> > When checking that ‘it' has the inferred type
> > it :: forall t. (Integral t, Num (t -> [t])) => t -> [t]
> > 
> > 
> > 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 learnyouahaskell.com but still has
> > no idea on why this error. Anybody can help to explain this?
> > Thanks a lot.
> > 
> > Regards
> > Zhiyi Xie
> > 
> > _______________________________________________
> > Beginners mailing list
> > Beginners@haskell.org
> > http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> > 
> > 
> 
> _______________________________________________
> Beginners mailing list
> Beginners@haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> 
> 


[Attachment #5 (text/html)]

<div dir="ltr">Thanks Frerich and David! I got it, :-)</div><div \
class="gmail_extra"><br><div class="gmail_quote">2016-02-09 0:43 GMT+08:00 David \
McBride <span dir="ltr">&lt;<a href="mailto:toad3k@gmail.com" \
target="_blank">toad3k@gmail.com</a>&gt;</span>:<br><blockquote class="gmail_quote" \
style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><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>&gt;:t \
(-)<br>(-) :: Num a =&gt; a -&gt; a -&gt; a<br>&gt;:t (+)<br>(+) :: Num a =&gt; a \
-&gt; a -&gt; a<br>&gt;:t (-1)<br>(-1) :: Num a =&gt; a<br>&gt;:t (+1)<br>(+1) :: Num \
a =&gt; a -&gt; a<br>&gt;:t (1-1)<br>(1-1) :: Num a =&gt; a<br>&gt;:t (1+1)<br>(1+1) \
:: Num a =&gt; a<br><br></div><div class="gmail_extra"><br><div \
class="gmail_quote"><div><div class="h5">On Mon, Feb 8, 2016 at 11:24 AM, wizard \
<span dir="ltr">&lt;<a href="mailto:xie.zhiyi@gmail.com" \
target="_blank">xie.zhiyi@gmail.com</a>&gt;</span> wrote:<br></div></div><blockquote \
class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc \
solid;padding-left:1ex"><div><div class="h5"><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 &quot;toZero \
10&quot; but not for &quot;toZero -10&quot;?</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">=&gt;</span> t <span \
style="color:rgb(170,34,255);font-weight:bold">-&gt;</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)">&gt;</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., &quot;toZero 10&quot; 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., &quot;toZero -10&quot;:</div><div><br></div><blockquote \
style="margin:0px 0px 0px 40px;border:none;padding:0px"><div><div><font \
face="monospace, monospace">*Main&gt; toZero -10</font></div></div><div><div><font \
face="monospace, monospace"><br></font></div></div><div><div><font face="monospace, \
monospace">&lt;interactive&gt;:12:1:</font></div></div><div><div><font \
face="monospace, monospace">      Non type-variable argument in the constraint: Num \
(t -&gt; [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 -&gt; [t])) =&gt; t -&gt; \
[t]</font></div></div></blockquote><div><br></div><div>This seems strange to me as 10 \
and -10 has exactly the same type &quot;Num a =&gt; a&quot;. I&#39;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></div></div><span class="">_______________________________________________<br> \
Beginners mailing list<br> <a href="mailto:Beginners@haskell.org" \
target="_blank">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></span></blockquote></div><br></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>



_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic