Cool! Thanks Ryan!<br><br><div class="gmail_quote">On Sat, Jan 24, 2009 at 8:31 PM, Ryan Ingram <span dir="ltr">&lt;<a href="mailto:ryani.spam@gmail.com">ryani.spam@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
2009/1/24 Olex P &lt;<a href="mailto:hoknamahn@gmail.com">hoknamahn@gmail.com</a>&gt;:<br>
<div class="Ih2E3d">&gt; What I want to ask you guys can we define a function with arbitrary number<br>
&gt; of parameters? Actually not really arbitrary but just several possibilities<br>
&gt; (as we have with value constructors).<br>
&gt; For instance cross product can have 1, 2 or 3 vectors as inputs depends on<br>
&gt; the dimension. Is it 2d, 3d or 4d case.<br>
<br>
</div>You need type classes plus some language extension to do this<br>
properly. &nbsp;Here&#39;s an example using type families:<br>
<br>
class Vector v =&gt; Cross v where<br>
 &nbsp; type CrossResult v<br>
 &nbsp; cross :: v -&gt; CrossResult v<br>
<br>
instance Cross Vector2 where<br>
 &nbsp; type CrossResult v = Vector2<br>
 &nbsp; cross (Vector2 x y) = Vector2 (-y) x &nbsp;-- is this the right operation?<br>
<br>
instance Cross Vector3 where<br>
 &nbsp; type CrossResult v = Vector3 -&gt; Vector3<br>
 &nbsp; cross (Vector3 x1 y1 z1) (Vector3 x2 y2 z2) = Vector3 (y1*z2 -<br>
z1*y2) (z1*x2 - x1*z2) (x1*y2 - y1*x2)<br>
<br>
etc.<br>
<br>
A difficult exercise is to define these operations in terms of each<br>
other inductively so that it works for arbitrary vector types; you<br>
need vectors indexed on some type-level natural, plus a bunch of<br>
hackery in the instances to make it work.<br>
<font color="#888888"><br>
 &nbsp;-- ryan<br>
</font></blockquote></div><br>