Re: infix functions with patterns

Matthias Blume <[email protected]> Thu, 14 Jan 2010 13:39:48 -0600
Newsgroups gmane.comp.lang.sml.smlnj
Message-ID <[email protected]>
Jon,

you need to close the outer parentheses before CarCAdd (and then re- =

open them afterwards):

     fun (CarC(X(x1),Y(y1))) CarCAdd (CarC(X(x2),Y(y2))) =3D  =

CarC(X(x1+x2),Y(y1+y2))

Hope this helps!

Cheers,
Matthias

On Jan 14, 2010, at 7:16 AM, Jon Loldrup wrote:

> Hi
>
> I'm new on this mailinglist. I'm implementing an ant colony algorithm
> in SML and I'm trying to make an infix function (fun CarCAdd) to add
> two cartesian coordinates:
>
> fun (CarC(X(x1),Y(y1)) CarCAdd CarC(X(x2),Y(y2))) =3D  =

> CarC(X(x1+x2),Y(y1+y2))
>
> I get this error message:
>
> File "/tmp/sml2231HHf", line 47, characters 4-49:
> ! fun (CarC(X(x1),Y(y1)) CarCAdd CarC(X(x2),Y(y2))) =3D  =

> CarC(X(x1+x2),Y(y1+y2))
> !     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ (*these ^ are pointing up to*)
> ! Ill-formed left hand side of a clause                         (*but
> not including the  =3D   *)
> [closing file "/tmp/sml2231HHf"]
>
> I have another infix function using pattern matching as well:
> fun (Rad(r1)) radAdd (Rad(r2)) =3D Rad(r1+r2)
>
> so I don't see why my CarC should be erroneous.
>
> Heres the rest of the code needed to compile:
> (the compiler output is below the code)
>
>
> load "Math";
> open Math;
> load "Random";
> open Random;
> load "Array2";
> load "Array";
> open Array;
> load "Real";
> open List;
>
> val RGen     =3D newgen()
> val RNum     =3D random RGen
>
> datatype xCoord   =3D X      of real
> datatype yCoord   =3D Y      of real
> datatype angle    =3D Rad    of real
> datatype distance =3D Dist   of real (*en vektors l=E6ngde*)
> datatype polCoord =3D PolC   of (angle*distance) (*radian*laengde*)
> datatype carCoord =3D CarC   of (xCoord*yCoord) (*x,y  Cartesiske  =

> koordinater*)
> datatype path     =3D Path   of (angle  array) (*een vej*)
> datatype paths    =3D Paths  of (path   array) (*en vej pr myre*)
> datatype feromone =3D Ferom  of  real
> datatype feroSpot =3D fSpot  of (feromone*carCoord)
> datatype feroSpots=3D fSpots of (feroSpot list)
>
> val origo    =3D CarC(X(0.0),Y(0.0))
> val orig     =3D CarC(X(1.0),Y(1.0))
> val dest     =3D CarC(X(5.0),Y(2.0))
> val antsNum  =3D 17
> val jointsNum =3D 5
> val lengths  =3D fromList([1,2,1,2,1])
>
> fun PCtoCC Polk(Rad(rad),Dist(dist)) =3D  =

> CarC(X(dist*cos(rad)),Y(dist*sin(rad)))
>
> infix radAdd
> fun (Rad(r1)) radAdd (Rad(r2)) =3D Rad(r1+r2)
>
> (*adds cartesian coordinates*)
> infix CarcAdd
> fun (CarC(X(x1),Y(y1)) CarCAdd CarC(X(x2),Y(y2))) =3D  =

