"ocaml_beginners"::[] Error about arguments

View: New views
5 Messages — Rating Filter:   Alert me  

"ocaml_beginners"::[] Error about arguments

by artemis01 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

    Hello Ocaml users,

  The following code works well:


let test s =
  (match s with
     | "plus" -> (+)
     | "minus" -> (-)
     | _ -> failwith "Error") 3 2;;


but I do not understand why this one does not:


type term =
  | Int of int
  | Plus of term * term
  | Minus of term * term;;


let test2 s =
  (match s with
     | "plus" -> Plus
     | "minus" -> Minus
     | _ -> failwith "Error") (Int 3) (Int 2);;



I get the following error:
Error: The constructor Plus expects 2 argument(s), but is applied here
to 0 argument(s)

Thanks,
--
Chantal KELLER

"ocaml_beginners"::[] Re: Error about arguments

by Sylvain Le Gall-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 05-10-2009, Chantal KELLER <chantal.keller@...> wrote:

> --n6LSggof4KROBmCwUop-d4R2605Ep7seT1f8jGs
> Content-Type: text/plain; charset=UTF-8
> Content-Transfer-Encoding: 7bit
>
>     Hello Ocaml users,
>
>   The following code works well:
>
>
> let test s =
>   (match s with
>      | "plus" -> (+)
>      | "minus" -> (-)
>      | _ -> failwith "Error") 3 2;;
>
>
> but I do not understand why this one does not:
>
>
> type term =
>  | Int of int
>  | Plus of term * term
>  | Minus of term * term;;
>
>
> let test2 s =
>   (match s with
>      | "plus" -> Plus
>      | "minus" -> Minus
>      | _ -> failwith "Error") (Int 3) (Int 2);;
>
>
>
> I get the following error:
> Error: The constructor Plus expects 2 argument(s), but is applied here
> to 0 argument(s)
>

Variant type constructor (i.e. Plus/Minus/Int) are not functions.

Use it directly

let test2 =
  function
    | "plus" -> Plus(Int 2, Int 3)
    | "minus" -> Minus(Int 2, Int 3)
    |  _ -> ...


or create functions to build them:

let plus  x y = Plus(x, y)
let minus x y = Minus(x, y)

let test2 s =
  (match s with
     | "plus" -> plus
     | "minus" -> minus
     | _ -> failwith "Error") (Int 3) (Int 2)

Regards
Sylvain Le Gall


Re: "ocaml_beginners"::[] Re: Error about arguments

by artemis01 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Sylvain Le Gall a écrit :
>  
> Variant type constructor (i.e. Plus/Minus/Int) are not functions.

Is there a good reason for that?
--
Chantal KELLER

"ocaml_beginners"::[] Re: Error about arguments

by Sylvain Le Gall-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 05-10-2009, Chantal KELLER <chantal.keller@...> wrote:
> --6TaNalhyYyTdoQP4Ivu4OgVo6Bz7kipsmf6mTVw
> Content-Type: text/plain; charset=ISO-8859-1
> Content-Transfer-Encoding: 8bit
>
> Sylvain Le Gall a écrit :
>>  
>> Variant type constructor (i.e. Plus/Minus/Int) are not functions.
>
> Is there a good reason for that?

I think it is related to the fact that it can be used for
pattern-matching (which cannot support function as pattern).

I.e. you can do:
match x with Plus(_,_) -> ...

Regards
Sylvain Le Gall


Re: "ocaml_beginners"::[] Re: Error about arguments

by Richard Jones-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Mon, Oct 05, 2009 at 12:21:53PM +0200, Chantal KELLER wrote:
> Sylvain Le Gall a écrit :
> >  
> > Variant type constructor (i.e. Plus/Minus/Int) are not functions.
>
> Is there a good reason for that?

There's a good question.  In ML those constructors _are_
functions.  This is a question for the main list.

Rich.

--
Richard Jones
Red Hat