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

Re: "ocaml_beginners"::[] fun multiple-matching

Expand Messages
  • Florent Monnier
    ... (fun (a,b) c - a + b + c) + expresses that there can be several parameters, and parameter can be any pattern from:
    Message 1 of 6 , Jun 1, 2008
    • 0 Attachment
      marlenemiller71 a écrit :
      > Would you please show an example of multiple-matching?
      >
      > expr ::= fun multiple-matching
      > multiple-matching ::= {parameter}+ -> expr
      > parameter ::= pattern

      (fun (a,b) c -> a + b + c)

      "+" expresses that there can be several parameters,
      and parameter can be any pattern from:
      http://caml.inria.fr/pub/docs/manual-ocaml/patterns.html
      which means a constructed type can be matched unconstructed,
      for exemple:

      type 'a weird = { fst: 'a; last: 'a * ('a * 'a) }

      (fun {fst=a; last= b,(c,d) } -> a + b + c + d)

      Though not exhaustive pattern-matching will raise a Warning, see:

      # let f = (fun (h::t) b -> h + b) ;;
      Warning P: this pattern-matching is not exhaustive.
      Here is an example of a value that is not matched:
      []
      val f : int list -> int -> int = <fun>
      # f [1] 2 ;;
      - : int = 3
      # f [] 2 ;;
      Exception: Match_failure ("", 1, 8).


      You cannot make alternative matching as with the keyword "function" wich
      accept only one parameter (which can be a n-uplet):

      let head = function h::t -> Some h | [] -> None ;;

      # let qadd = function (v, h::t) -> v + h | (v, []) -> v ;;
      val qadd : int * int list -> int = <fun>
      # qadd (4, [1;2]) ;;
      - : int = 5
      # qadd (4, []) ;;
      - : int = 4

      --
    • marlenemiller71
      ... Thank you, very helpful. I see the difference now: fun p1 ... pn - expr transforms to fun p1 - ... fun pn - expr whereas function p1 - expr1 | ... | pn
      Message 2 of 6 , Jun 1, 2008
      • 0 Attachment
        Florent Monnier wrote:

        > (fun (a,b) c -> a + b + c)
        > [...]

        Thank you, very helpful.

        I see the difference now:

        fun p1 ... pn -> expr
        transforms to
        fun p1 -> ... fun pn -> expr

        whereas
        function p1 -> expr1 | ... | pn -> exprn
      • Florent Monnier
        marlenemiller71 a écrit : ... maybe the WP article on currying will interest you: http://en.wikipedia.org/wiki/Currying the french page provides an OCaml
        Message 3 of 6 , Jun 1, 2008
        • 0 Attachment
          marlenemiller71 a écrit :
          ...
          > I see the difference now:
          >
          > fun p1 ... pn -> expr
          > transforms to
          > fun p1 -> ... fun pn -> expr
          >
          > whereas
          > function p1 -> expr1 | ... | pn -> exprn

          maybe the WP article on currying will interest you:
          http://en.wikipedia.org/wiki/Currying

          the french page provides an OCaml example.

          Currying allows the partial application

          --
        • marlenemiller71
          ... I was confused about this example in the book by Cousineau and Mauny: # fun true true - true | _ _ - false;; Syntax error Apparently the language has
          Message 4 of 6 , Jun 1, 2008
          • 0 Attachment
            Florent Monnier wrote:
            > maybe the WP article on currying will interest you:
            > http://en.wikipedia.org/wiki/Currying
            >
            > the french page provides an OCaml example.
            >
            > Currying allows the partial application

            I was confused about this example in the book by Cousineau and Mauny:

            # fun true true -> true | _ _ -> false;;
            Syntax error

            Apparently the language has changed since the book was written.

            It didn't occur to me that multiple-matching meant the "shorthand"
            syntax for currying. I know about currying from Scheme.

            The example was perfect.
          • Florent Monnier
            ... alternative patterns with function works exactly like the structure match v with you cannot match several arguments as in match v1 v2 with , you need
            Message 5 of 6 , Jun 2, 2008
            • 0 Attachment
              > > maybe the WP article on currying will interest you:
              > > http://en.wikipedia.org/wiki/Currying
              > >
              > > the french page provides an OCaml example.
              > >
              > > Currying allows the partial application
              >
              > I was confused about this example in the book by Cousineau and Mauny:
              >
              > # fun true true -> | _ _ -> false;;
              > Characters 17-18:
              > fun true true -> | _ _ -> false;;
              > ^
              > Syntax error
              >
              > Apparently the language has changed since the book was written.

              alternative patterns with "function" works exactly like the structure
              "match v with"
              you cannot match several arguments as in "match v1 v2 with", you need to group
              them in a n-uple "match v1, v2 with", so your example would become:

              # function (true, true) -> expr1 | (_,_) -> expr2 ;;


              There would have been several advance options that could have been done to
              make pattern matching work with very advanced options and more complex ways,
              but it would have had a cost, for only been used 1% of the time.
              So the choice have been made to make pattern matching the most efficient to
              feet the 99% of the needs.


              > # let qadd = function (v, h::t) -> v + h | (v, []) -> v ;;
              > val qadd : int * int list -> int = <fun>

              and of course you can mix a curry part with a pure function part,
              the previous example would then become:

              let qadd v = function h::t -> v + h | [] -> v ;;

              --
            Your message has been successfully submitted and would be delivered to recipients shortly.