> CarC(X(x1+x2),Y(y1+y2))
>
>
>
>
> *******************************************
> *******************************************
> The compiler gives me the following:
> (The error description is at the bottom
> I used MoSML for this ouput, to get
> a more detailed error description)
> *******************************************
> *******************************************
>
>
> Moscow ML version 2.01 (January 2004)
> Enter `quit();' to quit.
> - [opening file "/tmp/sml2231HHf"]
>> val it =3D () : unit
>> type real =3D real
>  val cos =3D fn : real -> real
>  val tan =3D fn : real -> real
>  val ln =3D fn : real -> real
>  val pi =3D 3.14159265359 : real
>  val cosh =3D fn : real -> real
>  val pow =3D fn : real * real -> real
>  val exp =3D fn : real -> real
>  val tanh =3D fn : real -> real
>  val asin =3D fn : real -> real
>  val atan2 =3D fn : real * real -> real
>  val log10 =3D fn : real -> real
>  val acos =3D fn : real -> real
>  val atan =3D fn : real -> real
>  val sin =3D fn : real -> real
>  val sinh =3D fn : real -> real
>  val e =3D 2.71828182846 : real
>  val sqrt =3D fn : real -> real
>> val it =3D () : unit
>> type generator =3D generator
>  val rangelist =3D fn : int * int -> int * generator -> int list
>  val newgenseed =3D fn : real -> generator
>  val random =3D fn : generator -> real
>  val newgen =3D fn : unit -> generator
>  val range =3D fn : int * int -> generator -> int
>  val randomlist =3D fn : int * generator -> real list
>> val it =3D () : unit
>> val it =3D () : unit
>> type 'a array =3D 'a array
>  val 'a copy =3D fn :
>  {di : int, dst : 'a array, len : int option, si : int, src : 'a  =

> array} ->
>  unit
>  val 'a length =3D fn : 'a array -> int
>  val 'a update =3D fn : 'a array * int * 'a -> unit
>  val ('a, 'b) foldr =3D fn : ('a * 'b -> 'b) -> 'b -> 'a array -> 'b
>  val maxLen =3D 4194303 : int
>  val 'a tabulate =3D fn : int * (int -> 'a) -> 'a array
>  val 'a extract =3D fn : 'a array * int * int option -> 'a vector
>  val 'a array =3D fn : int * 'a -> 'a array
>  val 'a appi =3D fn : (int * 'a -> unit) -> 'a array * int * int  =

> option -> unit
>  val ('a, 'b) foldri =3D fn :
>  (int * 'a * 'b -> 'b) -> 'b -> 'a array * int * int option -> 'b
>  val 'a modifyi =3D fn : (int * 'a -> 'a) -> 'a array * int * int  =

> option -> unit
>  val 'a modify =3D fn : ('a -> 'a) -> 'a array -> unit
>  val 'a sub =3D fn : 'a array * int -> 'a
>  val 'a app =3D fn : ('a -> unit) -> 'a array -> unit
>  val 'a fromList =3D fn : 'a list -> 'a array
>  val ('a, 'b) foldli =3D fn :
>  (int * 'a * 'b -> 'b) -> 'b -> 'a array * int * int option -> 'b
>  val ('a, 'b) foldl =3D fn : ('a * 'b -> 'b) -> 'b -> 'a array -> 'b
>  val 'a copyVec =3D fn :
>  {di : int, dst : 'a array, len : int option, si : int, src : 'a  =

> vector} ->
>  unit
>> val it =3D () : unit
>> datatype 'a list =3D
>  ('a list,{con 'a nil : 'a list, con 'a :: : 'a * 'a list -> 'a list})
>  val 'a tabulate =3D fn : int * (int -> 'a) -> 'a list
>  val 'a rev =3D fn : 'a list -> 'a list
>  exn Empty =3D Empty : exn
>  val 'a tl =3D fn : 'a list -> 'a list
>  val 'a partition =3D fn : ('a -> bool) -> 'a list -> 'a list * 'a list
>  val ('a, 'b) map =3D fn : ('a -> 'b) -> 'a list -> 'b list
>  val 'a take =3D fn : 'a list * int -> 'a list
>  con 'a :: =3D fn : 'a * 'a list -> 'a list
>  val 'a last =3D fn : 'a list -> 'a
>  val 'a all =3D fn : ('a -> bool) -> 'a list -> bool
>  val 'a getItem =3D fn : 'a list -> ('a * 'a list) option
>  val 'a revAppend =3D fn : 'a list * 'a list -> 'a list
>  val 'a hd =3D fn : 'a list -> 'a
>  val 'a null =3D fn : 'a list -> bool
>  val 'a length =3D fn : 'a list -> int
>  val 'a filter =3D fn : ('a -> bool) -> 'a list -> 'a list
>  val 'a drop =3D fn : 'a list * int -> 'a list
>  val 'a app =3D fn : ('a -> unit) -> 'a list -> unit
>  val 'a nth =3D fn : 'a list * int -> 'a
>  val 'a exists =3D fn : ('a -> bool) -> 'a list -> bool
>  val 'a concat =3D fn : 'a list list -> 'a list
>  val ('a, 'b) mapPartial =3D fn : ('a -> 'b option) -> 'a list -> 'b  =

> list
>  con 'a nil =3D nil : 'a list
>  val 'a find =3D fn : ('a -> bool) -> 'a list -> 'a option
>  val ('a, 'b) foldl =3D fn : ('a * 'b -> 'b) -> 'b -> 'a list -> 'b
>  val 'a @ =3D fn : 'a list * 'a list -> 'a list
>  val ('a, 'b) foldr =3D fn : ('a * 'b -> 'b) -> 'b -> 'a list -> 'b
> File "/tmp/sml2231HHf", line 47, characters 4-49:
> ! fun (CarC(X(x1),Y(y1)) CarCAdd CarC(X(x2),Y(y2))) =3D  =

> CarC(X(x1+x2),Y(y1+y2))
> !     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ (*these ^ are pointing up to*)
> ! Ill-formed left hand side of a clause                         (*but
> not including the  =3D   *)
> [closing file "/tmp/sml2231HHf"]
>
>
>
> kind regards Jon Loldrup
>
> -------------------------------------------------------------------------=
-----
> Throughout its 18-year history, RSA Conference consistently attracts  =

> the
> world's best and brightest in the field, creating opportunities for  =

> Conference
> attendees to learn about information security's most important  =

> issues through
> interactions with peers, luminaries and emerging and established  =

> companies.
> http://p.sf.net/sfu/rsaconf-dev2dev
> _______________________________________________
> Smlnj-list mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/smlnj-list


---------------------------------------------------------------------------=
---
Throughout its 18-year history, RSA Conference consistently attracts the
world's best and brightest in the field, creating opportunities for Confere=
nce
attendees to learn about information security's most important issues throu=
gh
interactions with peers, luminaries and emerging and established companies.
http://p.sf.net/sfu/rsaconf-dev2dev