[plt-scheme] macros that expand into top-level definitions with computed names

From: Lee Spector (lspector at hampshire.edu)
Date: Tue Aug 11 21:31:19 EDT 2009

I'm still transitioning from Common Lisp (thanks for all of the  
pointers so far) and I'm having trouble doing something that I could  
easily do with a CL macro.

I'd like to write a macro (or whatever) that takes two strings and  
defines a function named with the symbol formed from interning the  
concatenation of the strings. For example if the arguments have the  
values "my-" and "function" in a particular call then I'd like the  
effect of the call to be the same as something like (define my- 
function <more stuff here>).

This is straightforward using defmacro in CL, but looking through the  
section of the PLT reference on macros I don't see where to start.  In  
CL if I didn't know about defmacro, and if I didn't mind having my  
definitions evaluated in a null lexical environment, then I could hack  
it with eval. So with my new (but still minimal) understanding of eval  
and namespaces in PLT I tried this:

-------
#lang scheme

(define-namespace-anchor nsa)
(define ns (namespace-anchor->namespace nsa))

(eval `(define ,(string->symbol (string-append "my-" "function"))
               (lambda () 'runs))
       ns)

(my-function)
-------

This doesn't work -- expand: unbound identifier in module in: my- 
function -- although if I get rid of the call to my-function in the  
definitions pane then it does compile and allow me to call my-function  
(which then does the right thing) in the interactions pane. But that's  
not good enough for my purposes.

Of course this hack with eval is probably the wrong approach anyway --  
it would be a bad way to do it in CL and I'm guessing that a  
completely different approach would be appropriate in PLT. And I think  
I'm just not seeing the right part of the PLT docs because of the  
differences in terminology.

Can someone point me in the right direction on this?

Thanks,

  -Lee

--
Lee Spector, Professor of Computer Science
School of Cognitive Science, Hampshire College
893 West Street, Amherst, MA 01002-3359
lspector at hampshire.edu, http://hampshire.edu/lspector/
Phone: 413-559-5352, Fax: 413-559-5438

Check out Genetic Programming and Evolvable Machines:
http://www.springer.com/10710 - http://gpemjournal.blogspot.com/



Posted on the users mailing list.