Version française
Home     About     Download     Resources     Contact us    
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: -- (:)
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.