Macros

Discussion of Scheme and Racket

Macros

Postby Indecipherable » Sat Oct 01, 2011 2:42 pm

Can somebody show me an example of a macro in Scheme? I coded this in Racket:

Code: Select all
(define-syntax setvar
    (syntax-rules ()
      ([setvar var value] [define var value])))


I'm trying to write this in Scheme. Can't be too different. Thanks.
Using Gambit.
Don't take the FUN out of DEFUN !
User avatar
Indecipherable
 
Posts: 47
Joined: Fri Jun 03, 2011 5:30 am
Location: Behind you.

Re: Macros

Postby saulgoode » Sun Oct 02, 2011 12:09 am

The same code works with Guile (substituting for the square brackets):
Code: Select all
(define-syntax setvar
  (syntax-rules ()
    ((setvar var value)
      (define var value) )))
saulgoode
 
Posts: 45
Joined: Tue Dec 14, 2010 1:39 am

Re: Macros

Postby Indecipherable » Fri Nov 11, 2011 1:45 pm

Doesn't work in Gambit-C. I should just stick with Racket. :D
Don't take the FUN out of DEFUN !
User avatar
Indecipherable
 
Posts: 47
Joined: Fri Jun 03, 2011 5:30 am
Location: Behind you.

Re: Macros

Postby saulgoode » Sun Nov 13, 2011 2:03 am

Indecipherable wrote:Doesn't work in Gambit-C.

It worked fine for me when I invoked using the command 'gsi -:s'.
saulgoode
 
Posts: 45
Joined: Tue Dec 14, 2010 1:39 am

A way for define-syntax/syntax-rules to match subexpr?

Postby sylwester » Fri Jun 29, 2012 10:44 am

I have a slightly advanced question about scheme macroes.

I'm wondering if syntax-rules (which is the only accepted mecro-type when setting DrRacket to R5RS) can do the trick if I want:

Code: Select all
(quote-unique sym1 sym2 sym1 sym3 "bla" sym4 "bla")
=> '(sym1 sym2 sym3 "bla" sym4)


So far I'ved tried this:
Code: Select all
(define-syntax quote-unique
  (syntax-rules (magic end)

    ;; end case
    ((quote-unique magic processed end)
     'processed)

    ;; finished iteration
    ((quote-unique magic (processed ...) sym1 end rest ... )
     (quote-unique magic (processed ... sym1) rest ... end))
   
    ;; match (doesnt work since racket doesn't like sym1 twice
    ;; but I'm looking for the same expression twice
    ((quote-unique magic processed sym1 sym1 . rest )
     (quote-unique magic processed sym1 . rest))

    ;; rotate
    ((quote-unique magic processed sym1 sym2 rest ... )
     (quote-unique magic processed sym1 rest ... sym2))

    ;; start iteration
    ((quote-unique rest ...)
     (quote-unique magic () rest ... end))))


I also saw Define syntax primer and I assume the implementation of is-eqv? would have pointed me in the right directions, but it seems it's not a macro that is defined there :(

I'm planning to add inheritance to my object system when I figured this out so any help is appreciated
I'm the author of two useless languages that uses BF as target machine.
Currently I'm planning a Scheme compiler :p
sylwester
 
Posts: 83
Joined: Mon Jul 11, 2011 2:53 pm

Re: Macros

Postby saulgoode » Fri Jul 06, 2012 8:44 am

Code: Select all
(define (remove-duplicates lis)
  (cond
    ((null? lis)
      '() )
    ((member (car lis) (cdr lis))
      (remove-duplicates (cdr lis)) )
    (else
      (cons (car lis) (remove-duplicates (cdr lis))) )))
         
(define-syntax quote-unique
  (syntax-rules ()
    ((_ args ...)
      (remove-duplicates (quote (args ...))) )))


Perhaps?
saulgoode
 
Posts: 45
Joined: Tue Dec 14, 2010 1:39 am

Re: Macros

Postby sylwester » Sat Jul 07, 2012 6:28 am

Close but no cigar. Using a function works fine, but then it happens after expansion and thus runtime.
The whole point of the macro is to only do it once (compilation time) and only with static data presented in the macro.

In Common Lisp I would have done this (using your function for kicks):
Code: Select all
(defmacro quote-unique ( &rest xs )
   (list 'quote (remove-duplicates xs)))



Now, this was easy because you have CL powered macro, but I'm trying do the same with the not so easy syntax-rules and R5RS to be able to reproduce it in both gambit and chicken.. I'm actually doing the development in Racked because of the very good (but sometimes buggy) macro-stepper.

This example is actually a bare minimum problem to a more complex project I'm doing to be more familiar with syntax-rules.
I'm the author of two useless languages that uses BF as target machine.
Currently I'm planning a Scheme compiler :p
sylwester
 
Posts: 83
Joined: Mon Jul 11, 2011 2:53 pm


Return to Scheme

Who is online

Users browsing this forum: No registered users and 0 guests