Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
* [Q] Multiple patterns in O'Caml
@ 1999-01-26 18:49 Matías Giovannini
  1999-01-27 14:02 ` Pierre Weis
  0 siblings, 1 reply; 2+ messages in thread
From: Matías Giovannini @ 1999-01-26 18:49 UTC (permalink / raw)
  To: caml-list

Hi, people.

I was wondering about multiple pattern matching using fun. Why is that,
compared to Caml Light, fun (unlike function) permits only one pattern
to be matched?
I'd think that, since type constructors and variables are in different
namespaces now, there would be no (syntactic) need for separate keywords
function and fun (as was true of Caml Light: fun couldn't distinguish
between a type constructor and a variable starting with an uppercase
letter in a pattern, hence it required every pattern to be parenthesized
to avoid ambiguities), and that fun could subsume the multiple-branch,
multiple-pattern of yore with the same elegant (and parentheses-sparing)
syntax of function.

Because, you see, I realize that the only way (other than to nest
match'es, or function's) to match multiple patterns is to use a tuple,
like this:

(* In Caml Light *)
let rec drop = fun
| 0 l -> l
| n [] -> failwith "drop"
| n (a::x) -> drop (pred n) x
;;

(* In O'Caml *)
let rec drop n l = match (n,l) with
| 0,l -> l
| n,[] -> failwith "drop"
| n,a::x -> drop (pred n) x
;;

but this requires an extraneous tuple construction and its subsequent
destruction, and kinda seems wasteful to me.

Thank you in advance, best regards
Matías.




^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [Q] Multiple patterns in O'Caml
  1999-01-26 18:49 [Q] Multiple patterns in O'Caml Matías Giovannini
@ 1999-01-27 14:02 ` Pierre Weis
  0 siblings, 0 replies; 2+ messages in thread
From: Pierre Weis @ 1999-01-27 14:02 UTC (permalink / raw)
  To: matias; +Cc: caml-list

> (* In O'Caml *)
> let rec drop n l = match (n,l) with
> | 0,l -> l
> | n,[] -> failwith "drop"
> | n,a::x -> drop (pred n) x
> ;;
> 
> but this requires an extraneous tuple construction and its subsequent
> destruction, and kinda seems wasteful to me.
> 
> Thank you in advance, best regards
> Matías.

There is no spurious construction of tuples nor subsequent destuction
when matching an immediate tuple: this is optimised by the compiler of
Objective Caml. This way you can consider writing

match x, y, z with
| 1, 2, 3 -> ...
...

as a syntactic notation for ``parallel matching'', or matching of more
than one value at a time.

Pierre Weis

INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://cristal.inria.fr/~weis/





^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~1999-01-27 18:28 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-01-26 18:49 [Q] Multiple patterns in O'Caml Matías Giovannini
1999-01-27 14:02 ` Pierre Weis

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox