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

Error about arguments

Expand Messages
  • Chantal KELLER
    Hello Ocaml users, The following code works well: let test s = (match s with ... but I do not understand why this one does not: type term = ... let test2 s =
    Message 1 of 5 , Oct 5, 2009
    • 0 Attachment
      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
    • Sylvain Le Gall
      ... Variant type constructor (i.e. Plus/Minus/Int) are not functions. Use it directly let test2 = function ... or create functions to build them: let plus x y
      Message 2 of 5 , 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
      • Chantal KELLER
        ... Is there a good reason for that? -- Chantal KELLER
        Message 3 of 5 , Oct 5, 2009
        • 0 Attachment
          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
        • Sylvain Le Gall
          ... 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
          Message 4 of 5 , Oct 5, 2009
          • 0 Attachment
            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
          • Richard Jones
            ... There s a good question. In ML those constructors _are_ functions. This is a question for the main list. Rich. -- Richard Jones Red Hat
            Message 5 of 5 , Oct 5, 2009
            • 0 Attachment
              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
            Your message has been successfully submitted and would be delivered to recipients shortly.