[plt-scheme] contract & tail recursion

From: Robby Findler (robby at cs.uchicago.edu)
Date: Fri Sep 12 14:57:49 EDT 2008

On Fri, Sep 12, 2008 at 1:55 PM, YC <yinso.chen at gmail.com> wrote:
> On Fri, Sep 12, 2008 at 12:18 AM, John Clements <clements at brinckerhoff.org>
> wrote:
>> 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.
> Thanks for the response. I apologize for my unclear writing.
> What you say makes sense, to me it means that if we care about space
> efficiency we cannot use anything besides `any` for the result contract,
> unless we can foresee how the function would be used ahead of time, or have
> a way to conditionally circumvent contracts.

I don't think it is quite that dire! For example, I care about space
efficiency, but still use result contracts all the time.

In general, if the function is not involved in loops, you're fine.
Also note that contracts are not checked "internally" to a module. So
a recursive function with a result contract is still going to be fine.


Posted on the users mailing list.