# 5: A compatible let form with signatures and rest arguments

by Andy Gaynor

## Status

This SRFI is currently in final status. Here is an explanation of each status that a SRFI can hold. To provide input on this SRFI, please send email to srfi-5@nospamsrfi.schemers.org. To subscribe to the list, follow these instructions. You can access previous messages via the mailing list archive.

• Draft: 1999-02-10--1999-04-12
• Final: 1999-04-26
• Revised reference implementation: 2003-01-27

## Abstract

The named-let incarnation of the let form has two slight inconsistencies with the define form. As defined, the let form makes no accommodation for rest arguments, an issue of functionality and consistency. As defined, the let form does not accommodate signature-style syntax, an issue of aesthetics and consistency. Both issues are addressed here in a manner which is compatible with the traditional let form but for minor extensions.

## Rationale

### Signature-style Syntax

Consider the following two equivalent definitions:

(define fibonacci
(lambda (n i f0 f1)
(if (= i n)
f0
(fibonacci n (+ i 1) f1 (+ f0 f1)))))

(define (fibonacci n i f0 f1)
(if (= i n)
f0
(fibonacci n (+ i 1) f1 (+ f0 f1))))

Although there is a named-let analog for the former form, there is none for the latter. To wit, suppose one wished to compute the 10th element of the Fibonacci sequence using a named let:

(let fibonacci ((n 10) (i 0) (f0 0) (f1 1))
(if (= i n)
f0
(fibonacci n (+ i 1) f1 (+ f0 f1))))
Values: 55

As it stands, one cannot equivalently write

(let (fibonacci (n 10) (i 0) (f0 0) (f1 1))
(if (= i n)
f0
(fibonacci n (+ i 1) f1 (+ f0 f1))))

which is consistent with define's signature-style form.

Those that favor the signature style may prefer this extension. In any case, it may be more appropriate to include all bound names within the binding section. As presented, this straightforward extension introduces no ambiguity or incompatibility with the existing definition of let.

### Rest Arguments

As it stands, one cannot write a named let with rest arguments, as in

(let (blast (port (current-output-port)) . (x (+ 1 2) 4 5))
(if (null? x)
'just-a-silly-contrived-example
(begin
(write (car x) port)
(apply blast port (cdr x)))))

otherwise equivalent to

(letrec ((blast (lambda (port . x)
(if (null? x)
'just-a-silly-contrived-example
(begin
(write (car x) port)
(apply blast port (cdr x)))))))
(blast (current-output-port) (+ 1 2) 4 5))

While this example is rather contrived, the functionality is not. There are several times when the author has used this construct in practice. Regardless, there is little reason to deny the let form access to all the features of lambda functionality.

### Symbols in Binding Sections

Both the features above rely upon the placement of symbols in let binding lists (this statement is intentionally simplistic). The only other apparent use of such symbol placement is to tersely bind variables to unspecified values. For example, one might desire to use (let (foo bar baz) ...) to bind foo, bar, and baz to unspecified values.

This usage is considered less important in light of the rationales presented above, and an alternate syntax is immediately apparent, as in (let ((foo) (bar) (baz)) ...) This may even be preferable, consistently parenthesizing normal binding clauses.

## Specification

### Syntax

A formal specification of the syntax follows. Below, body, expression, and identifier are free. Each instantiation of binding-name must be unique.

let = "(" "let" let-bindings body ")"
expressions = nothing | expression expressions
let-bindings = let-name bindings
| "(" let-name "." bindings ")"
let-name = identifier
bindings = "(" ")"
| rest-binding
| "(" normal-bindings ["." rest-binding] ")"
normal-bindings = nothing
| normal-binding normal-bindings
normal-binding = "(" binding-name expression ")"
binding-name = identifier
rest-binding = "(" binding-name expressions ")"

For clarity and convenience, an informal specification follows.

1. Unnamed
(let ((<parameter> <argument>)...)
<body>...)

2. Named, non-signature-style, no rest argument
(let <name> ((<parameter> <argument>)...)
<body>...)

3. Named, signature-style, no rest argument
(let (<name> (<parameter> <argument>)...)
<body>...)

4. Named, non-signature-style, rest argument
(let <name> ((<parameter> <argument>)...

. (<rest-parameter> <rest-argument>...))
<body>...)

5. Named, signature-style, rest argument
(let (<name> (<parameter> <argument>)...

. (<rest-parameter> <rest-argument>...))
<body>...)

### Semantics

Let \$lambda and \$letrec be hygienic bindings for the lambda and letrec forms, respectively.

• For informal syntax 1:
((\$lambda (<parameter>...) <body>...) <argument>...)

• For informal syntaxes 2 and 3:
(\$letrec ((<name> (\$lambda (<parameter>...) <body>...)))
(<name> <argument>...))

• For informal syntaxes 4 and 5:
(\$letrec ((<name> (\$lambda (<parameter>...

. <rest-parameter>) <body>...)))
(<name> <argument>... <rest-argument>...))

## Implementation

Here is an implementation using SYNTAX-RULES.

;; Use your own standard let.
;; Or call a lambda.
;; (define-syntax standard-let
;;
;;   (syntax-rules ()
;;
;;     ((let ((var val) ...) body ...)
;;      ((lambda (var ...) body ...) val ...))))

(define-syntax let

(syntax-rules ()

;; No bindings: use standard-let.
((let () body ...)
(standard-let () body ...))
;; Or call a lambda.
;; ((lambda () body ...))

;; All standard bindings: use standard-let.
((let ((var val) ...) body ...)
(standard-let ((var val) ...) body ...))
;; Or call a lambda.
;; ((lambda (var ...) body ...) val ...)

;; One standard binding: loop.
;; The all-standard-bindings clause didn't match,
;; so there must be a rest binding.
((let ((var val) . bindings) body ...)
(let-loop #f bindings (var) (val) (body ...)))

;; Signature-style name: loop.
((let (name binding ...) body ...)
(let-loop name (binding ...) () () (body ...)))

;; defun-style name: loop.
((let name bindings body ...)
(let-loop name bindings () () (body ...)))))

(define-syntax let-loop

(syntax-rules ()

;; Standard binding: destructure and loop.
((let-loop name ((var0 val0) binding ...) (var ...     ) (val ...     ) body)
(let-loop name (            binding ...) (var ... var0) (val ... val0) body))

;; Rest binding, no name: use standard-let, listing the rest values.
;; Because of let's first clause, there is no "no bindings, no name" clause.
((let-loop #f (rest-var rest-val ...) (var ...) (val ...) body)
(standard-let ((var val) ... (rest-var (list rest-val ...))) . body))
;; Or call a lambda with a rest parameter on all values.
;; ((lambda (var ... . rest-var) . body) val ... rest-val ...))
;; Or use one of several other reasonable alternatives.

;; No bindings, name: call a letrec'ed lambda.
((let-loop name () (var ...) (val ...) body)
((letrec ((name (lambda (var ...) . body)))
name)
val ...))

;; Rest binding, name: call a letrec'ed lambda.
((let-loop name (rest-var rest-val ...) (var ...) (val ...) body)
((letrec ((name (lambda (var ... . rest-var) . body)))
name)
val ... rest-val ...))))