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

Re: "ocaml_beginners"::[] syntax problem: if vs match with

Expand Messages
  • Sebastien Mondet
    Hi * In short: no you cannot do that. * Tentative explanation: when you say: match x with ... you ask the match construct to bind lowercase_identifier to the
    Message 1 of 2 , Nov 1, 2012
    • 0 Attachment
      Hi

      * In short:
      no you cannot do that.

      * Tentative explanation:

      when you say:

      match x with
      | Var (lowercase_identifier) -> ...

      you ask the match construct to bind lowercase_identifier to the contents of
      x, so after the arrow you can use "lowercase_identifier" (like if it was a
      "let lowercase_identifier = ... in ... " construct)

      when you say

      if x = Var (lowercase_identifier) then ...

      you ask for the evaluation of the boolean expression "x = Var
      (lowercase_identifier)", so ocaml is going to look for lowercase_identifier
      in the current environment.


      hope it helps
      Seb



      On Thu, Nov 1, 2012 at 2:06 PM, George <siberianowl@...> wrote:

      > I have a type:
      > type operator = Plus | Minus | Var of string;
      > And I have an array of this type. Now I need to check all elements of this
      > array, but the work should be done only on the Var(string) elements:
      >
      > Array.iter (fun x ->
      > match x with
      > Var(name) -> print_endline(name)
      > | _ -> ()
      > ) array_with_operators
      >
      > Is it possible, to use IF instead of MATCH WITH? I'd like to do something
      > like:
      >
      > Array.iter (fun x ->
      > if x = Var(name) then print_endline(name)
      > ) array_with_operators
      >
      > but ocamlc complains that this is a syntax error.
      >
      >
      >
      > ------------------------------------
      >
      > Archives up to December 31, 2011 are also downloadable at
      > http://www.connettivo.net/cntprojects/ocaml_beginners
      > The archives of the very official ocaml list (the seniors' one) can be
      > found at http://caml.inria.fr
      > Attachments are banned and you're asked to be polite, avoid flames
      > etc.Yahoo! Groups Links
      >
      >
      >
      >


      [Non-text portions of this message have been removed]
    Your message has been successfully submitted and would be delivered to recipients shortly.