[plt-scheme] contract & tail recursion

From: John Clements (clements at brinckerhoff.org)
Date: Fri Sep 12 03:18:17 EDT 2008

On Sep 11, 2008, at 9:55 PM, YC wrote:

> Hi -
>
> Question regarding contracts:
>
> I am reading up on contracts, and I just found that while `any` is  
> tail recursive, `any/c` is not.  Since any/c matches one and only  
> one return value and is not tail recursive, what about other tests  
> that matches one and only one value?  Such as `void?`, `null?`,  
> `list?`, etc?  Are they tail recursive?  I assume (and hope) they  
> are, is my assumption correct?

When you say that "any" is tail recursive, I believe what you  
actually mean is that a procedure that uses "any" as its result  
contract can still be tail-calling (or tail recursive, if you  
prefer).  This is because the "any" contract is satisfied by any  
value, so no check need be performed.

In other cases, though, you can't have tail-calling; this is because  
the contract that you're specifying requires a check after the return  
of the procedure that the contract is applied to.

To be more concrete:  suppose you have a function f that's wrapped  
with a number? -> number? contract.  Calls to f can never be tail  
calls, because the current continuation must be extended with a check  
that the result value is a number.

My apologies in advance if I've misunderstood your question.

John Clements

-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/pkcs7-signature
Size: 2484 bytes
Desc: not available
URL: <http://lists.racket-lang.org/users/archive/attachments/20080912/4b1d4094/attachment.p7s>

Posted on the users mailing list.