[plt-scheme] How to make a setter out of an identifier
This may be what you are looking for:
#lang scheme
(define kk 0)
(define-syntax (make-setter stx)
(syntax-case stx ()
((_ id)
(with-syntax
((setter
(datum->syntax stx ; specify context
(string->symbol ; we have no symbol-append, therefore use string-append.
(string-append "set-" (symbol->string (syntax->datum #'id)) "!")))))
#'(define-syntax setter
(syntax-rules ()
((_ v) (set! id v))))))))
(make-setter kk)
(set-kk! 3)
kk ; --> 3
You have to use datum->syntax in order to introduce identifiers within their appropriate context.
Good luck, Jos
----- Original Message -----
From: "Cheng-Chang Wu" <chengchangwu at yahoo.com>
To: <plt-scheme at list.cs.brown.edu>
Sent: Wednesday, November 04, 2009 2:19 AM
Subject: [plt-scheme] How to make a setter out of an identifier
Hi,
I am new to the brave scheme world, and trying hard to understand the secrets of macro.
I want to do the following: take an identifier and make a setter out of the identifier.
For example,
(define kk 0)
(make-setter kk)
should give me a set-kk! which can be used to set the value of kk.
The best macro I can image is
(define-syntax make-setter
(lambda (x)
(syntax-case x ()
((make-setter id)
(with-syntax
((def
(datum->syntax
(syntax k)
`(define (,(string->symbol (string-append "set-" (symbol->string 'id) "!")) a-value) (set! id a-value)))))
(syntax def))))))
___________________________________________________
您的生活即時通 - 溝通、娛樂、生活、工作一次搞定!
http://messenger.yahoo.com.tw/
_________________________________________________
For list-related administrative tasks:
http://list.cs.brown.edu/mailman/listinfo/plt-scheme