Version française
Home     About     Download     Resources     Contact us    
Browse thread
[Caml-list] tree walking with... specular rec functions? what else?
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Xavier Leroy <xavier.leroy@i...>
Subject: Re: [Caml-list] tree walking with... specular rec functions? what else?
> > Which means that lst = [] does not imply lst == [].
> > 
> > In other words, one should use:
> > 
> >  	if lst = [] then empty-code else full-code
> > 
> > or pattern matching, as you said.
> 
> Is it really not defined by Ocaml?  Ocaml implements the empty list as
> the integer value zero.  Although (==) won't tell you if two cons-cells
> have the same contents, it will tell you if they are the same.

It happens to work in this case, but relies on features of the
implementation.  It's better to use "==" only between mutable data
structures, where it has a well-defined meaning (two mutable
structures are == iff an in-place modification to one of them affects
the other one as well), and "=" otherwise.

> So is there any implementation of a caml language where [] == [] isn't
> always true, for any way that [] is generated?

Both Caml Light and OCaml ensure [] == [], but again it's better not
to rely on this fact.

> The (=) has more overhead

It doesn't: the OCaml compiler is clever enough to compile "lst = []"
as a direct comparison against the integer value zero.

- Xavier Leroy

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