| 1 |
;; Copyright © 2020 Göran Weinholt |
| 2 |
;; SPDX-License-Identifier: MIT |
| 3 |
#!r6rs |
| 4 |
|
| 5 |
;; SRFI 5: let form with define-style syntax and rest arguments |
| 6 |
|
| 7 |
(library (srfi :5 let) |
| 8 |
(export let) |
| 9 |
(import (rename (rnrs) (let rnrs:let))) |
| 10 |
|
| 11 |
(define-syntax let |
| 12 |
(lambda (x) |
| 13 |
(define (let-args x) |
| 14 |
(syntax-case x () |
| 15 |
;; Push lhs and rhs to the end of lhs* and rhs*, respectively |
| 16 |
[(_ ((lhs rhs) . x*) (lhs* ...) (rhs* ...)) |
| 17 |
(identifier? #'lhs) |
| 18 |
(let-args #'(_ x* (lhs* ... lhs) (rhs* ... rhs)))] |
| 19 |
;; Finally handle the rest arguments, if any |
| 20 |
[(_ (rest arg* ...) (lhs* ...) (rhs* ...)) |
| 21 |
(identifier? #'rest) |
| 22 |
#'((lhs* ... . rest) (rhs* ... arg* ...))] |
| 23 |
[(_ () lhs* rhs*) #'(lhs* rhs*)])) |
| 24 |
(syntax-case x () |
| 25 |
;; Named let |
| 26 |
[(_ name bindings body ...) |
| 27 |
(identifier? #'name) |
| 28 |
(with-syntax ([(lhs* rhs*) (let-args #'(let-args bindings () ()))]) |
| 29 |
#'((letrec ((name (lambda lhs* body ...))) name) |
| 30 |
. rhs*))] |
| 31 |
;; Define-style named let |
| 32 |
[(_ (name . bindings) body ...) |
| 33 |
(identifier? #'name) |
| 34 |
#'(let name bindings body ...)] |
| 35 |
;; Let, possibly with rest arguments |
| 36 |
[(_ bindings body ...) |
| 37 |
(with-syntax ([(lhs* rhs*) (let-args #'(let-args bindings () ()))]) |
| 38 |
#'((lambda lhs* body ...) . rhs*))])))) |