Next: , Previous: , Up: srfi rec   [Index]


2.18.4 Specification

Syntax

The following production rules are to be added to those of [KCR1998] (we reuse names of non–terminals).

<derived expression> --> <rec expression>
<rec expression>     --> (rec <variable>    <expression>)
<rec expression>     --> (rec (<variable>+) <body>)

Semantics

Scheme versions such as [KCR1998] providing define-syntax, syntax-rules, letrec and lambda might implement rec as follows.

(define-syntax rec
  (syntax-rules ()
    [(rec (NAME . VARIABLES) . BODY)
     (letrec ( (NAME (lambda VARIABLES . BODY)) ) NAME)]
    [(rec NAME EXPRESSION)
     (letrec ( (NAME EXPRESSION) ) NAME)]))

Test

The following session shows in which way rec allows a tail–recursive implementation of the factorial function.

> (define F (rec (F N)
                ((rec (G K L)
                   (if (zero? K) L
                     (G (- K 1) (* K L)))) N 1)))
> F
#<procedure>
> (F 0)
1
> (F 10)
3628800