Version française
Home     About     Download     Resources     Contact us    
Browse thread
Warning on not-tail recursive functions
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: -- (:)
From: Brian Hurt <bhurt@s...>
Subject: Re: [Caml-list] Warning on not-tail recursive functions


On Sat, 4 Aug 2007, tmp123@menta.net wrote:

> Hello,
>
> Please, knows someone if "ocamlopt" can print a warning message when a 
> recursive function is not tail recursive, and code with the "jmp" 
> optimization has not been generated?.

The problem is that I often write recursive functions which are not tail 
recursive, and that's OK.  As an example, take a look at the join (or 
filter) functions I wrote in priority queue implementation in my previous 
email (subject line: "Sorted list").  They're not tail recursive, but 
since they only go O(log N) deep, that's not a problem.

The rules for what is or is not tail recursive are pretty simple.  Boiled 
down, they are:
1) The tail call must not be within a try expression
2) You can not do anything except return, uninspected and unmodified, the 
returned value from the call.

Given that the rules are simple and local, I can generally just tell, by 
looking at the call, wether it's a tail call or not.

Brian