why is:
(load "t1.scm") (define x 20)
valid, where as:
(define-macro (foo) (load "t1.scm") (define x 20))
result in a ill-placed define error?
I was under the impression that scheme define-macro was just scheme code; it appears that the environment the macro is run in is different from the runtime ones?
Thanks!
Afficher les réponses par date
2009/8/14 lowly coder lowlycoder@huoyanjinjing.com:
why is:
(load "t1.scm") (define x 20)
Load works at run time.
(define foo "bar.scm") (lofd foo)
valid, where as: (define-macro (foo) (load "t1.scm") (define x 20)) result in a ill-placed define error?
You have to think that macros is a different language than scheme that just conveniently happen to have a similar syntax.
I was under the impression that scheme define-macro was just scheme code; it appears that the environment the macro is run in is different from the runtime ones?
I think told you yesterday, with sample code, that macros accessed basically two environments, and handled them differently than the runtime code. And also, you *want* a different environment than the runtime one…
I *strongly* suggest you buy and read "Lisp In Small Pieces" by Christian Queinnec. All your macros questions, current and future, are answered there.
P!
You have to think that macros is a different language than scheme that just conveniently happen to have a similar syntax.
Isn't one of the core tenents of define-macro that it's just lisp code generating lisp code?
I think told you yesterday, with sample code, that macros accessed basically two environments, and handled them differently than the runtime code.
You most definitely did. I don't think I quite understand it yet.
And also, you *want* a different environment than the runtime one…
Why? If I want to avoid naming clash, I can just put them in different namespaces.
I *strongly* suggest you buy and read "Lisp In Small Pieces" by Christian Queinnec. All your macros questions, current and future, are answered there.
P!
-- Français, English, 日本語, 한국어
Isn't one of the core tenents of define-macro that it's just lisp code generating lisp code?
For what kind of lisp code? Gambit-specific code? Larceny-specific code? Just R4RS-specific code? Is it portable?
What define-macro allows is implementation specific, but the bare minimum might not even be full R4RS compliant…
You most definitely did. I don't think I quite understand it yet.
Read it again ^^
And also, you *want* a different environment than the runtime one…
Why? If I want to avoid naming clash, I can just put them in different namespaces.
Err, namespaces are *not* even in R5RS. How would you then expect macro to work in previous versions of Scheme ?
Moreover, with a common namespace, what would happen to such code?
(define-macro (foo) (define + (lambda (x y) 42)) (pp "foo"))
(foo) (pp (+ 1 2)) ;; 42 !!!!!!!!!!!!!!
You would not like that would you?
Consider that, "A macro is an external program with takes a scheme source and returns a scheme source", then "A macro is an external program", then you do not want the variables defined in an external program to mask thoses of the processed data, do you? It's not even a matter of namespace. It's two different worlds. The names defined in your OS do not influence the results of the programs you write. Yet, your OS is handling your source code as a (dummy) macro would do.
Please read Queinnec's book. Or write your own macro expanser.
P!
On Sat, Aug 15, 2009 at 12:43 AM, Adrien Piérard pierarda@iro.umontreal.cawrote:
Isn't one of the core tenents of define-macro that it's just lisp code generating lisp code?
For what kind of lisp code? Gambit-specific code? Larceny-specific code? Just R4RS-specific code? Is it portable?
What define-macro allows is implementation specific, but the bare minimum might not even be full R4RS compliant…
You most definitely did. I don't think I quite understand it yet.
Read it again ^^
And also, you *want* a different environment than the runtime one…
Why? If I want to avoid naming clash, I can just put them in different namespaces.
Err, namespaces are *not* even in R5RS. How would you then expect macro to work in previous versions of Scheme ?
Moreover, with a common namespace, what would happen to such code?
(define-macro (foo) (define + (lambda (x y) 42)) (pp "foo"))
(foo) (pp (+ 1 2)) ;; 42 !!!!!!!!!!!!!!
You would not like that would you?
Consider that, "A macro is an external program with takes a scheme source and returns a scheme source", then "A macro is an external program", then you do not want the variables defined in an external program to mask thoses of the processed data, do you? It's not even a matter of namespace. It's two different worlds. The names defined in your OS do not influence the results of the programs you write. Yet, your OS is handling your source code as a (dummy) macro would do.
Please read Queinnec's book. Or write your own macro expanser.
A bit unrelated, but thanks for recommending that book. I just picked up a copy yesterday. It looks really good.
P!
-- Français, English, 日本語, 한국어 _______________________________________________ Gambit-list mailing list Gambit-list@iro.umontreal.ca https://webmail.iro.umontreal.ca/mailman/listinfo/gambit-list
why is:
(load "t1.scm") (define x 20)
valid, where as:
(define-macro (foo) (load "t1.scm") (define x 20))
result in a ill-placed define error?
This is not a problem specific to macros, the same error would happen if you did
(define (foo) (load "t1.scm") (define x 20))
That would fail, as would
(lambda () (load "t1.scm") (define x 20))
, and
(lambda () (+ 1 1) (define x 20))
. load is just a "normal" function, whereas define is a special form, and it has to be in the beginning of a scope, before any function calls are made.
I think of it as if Gambit internally converts defines within scopes to letrecs, like
(lambda () (define x 1) (define y 2) (+ x y)) => (lambda () (letrec ((x 1) (x 2)) (+ x y)))
Given that definition, your code above doesn't really make any sense. You could write
(lambda () (load "t1.scm") (let () (define x 20)))
or
(lambda () (define x #f) (load "t1.scm") (set! x 20)))
depending on what you want.
/Per