[bcc haskell, cc haskell-cafe]<br><br><div><span class="gmail_quote">On 9/5/07, <b class="gmail_sendername">Tomi Owens</b> &lt;<a href="mailto:t.owens@hautlieu.sch.je">t.owens@hautlieu.sch.je</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hi there. I&#39;m a teacher of Maths and am working my way through the Euler
Project problems for fun. I have mostly been using Basic, but have read
up about Haskell and think it looks like a sensible way to solve many of
the problems.
<br>
<br>
OK, so I&#39;ve downloaded GHCi and am trying to teach myself.
<br>
<br>
So far I have done this:
<br>
<br>
&nbsp; ___&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ___ _
<br>
&nbsp;/ _ \ /\&nbsp; /\/ __(_)
<br>
/ /_\// /_/ / /&nbsp; | |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; GHC Interactive, version 6.6.1, for Haskell 98.
<br>
/ /_\\/ __&nbsp; / /___| |&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <a href="http://www.haskell.org/ghc/" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">http://www.haskell.org/ghc/</a>
<br>
\____/\/ /_/\____/|_|&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Type :? for help.
<br>
<br>
Loading package base ... linking ... done.
<br>
Prelude&gt; let f (a,b) = a * floor (100000/b)
<br>
Prelude&gt; f(2,5)
<br>
40000
</blockquote><div><br>Here you can find out type ghci has inferred for this function.<br>&gt; :t f<br>f :: (RealFrac b, Integral b1) =&gt; (b1, b) -&gt; b1<br><br>&nbsp;<br></div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">

This function works just as I want it to.
<br>
<br>
Now I try creating a list:
<br>
<br>
Prelude&gt; [(a<sup>2</sup>+b<sup>2</sup>,a)| a &lt;- [1..4] , b&lt;- [1..4], a<sup>2</sup>+b<sup>2</sup>&lt;20, b&lt;=a]
<br>
[(2,1),(5,2),(8,2),(10,3),(13,3),(18,3),(17,4)]
</blockquote><div><br>Let&#39;s assign this to an intermediate variable so we can query it&#39;s type:<br><br>Prelude&gt; let lst = [(a ^ 2 + b ^ 2, a) | a &lt;- [1..4], b &lt;- [1..4], a^2 + b^2 &lt; 20, b &lt;= a]<br>Prelude&gt; lst
<br>[(2,1),(5,2),(8,2),(10,3),(13,3),(18,3),(17,4)]<br>Prelude&gt; :t lst<br>lst :: [(Integer, Integer)]<br><br>aha; here&#39;s the source of the type mismatch:<br>Prelude&gt; :t floor<br>floor :: (RealFrac a, Integral b) =&gt; a -&gt; b
<br><br>Floor has to take a RealFrac. According to hoogle[1], we can use various floating-point approximations (Float, Double, CFloat, etc) or we can use the exact Rational type.<br>[1] <a href="http://haskell.org/hoogle/?q=RealFrac">
http://haskell.org/hoogle/?q=RealFrac</a><br><br> You can get your types to match by declaring your list to be of type [(Rational, Rational)] either by explicitly typing one of the untyped variables or the entire expression:
<br>Prelude&gt; let lst = [(a ^ 2 + b ^ 2, a) | (a::Rational) &lt;- [1..4], b &lt;- [1..4], a^2 + b^2 &lt; 20, b &lt;= a]<br>Prelude&gt; :t lst<br>lst :: [(Rational, Rational)]<br>Prelude&gt; let lst :: [(Rational, Rational)] = [(a ^ 2 + b ^ 2, a) | a &lt;- [1..4], b &lt;- [1..4], a^2 + b^2 &lt; 20, b &lt;= a]
<br>Prelude&gt; :t lst<br>lst :: [(Rational, Rational)]<br><br></div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
and this works
<br>
So now I try to apply the function to the list:
<br>
<br>
Prelude&gt; map (f) [(a<sup>2</sup>+b<sup>2</sup>,a)| a &lt;- [1..4] , b&lt;- [1..4], a<sup>2</sup>+b<sup>2</sup>&lt;20, b&lt;=a]
<br>
<br>
and I get this result:
<br>
<br>
&lt;interactive&gt;:1:5:
<br>
&nbsp;&nbsp; Ambiguous type variable `t&#39; in the constraints:
<br>
&nbsp;&nbsp;&nbsp;&nbsp; `Integral t&#39; arising from use of `f&#39; at &lt;interactive&gt;:1:5
<br>
&nbsp;&nbsp;&nbsp;&nbsp; `RealFrac t&#39; arising from use of `f&#39; at &lt;interactive&gt;:1:5
<br>
&nbsp;&nbsp; Probable fix: add a type signature that fixes these type variable(s)
</blockquote><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
I&#39;m sorry, but I don&#39;t quite get how to set the type signature and how
it will apply to my function...
<br>
<br>
Thanks,
</blockquote><div><br>Hope this helps<br>&nbsp;</div><br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Tomi
--------------------------------------------------------------------------------
<p>Department for Education, Sport and Culture E Mail<br>
  This message is for the named person&#39;s use only. It may contain<br>
  confidential, proprietary or legally privileged information. No<br>
  confidentiality or privilege is waived or lost by any mistransmission.<br>
  If you receive this message in error, please immediately delete it and all copies of it from your system, destroy any hard copies of it and notify the sender. You must not, directly or indirectly, use, disclose, distribute, print, or copy any part of this message if you are not the intended recipient. The Department for Education, Sport and Culture and any of its establishments each reserve the right to monitor all e-mail communications through its networks. 
</p>
<p> </p>
<p>Any views expressed in this message are those of the individual sender, except where the message states otherwise and the sender is authorised to state them to be the views of any such entity. </p>
<p> </p>
<p>The Department for Education, Sport and Culture shall not be liable to the recipient or any third party for any loss or damage, however it appears, from this e-mail or its content. This includes loss or damage caused by viruses. It is the responsibility of the recipient to ensure that the opening of this message and its attachments shall not adversely affect systems or data.
</p>
<p>--------------------------------------------------------------------------------</p><br>_______________________________________________<br>Haskell mailing list<br><a onclick="return top.js.OpenExtLink(window,event,this)" href="mailto:Haskell@haskell.org">
Haskell@haskell.org</a><br><a onclick="return top.js.OpenExtLink(window,event,this)" href="http://www.haskell.org/mailman/listinfo/haskell" target="_blank">http://www.haskell.org/mailman/listinfo/haskell</a><br><br></blockquote>
</div><br><br clear="all"><br>-- <br>Scott Williams