[plt-scheme] Macro-Fu (was Re: File Locks)

From: Brian Campbell (Brian.P.Campbell.04 at Alum.Dartmouth.ORG)
Date: Thu Aug 11 10:57:40 EDT 2005

First rule of macro-fu: don't use macros.
Second rule of macro-fu: you sometimes need to use macros.
Third rule of macro-fu: when you need macros, start with a function  
that implements what you need, and then make the macro a thin layer  
around that.
Fourth rule of macro-fu: always use syntax-rules.
Fifth rule of macro-fu: syntax-rules is not always sufficient.
Sixth rule of macro-fu: syntax-rules is more powerful than you think.  
Even when you think it is not sufficient, it probably is.

Judicious application of those rules can help your macro-fu  
considerably. For example, here is a macro that I originally thought  
would need syntax-case, but actually could be implemented with syntax  
rules.

(define-syntax macro-getarg
   (syntax-rules ()
     [(_ (name default) args)
      (letrec-syntax
          ([inner-getarg
            (syntax-rules (name)
              [(_ name) (error "No value provided for keyword:" 'name)]
              [(_ name value . rest) value]
              [(_) default]
              [(_ first . rest) (inner-getarg . rest)])])
        (inner-getarg . args))]
     [(_ name args) (macro-getarg (name #f) args)]))

-- Brian, student of macro-fu

On Aug 11, 2005, at 7:01 AM, Matt Jadud wrote:

> I am but a grasshopper in my macro-fu?
>
> M
>
> Eli Barzilay wrote:
>
>> On Aug 11, Matt Jadud wrote:
>>
>>> [...]
>>> (define-syntax forever
>>>   (lambda (stx)
>>>     (syntax-case stx ()
>>>       [(forever bodies ...)
>>>        #`(let loop ()
>>>            #,@(syntax->list (syntax (bodies ...)))
>>>            (loop))])))
>>>
>> Um, any need for this huge block of verbosity?  Seems like this is a
>> simple:
>>   (define-syntax forever
>>     (syntax-rules ()
>>       [(forever bodies ...)
>>        (let loop () bodies ... (loop))]))
>> In any case, your solution will obviously work as long as there is a
>> single process that tries to write to the file.  DrScheme uses a
>> lockfile for preferences in case you're running several instances.
>>
>
>



Posted on the users mailing list.