Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
From: Brian Hurt <bhurt@spnz.org>
To: Nicolas Cannasse <warplayer@free.fr>
Cc: Richard Jones <rich@annexia.org>, caml-list <caml-list@inria.fr>
Subject: Re: [Caml-list] List.rev
Date: Sun, 2 May 2004 11:04:45 -0500 (CDT)	[thread overview]
Message-ID: <Pine.LNX.4.44.0405021053020.9460-100000@localhost.localdomain> (raw)
In-Reply-To: <004d01c42f64$731704e0$19b0e152@warp>

On Sat, 1 May 2004, Nicolas Cannasse wrote:

> > Are there automatic ways to transform non-tail-recursive functions
> > into tail-recursive ones?
> >
> > Rich.
> 
> You can have a look at ExtLib sources. We provide tail-recursive
> implementations for each List operations (with same "little o" complexity).
> 

This is actually quite bad advice (sorry, Nicolas)- many of the "tricks" 
we do in Ext-Lib are not for newbies.  I'm thinking specifically of the 
Obj.magic stuff.  If you find yourself doing this anywhere else, you are 
almost certainly screwing up.

There is a fairly standard set of tricks I use to turn non-tail-recursive 
functions into tail-recursive functions.  The two most important ones are:

1) build lists backwards, then reverse them when they're done.  For 
example, List.append could be implemented:
	let append alist blist =
		let revlist = List.rev_append blist (List.rev alist) in
		List.rev revlist
	;;

2) Hoist recursive calls out of try/catch clauses, introducing variables
to detect when an exception was thrown.  For example, to read all the 
lines of a channel into a list of strings, do:
	let readfile chan = 
		let rec loop accum =
			let eof, line = 
				try
					false, (input_line chan)
				with
					| End_of_file ->
						true, ""
			in
			if (eof) then
				List.rev accum
			else
				loop (line :: accum)
		in
		loop []
	;;


-- 
"Usenet is like a herd of performing elephants with diarrhea -- massive,
difficult to redirect, awe-inspiring, entertaining, and a source of
mind-boggling amounts of excrement when you least expect it."
                                - Gene Spafford 
Brian

-------------------
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:[~2004-05-02 15:59 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-04-30 17:54 [Caml-list] "List.index" or "List.unique" functions? Rahul Siddharthan
2004-04-30 18:51 ` Martin Jambon
2004-04-30 19:01 ` Benjamin Geer
2004-04-30 19:07   ` Thanks " Rahul Siddharthan
2004-04-30 19:08 ` Karl Zilles
2004-04-30 19:29   ` Matthieu Sozeau
2004-04-30 20:01     ` Karl Zilles
2004-04-30 20:05   ` Remi Vanicat
2004-04-30 20:47     ` JM Nunes
2004-04-30 20:58       ` Karl Zilles
2004-05-01  1:59   ` [Caml-list] List.rev skaller
2004-05-01  4:18     ` Jon Harrop
2004-05-01  4:38     ` brogoff
2004-05-01  5:12       ` skaller
2004-05-01  7:08         ` William Lovas
2004-05-01  8:10           ` skaller
2004-05-01  8:32             ` Jon Harrop
2004-05-01  9:24               ` skaller
2004-05-02 12:07             ` Andreas Rossberg
2004-05-02 13:29               ` skaller
2004-05-01 10:07         ` Richard Jones
2004-05-01 10:09           ` Nicolas Cannasse
2004-05-02 16:04             ` Brian Hurt [this message]
2004-05-01 10:32           ` Jon Harrop
2004-05-01 16:41     ` John Goerzen
2004-05-01 19:11       ` skaller
2004-05-01 10:03 ` [Caml-list] "List.index" or "List.unique" functions? Richard Jones

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=Pine.LNX.4.44.0405021053020.9460-100000@localhost.localdomain \
    --to=bhurt@spnz.org \
    --cc=caml-list@inria.fr \
    --cc=rich@annexia.org \
    --cc=warplayer@free.fr \
    /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