unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Quasi-macros ?
@ 2003-02-09 19:23 Joris van der Hoeven
  2003-02-10 16:33 ` david
  0 siblings, 1 reply; 2+ messages in thread
From: Joris van der Hoeven @ 2003-02-09 19:23 UTC (permalink / raw)



Hi,

Consider the following piece of code:

  (define (define-table-decls name l)
    (if (null? l) l
        (cons `(hash-set! ,name ',(caar l) ',(cadar l))
  	      (define-table-decls name (cdr l)))))

  (define-macro (define-table name . l)
    `(begin 
       (define ,name (make-hash-table ,(+ (* (length l) 2) 1)))
       ,@(define-table-decls name l)))

  (define-table test
    (hello hoi)
    (joke grapje)
    (gnu blauwbilgorgel))

This allows you to define many tables in a nice way.
Sometimes however, it would be nice to write things like

  (define-table the-question
    (two ,(+ 1 1))
    (four ,(* 2 2)))

Someone has an idea of how to do this *without* explicitly
using 'eval', which might result in loosing the context?

Thanks, Joris


-----------------------------------------------------------
Joris van der Hoeven <vdhoeven@texmacs.org>
http://www.texmacs.org: GNU TeXmacs scientific text editor
http://www.math.u-psud.fr/~vdhoeven: personal homepage
-----------------------------------------------------------



_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Quasi-macros ?
  2003-02-09 19:23 Quasi-macros ? Joris van der Hoeven
@ 2003-02-10 16:33 ` david
  0 siblings, 0 replies; 2+ messages in thread
From: david @ 2003-02-10 16:33 UTC (permalink / raw)
  Cc: guile-user

On Sun, Feb 09, 2003 at 08:23:23PM +0100, Joris van der Hoeven wrote:
> 
>   (define (define-table-decls name l)
>     (if (null? l) l
>         (cons `(hash-set! ,name ',(caar l) ',(cadar l))
>   	      (define-table-decls name (cdr l)))))
> 
>   (define-macro (define-table name . l)
>     `(begin 
>        (define ,name (make-hash-table ,(+ (* (length l) 2) 1)))
>        ,@(define-table-decls name l)))
> 
>   (define-table test
>     (hello hoi)
>     (joke grapje)
>     (gnu blauwbilgorgel))
> 
> This allows you to define many tables in a nice way.
> Sometimes however, it would be nice to write things like
> 
>   (define-table the-question
>     (two ,(+ 1 1))
>     (four ,(* 2 2)))
> 
> Someone has an idea of how to do this *without* explicitly
> using 'eval', which might result in loosing the context?

It looks like quasiquote has special behaviour when it comes to
quoting himself... However, with a bit of hacking you can do what you
want.

  (define (define-table-decls h l)
    (define (sub ll) (hash-set! h (car ll) (cadr ll)))
    (for-each sub l))

  (define-macro (define-table name . l)
    `(begin
       (define ,name (make-hash-table ,(+ (* (length l) 2) 1)))
       (define-table-decls ,name ,(list 'quasiquote l))))

  (define-table the-question
    (two ,(+ 1 1))
    (four ,(* 2 2)))

-- 
David Allouche         | GNU TeXmacs -- Writing is a pleasure
Free software engineer |    http://www.texmacs.org
   http://ddaa.net     |    http://alqua.com/tmresources
   david@allouche.net  |    allouche@texmacs.org
TeXmacs is NOT a LaTeX front-end and is unrelated to emacs.


_______________________________________________
Guile-user mailing list
Guile-user@gnu.org
http://mail.gnu.org/mailman/listinfo/guile-user


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2003-02-10 16:33 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-02-09 19:23 Quasi-macros ? Joris van der Hoeven
2003-02-10 16:33 ` david

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).