Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Michal Moskal <malekith@pld-linux.org>
To: John Carr <jfc@MIT.EDU>
Cc: Brian Hurt <brian.hurt@qlogic.com>, caml-list@inria.fr
Subject: Re: [Caml-list] tree walking with... specular rec functions? what else?
Date: Mon, 12 May 2003 15:35:42 +0200	[thread overview]
Message-ID: <20030512133542.GA9977@roke.freak> (raw)
In-Reply-To: <200305121257.IAA29080@nerd-xing.mit.edu>

On Mon, May 12, 2003 at 08:57:05AM -0400, John Carr wrote:
> 
> I have a minor comment unrelated to the basic algorithm.
> 
> >     ...
> >     let rec outer lst =
> >         if (List.length lst) == 0 then ()
> >         else outer (inner lst [])
> >     in
> >     ...
> 
> In general it is better to test for empty rather than compare the size
> of a collection to 0.  With some collections (including lists) testing
> for empty takes constant time but computing size takes linear time.
> 
> Instead of
> 
> 	if (List.length lst) == 0 then empty-code else full-code
> 
> either of these will be faster:
> 
> 	if lst == [] then empty-code else full-code

    On non-mutable structures, the behavior of (==) is
    implementation-dependent.

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.

-- 
: Michal Moskal :: http://www.kernel.pl/~malekith : GCS {C,UL}++++$ a? !tv
: PLD Linux ::::::::: Wroclaw University, CS Dept : {E-,w}-- {b++,e}>+++ h

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


  reply	other threads:[~2003-05-12 13:36 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-05-09  9:56 Stalkern 2
2003-05-09 15:23 ` Brian Hurt
2003-05-12 12:57   ` John Carr
2003-05-12 13:35     ` Michal Moskal [this message]
2003-05-12 15:06       ` David Brown
2003-05-12 15:24         ` Xavier Leroy
2003-05-11 16:02 ` Xavier Leroy
2003-05-12  6:55   ` Stalkern 2
2003-05-12 10:14     ` Gérard Huet

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20030512133542.GA9977@roke.freak \
    --to=malekith@pld-linux.org \
    --cc=brian.hurt@qlogic.com \
    --cc=caml-list@inria.fr \
    --cc=jfc@MIT.EDU \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox