Matt Wette <[email protected]> writes:

> Is the following (incomplete) construct tail recursive?  The name-call
> is wrapped inside a "trivial" closure.
>
>  (let ITER ((res '()) (st 1) (nd 1))
>       (cond
>        ((string-contains str "," st) => (lambda (pos) (ITER res st pos)))))
>
> I tried to use "(compile 'above :from 'scheme :to 'xlil)" (and
> others), but the code was not readable for me.
>
> Matt

As per section "3.5. Proper tail recursion" of R5RS, page 8:

- If a cond expression is in tail context, and has a clause of the form
  (<expression1> => <expression2>) then the (implied) call to the
  procedure that results from the evaluation of <expression2> is in a
  tail context.

and the call to ITER within that procedure is, in turn, in tail context,
so yes, execution will jump from cond's code to your lambda's code,
which will in turn jump to ITER's code, all without stacking up frames.

Taylan

  • Q on tail-call Matt Wette
    • Re: Q on tail-call Taylan Ulrich Bayırlı/Kammer

Reply via email to