<div dir="ltr"><div dir="auto">Hi,<div dir="auto">as you said, the operator (/) takes arguments that belong to the class <font face="monospace"><b>Fractional</b></font> (instances of this class are the types <font face="monospace"><b>Double</b></font> and <font face="monospace"><b>Float</b></font>).</div><div dir="auto"><br></div><div dir="auto">The function <font face="monospace">length</font> has type:</div><div dir="auto"><font face="monospace">Prelude> :t length<br>length :: Foldable t => t a -> Int</font></div><div>this means that it takes a list and returns something of type <font face="monospace">Int</font>, In fact</div><div><font face="monospace">Prelude> :t (length [23,34,45])<br>(length [23,34,45]) :: Int<br></font></div><div><br></div><div>Since <font face="monospace">Int</font> is <b>not</b> an instance of the class Fractional, you can't use (/). </div><div>Instead <font face="monospace">Int</font> is an instance of the class <font face="monospace">Integral</font>, so you can use <font face="monospace">div</font> with arguments of type <font face="monospace">Int</font>. <br></div><div><br></div><div>The example <font face="monospace">6/3</font> works because you didn't assign any type to those numbers, so they are seen as belonging to the class <font face="monospace">Num</font>.</div><div><font face="monospace">Prelude> x=6<br>Prelude> :t x<br>x :: Num p => p<br></font></div><div><font face="arial, sans-serif">This means that they can be seen both as Integral and Fractional and you can use them with both functions that take Integral arguments and functions that take Fractional arguments.</font></div><div><font face="arial, sans-serif"><br></font></div><div><font face="arial, sans-serif">If you specify that for example 6 is an Int you can't use (/) any more:</font></div><div><font face="monospace">Prelude> x::Int; x=6;<br>Prelude> x/3<br><font color="#ff0000"><interactive>:20:1: error:<br> • No instance for (Fractional Int) arising from a use of ‘/’<br></font></font></div><div>From the error message you can see that the problem is as I said before that <font face="monospace">Int</font> is not an instace of the class <font face="monospace">Fractional</font>.<br></div><div><br></div><div>Hope is clear</div><div>Best,</div><div>Ut</div></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Il ven 17 apr 2020, 09:20 Alexander Chen <<a href="mailto:alexander@chenjia.nl" target="_blank">alexander@chenjia.nl</a>> ha scritto:<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 style="font-family:Arial;font-size:13px">Hi, <div><br></div><div><br></div><div><div>Prelude> :t (/)</div><div>(/) :: <b>Fractional</b> a => a -> a -> a</div><div><br></div><div><div>Prelude> :t div</div><div>div :: <b>Integral</b> a => a -> a -> a</div></div><div><br></div><div><br></div><div>Prelude> 6 / length [23,34,45]</div><div>error </div><div><br></div><div>Prelude> 6 / 3</div><div>2.0<br><br>Could somebody explain to me why this is?</div><div><br></div><div>thanks,</div><div><br></div><div>Alexander Chen</div><div><br></div><div><br><div><div style="font-family:Arial;font-size:13px"><br></div></div></div></div></div>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" rel="noreferrer" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
</blockquote></div>