Loading ...
Sorry, an error occurred while loading the content.

11448Re: Error about arguments

Expand Messages
  • Sylvain Le Gall
    Oct 5, 2009
    • 0 Attachment
      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
    • Show all 5 messages in this topic