caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] How to find out if a function is tail recursive?
@ 2003-06-12 14:15 Richard Jones
  2003-06-12 14:31 ` Paul Steckler
                   ` (4 more replies)
  0 siblings, 5 replies; 10+ messages in thread
From: Richard Jones @ 2003-06-12 14:15 UTC (permalink / raw)
  To: caml-list

I was writing the section on tail recursion in the OCaml tutorial, and
was surprised to find out that the range function (below) isn't tail
recursive. Or at least it causes a stack overflow on a
large-but-not-unreasonable input value.

let rec range a b =
  if a > b then []
  else a :: range (a+1) b
  ;;

let list = range 1 1000000;;

Printf.printf "length = %d\n" (List.length list);;

Can you tell me why this function isn't tail recursive, and share any
useful tips on how to tell whether a function is or is not tail
recursive?

Thanks,

Rich.

-- 
Richard Jones, Red Hat Inc. (London) and Merjis Ltd. http://www.merjis.com/
http://www.annexia.org/ Freshmeat projects: http://freshmeat.net/users/rwmj
MONOLITH is an advanced framework for writing web applications in C, easier
than using Perl & Java, much faster and smaller, reusable widget-based arch,
database-backed, discussion, chat, calendaring:
http://www.annexia.org/freeware/monolith/

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


^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2003-06-13  7:23 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-06-12 14:15 [Caml-list] How to find out if a function is tail recursive? Richard Jones
2003-06-12 14:31 ` Paul Steckler
2003-06-12 14:43   ` Luc Maranget
2003-06-12 16:15     ` John Max Skaller
2003-06-12 16:37       ` John Max Skaller
2003-06-13  7:23         ` [Caml-list] This is so nice John Max Skaller
2003-06-12 14:37 ` [Caml-list] How to find out if a function is tail recursive? Wolfgang Müller
2003-06-12 14:49 ` Neel Krishnaswami
2003-06-12 15:14 ` Chris Uzdavinis
2003-06-12 15:33 ` Brian Hurt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).