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 (03:30)
From: Sébastien Hinderer <Sebastien.Hinderer@e...>
Subject: Re: [Caml-list] A basic question
Hi,

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

This is because, for pattern-matching to work, the two lists _must_
have the same length, so that the nth element in the right-hand list can
be assigned to the nth variable specified in the left-hand list.
But, the compiler has no way to check that in every case, hence this warning.

> I think they just setting values for x, y, and z.

Of course, it is probably possible to find some heuristics to check the
equality between lengths, making your first example work, but it'd be
tricky.
Concerning your second example, it is not that obvious to attach a good
semantics to what you write, because the lists are of different lengths.

Now, if the only thing you want to do is to assign values to several
variables at the same time, you can do something like : 
let (x, y z) = (1, 2, 3);;

Of course, the types of (x y, z) and (1, 2, 3) must be compatible.
So, a phrase like 

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

is wrong.

hth,
Sébastien.