Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] Pattern matching over elements at the front of a container
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Luc Maranget <Luc.Maranget@i...>
Subject: Re: [Caml-list] Pattern matching over elements at the front of a container
Hello,
> 
> I'm currently storing stuff and things in a list. The ability to pattern match 
> over the first element or two using a succinct notation is, of course, nifty. 
> However, I'd like to replace my list with some other container, maybe an 
> array, maybe something of my own creation. What's the easiest way to keep my 
> ability to pattern match over the first few elements at the front of the 
> container? I could try to extract the first couple of elements and match over 
> a 2-tuple of "element option"s. Can a lazily evaluated stream help? How 
> expensive is this in terms of performance?
Well, have a try, you may pay a little in performance, most of the time it
does not matter.


> 
> In the case of lists, you can match against the first two elements using the 
> pattern "a::b::_". As the "_" is not bound in the corresponding expression, 
> an equivalent notation could be invented for arrays in this case. Is this 
> feasible? Would anyone else find this useful?
It is certainly feasible, whether it is worth including in the compiler is debatable...


> 
> Also, is it not possible to alter the definition and implementation of OCaml 
> such that the pattern "(a, a)" is treated as "(a, b) when a=b"? Has this not 
> been done because the "=" is suspect?

Just say No (to non-linear patterns) !
Basically and you can interpret this by saying << = is suspect >>, this apparently innocent
addition is a radical change in semantics.


> 
> Cheers,
> Jon.
> 
> -------------------
> To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
> Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners

-- 
Luc Maranget

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners