Cool! Thanks Ryan!<br><br><div class="gmail_quote">On Sat, Jan 24, 2009 at 8:31 PM, Ryan Ingram <span dir="ltr"><<a href="mailto:ryani.spam@gmail.com">ryani.spam@gmail.com</a>></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 <<a href="mailto:hoknamahn@gmail.com">hoknamahn@gmail.com</a>>:<br>
<div class="Ih2E3d">> What I want to ask you guys can we define a function with arbitrary number<br>
> of parameters? Actually not really arbitrary but just several possibilities<br>
> (as we have with value constructors).<br>
> For instance cross product can have 1, 2 or 3 vectors as inputs depends on<br>
> 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. Here's an example using type families:<br>
<br>
class Vector v => Cross v where<br>
type CrossResult v<br>
cross :: v -> CrossResult v<br>
<br>
instance Cross Vector2 where<br>
type CrossResult v = Vector2<br>
cross (Vector2 x y) = Vector2 (-y) x -- is this the right operation?<br>
<br>
instance Cross Vector3 where<br>
type CrossResult v = Vector3 -> Vector3<br>
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>
-- ryan<br>
</font></blockquote></div><br>