Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

define* macro: a solution for sequential definitions within syntax-case
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
[url]http://cvs.sourceforge.net/viewcvs.py/gauche/Gauche/src/gauche-init.scm?rev=1.72[/
url]
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


Report this thread to moderator Post Follow-up to this message
Old Post
Andrew Wilcox
12-08-04 01:57 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Scheme archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:04 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.