[plt-scheme] Newbie question: using a macro to create a top-level variable?

From: Gregg Williams (spamme at innerpaths.net)
Date: Fri Aug 21 23:57:35 EDT 2009

Hi! I am working on creating a command-line environment for
interactively working with electronic notecards called infocards (see
my project http://InfoML.org if this sounds interesting to you). A
collection of infocards is called a "rack," and with various
manipulations I am creating racks, adding infocards to and deleting
them from racks, etc..

Here's my problem. To make racks modifiable, they must be top-level
variables created with "(define... )" (Please correct me anywhere that
my assumptions are wrong.) For this reason, I've been creating racks
as follows:

(define ideas (infoml:make-rack-manual 'ideas))

I need the variable named "ideas" so I can manipulate the rack from
the command line. I need the symbol 'ideas (which goes into the rack
data structure) so that I can, for example, print a list of all
existing racks.

The thing is, I would like to be able to write the simpler
"(infoml:make-rack-manual 'ideas))" and let it create a top-level
variable with the same name as the symbol given as an argument. I
can't do that within a simple function definition, but I thought I
could do it within a macro. Here's what I tried at first (which of
course didn't work):

(define-syntax-rule (infoml:make-rack rack-symbol)
  (define rack-symbol (infoml:make-rack-manual rack-symbol)))

When I did a macro expand in DrScheme, I could see why it didn't work--
the expansion was:

(define (quote rack-symbol) (infoml:make-rack-manual rack-symbol))

Obviously, it was important to get precisely the right thing after the
keyword "define". I tried unquoting the symbol; I tried converting the
symbol into a string--neither worked.

Despite some time spent reading the PLT Scheme documentation, I
couldn't get any insight as to what needs to follow "define". Can I do
this with a macro? Is my overall reasoning flawed? Am I approaching
this problem in the wrong way? Any and all feedback would be greatly
appreciated. Thanks.


Posted on the users mailing list.