A basic question
[
Home
]
[ Index:
by date

by threads
]
[ Message by date: previous  next ] [ Message in thread: previous  next ] [ Thread: previous  next ]
[ Message by date: previous  next ] [ Message in thread: previous  next ] [ Thread: previous  next ]
Date:  20041230 (03:38) 
From:  skaller <skaller@u...> 
Subject:  Re: [Camllist] A basic question 
On Thu, 20041230 at 13:47, Kathy Chen wrote: > 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 patternmatching is not exhaustive". > I think they just setting values for x, y, and z. > > Could anyone pls tell me why? It's a shortcoming of the type system. The RHS is just a list. The fact it has 3 elements in it is lost by the type system. The let is then matching a list of 3 elements against a list of unknown number of elements.. which isn't exhaustive. The same example using tuples will work: let x,y,z = 1,2,3 without a warning because the number (and type) of elements in a tuple is known to the type system. The difficulty comes from the 'weak' interpretation of recursive types as the union of all expansions. For 'lists' a stronger interpretation would be useful, and is equivalent to the notion of an array of some length (however Ocaml arrays are distinct types which don't have lengths either). C++ therefore has a 'stronger' type system in this respect, since arrays of definite length exist. However, the generalisation to arbitrary inductive types is probably not so useful as arrays: the length of an array is a single value, to describe the structure of a finite tree would require a lot of extra data in the type system. FYI: as an experiment, Felix has algebraic arrays. They are identical to tuples of n elements from a typing viewpoint (however internally the representation is distinct, and externally the generated code is too).  John Skaller, mailto:skaller@users.sf.net voice: 061296600850, snail: PO BOX 401 Glebe NSW 2037 Australia Checkout the Felix programming language http://felix.sf.net