English version
Accueil     À propos     Téléchargement     Ressources     Contactez-nous    

Ce site est rarement mis à jour. Pour les informations les plus récentes, rendez-vous sur le nouveau site OCaml à l'adresse ocaml.org.

Browse thread
A basic question
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-12-30 (10:28)
From: maranget@y...
Subject: Re: [Caml-list] A basic question
> Hi, all,
> I'm a newcomer here.
> I don't understand why the following two
>     let [x;y;z] = [1;2;3] ;;
> and
>     let [x;y;z] = [1;2;3;4] ;;
> have warnings: "this pattern-matching is not exhaustive".
> I think they just setting values for x, y, and z.
> Could anyone pls tell me why?
> Thanks.
> Kathy

There is nothing special with 'let pat = e1 in e2' bindings,
they get translated to 'match e1 with pat -> e2'
[Toplevel bindings 'let p = e ;;' are similar]

Then, usual pattern matching analysis is performed.

So your question can be reformulated for instance as
'match [1;2;3] with [x;y;z] -> ....'
'match [1;2;3;4] with [x;y;z] -> ....'
yield "this pattern-matching is not exhaustive" warnings.

First notice that running the second example results in a match failure.
(I mean, the warning is useful).

Then, one can argue that, since the matched expression is constant,
matching result can be known by the compiler and that it  should
produce no warning in the first case and produce an error in the second case.
One can also argue, as Skaller does that a more sophisticated typing system
would behave similarily whatever the matched expression is.

Well, the compiler is too lazy ! So it produces a warning in both examples !

Finally as said by other contributors, a good technique for performing
several bindings at time is using tuples:

let (x,y,z) = (1,2,3);;

Hope it helps,

-- Luc Maranget