| Andrew Wilcox 2004-12-07, 8:57 pm |
| In September I noted that I liked being able to define functions in
terms of other higher order functions, but the expansion of internal
definitions into an equivalent letrec form defeats the way I naturally
like to express them. For example, using a Chez Scheme style module:
(module calculate (calc)
(define (add-n n)
(lambda (x)
(+ n x)))
(define add-10 (add-n 10)) ; error!
(define add-20 (add-n 20)) ; error!
(define (calc x)
(if (even? x) (add-10 x) (add-20 x))))
With help from Abdulaziz Ghuloum, Jens Axel Soegaard, Michael Sperber,
and Marcin 'Qrczak' Kowalczyk, I've implemented a macro that allows the
natural expression of defining functions in terms of other functions. I
dedicate this code to the public domain, in case it is useful to anyone
else.
(define*
(zero 0)
(ten (+ zero 10))
(twenty (+ ten 10)))
This binds in the enclosing environment zero, ten, and twenty to 0, 10,
and 20.
The usual abbreviation for lambda's can be used, i.e. where
(define (f x) (+ x 1))
is equivalent to
(define f (lambda (x) (+ x 1)))
So my desired example can be written,
(module calculate (calc)
(define*
((add-n n) (lambda (x) (+ n x)))
(add-10 (add-n 10))
(add-20 (add-n 20))
((calc x)
(if (even? x) (add-10 x) (add-20 x)))))
Portability
The macro requires the availability of standard R5RS syntax-rules
macros, the define-values form, and uses (void) to produce undefined values.
void could be defined simply as
(define (void) (if #f 'ignored))
or you could replace (void) with some other expression such as #f.
Whether a define* form can be followed by further internal definitions,
such as in:
(let ()
(define* (a 1))
(define b 2)
(list a b))
depends on whether you have available to you a define-values form which
itself can be followed by further internal definitions.
* If your version of Scheme implements define-values in such a way that
it can be followed by further definitions, then a define* can also be
followed by further definitions.
* If your version of Scheme implements internal definitions as a letrec*
as has been proposed for R6RS, you don't need this define* macro.
* If your Scheme uses the portable syntax-case expander from Chez Scheme
(which implements anonymous modules), you can use Abdulaziz Ghuloum's
implementation of define-values for syntax-case. See
http://groups-beta.google.com/group...baf520a516a3af8
* Finally, define-values can be implemented in plain R5RS, however you
won't then be able to follow a define* form with further internal
definitions. See
http://cvs.sourceforge.net/viewcvs....it.scm?rev=1.72
for one implementation.
Restrictions
Variables may depend on the values of earlier variables, and mutually
recursive functions can be written as long as all the expressions
involved are lambda expressions.
However it should be an error for a variable to depend on the value of a
later variable. This implementation defeats such error reporting, even
if your Scheme implementation would otherwise be able to report such an
error. (Thanks to Marcin Kowalczyk).
Implementation
I dedicate this code to the public domain.
The following macro first converts function expressions such as
((f x) ...) to equivalent (f (lambda (x) ...), and then expands
a form such as
(define* ((a 1) (b 2)))
into
(define-values (a b)
(let ((a (void)) (b (void)))
(set! a 1)
(set! b 2)
(values a b)))
(define-syntax define*
(syntax-rules ()
((define* "reduce" () (simple-variable ...) (expression ...))
(define-values (simple-variable ...)
(let ((simple-variable (void)) ...)
(set! simple-variable expression) ...
(values simple-variable ...))))
((define* "reduce" (((variable . formals) body ...) more ...)
(simple-variables ...)
(expressions ...))
(define* "reduce"
(more ...)
(simple-variables ... variable)
(expressions ... (lambda formals body ...))))
((define* "reduce" ((simple-variable expression) more ...)
(simple-variables ...)
(expressions ...))
(define* "reduce"
(more ...)
(simple-variables ... simple-variable)
(expressions ... expression)))
((define* defines ...)
(define* "reduce" (defines ...) () ()))))
Thank you all for your help.
Andrew Wilcox
email: scheme[at]andrewwilcox.name
|