- 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 - On 05-10-2009, Chantal KELLER <chantal.keller@...> wrote:
> --n6LSggof4KROBmCwUop-d4R2605Ep7seT1f8jGs

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

> 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)

>

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 - On 05-10-2009, Chantal KELLER <chantal.keller@...> wrote:
> --6TaNalhyYyTdoQP4Ivu4OgVo6Bz7kipsmf6mTVw

I think it is related to the fact that it can be used for

> 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?

pattern-matching (which cannot support function as pattern).

I.e. you can do:

match x with Plus(_,_) -> ...

Regards

Sylvain Le Gall - On Mon, Oct 05, 2009 at 12:21:53PM +0200, Chantal KELLER wrote:
> Sylvain Le Gall a écrit :

There's a good question. In ML those constructors _are_

> >

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

>

> Is there a good reason for that?

functions. This is a question for the main list.

Rich.

--

Richard Jones

Red Hat