- marlenemiller71 a écrit :
> Would you please show an example of multiple-matching?

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

>

> expr ::= fun multiple-matching

> multiple-matching ::= {parameter}+ -> expr

> parameter ::= pattern

"+" 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

-- - 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 - marlenemiller71 a écrit :

...> I see the difference now:

maybe the WP article on currying will interest you:

>

> fun p1 ... pn -> expr

> transforms to

> fun p1 -> ... fun pn -> expr

>

> whereas

> function p1 -> expr1 | ... | pn -> exprn

http://en.wikipedia.org/wiki/Currying

the french page provides an OCaml example.

Currying allows the partial application

-- - Florent Monnier wrote:
> maybe the WP article on currying will interest you:

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

> http://en.wikipedia.org/wiki/Currying

>

> the french page provides an OCaml example.

>

> Currying allows the partial application

# 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. > > maybe the WP article on currying will interest you:

alternative patterns with "function" works exactly like the structure

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

"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 ;;

and of course you can mix a curry part with a pure function part,

> val qadd : int * int list -> int = <fun>

the previous example would then become:

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

--