[plt-scheme] SRFI 31 rec and dotted arglist

From: Jens Axel Søgaard (jensaxel at soegaard.net)
Date: Sun Nov 13 14:20:03 EST 2005

> The previous implementation of SRFI 31 `rec' (presumably the reference
> implementation) allowed a dotted argument list for the procedure form.
> The current implementation does not.  I was going to submit this as a
> bug, but I noticed that the syntax specification in the SRFI requires
> that the argument list be a proper list.  Is there a reason to require
> this?

None - I simply followed the specification closely, when I made
the version with improved error messages.

The specification says:

    1. <rec expression> --> (rec <variable> <expression>)
    2. <rec expression> --> (rec (<variable>+) <body>)

and you need

    3. <rec expression> --> (rec (<variable>+ . variable) <body>)

Adding a single clause to

    <http://svn.plt-scheme.org/plt/trunk/collects/srfi/31/rec.ss>

will allow that possibility:

(module rec mzscheme
   (provide rec)
   (define-syntax (rec stx)
     (syntax-case stx ()
       ; 1.
       [(rec id expr)
        (identifier? #'id)
        #`(letrec ((id expr))
            #,(syntax-property #'expr 'inferred-name (syntax-e #'id)))]
       ; 2.
       [(rec (name id ...) body ...)
        (andmap identifier? (syntax->list #'(name id ...)))
        #`(letrec ((name (lambda (id ...) body ...)))
            #,(syntax-property #'name 'inferred-name (syntax-e #'name)))]
        ; 3.
        [(rec (name id ... . more) body ...)
         (andmap identifier? (syntax->list #'(name id ...)))
          #`(letrec ((name (lambda (id ... . more) body ...)))
            #,(syntax-property #'name 'inferred-name (syntax-e #'name)))]
        [_
         (raise-syntax-error
           #f "expects either a variable followed by an expresion, or a
               list of variables followed by a body" stx)]))
)

Here is a test case:

(require (lib "31.ss" "srfi" ))
(define s (rec (sum . xs)
             (cond
               [(null? xs) 0]
               [else       (+ (first xs)
                              (apply sum (rest xs)))])))

(s)        ; => 0
(s 1)      ; => 1
(s 1 2)    ; => 3
(s 1 2 3)  ; => 6


I see no harm in allowing 4.

-- 
Jens Axel Søgaard




Posted on the users mailing list.