Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

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: 2003-05-12 (15:24)
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 Archives:
Bug reports: FAQ:
Beginner's list: