[英]Change variable outside of lambda in Scheme/Racket
The examples from the doc of Racket come with lambda
always:https://docs.racket-lang.org/syntax/Defining_Simple_Macros.html Racket 文档中的示例始终带有
lambda
:https ://docs.racket-lang.org/syntax/Defining_Simple_Macros.html
And my define-syntax-parser
is like this:我的
define-syntax-parser
是这样的:
(require syntax/parse/define)
(define-syntax-parser sp
[_ #'(lambda (x) (set! x (add1 x)))]
)
(define a 0)
((sp) a)
(display a)
Possible to do something like this (remove the lambda
)?可以做这样的事情(删除
lambda
)?
(require syntax/parse/define)
(define-syntax-parser sp
[(f x) #'(set! x (add1 x))]
)
(define a 0)
(f a)
(display a)
The result is expected to be 1
but it's still 0
.结果预计为
1
但它仍然是0
。 And Scheme/Racket don't pass by reference(?!), so how to change those variables outside of lambda?并且 Scheme/Racket 不通过引用传递(?!),那么如何在 lambda 之外更改这些变量?
There's a related answer here: https://stackoverflow.com/a/8998055/5581893 but it's about the deprecated define-macro
( https://docs.racket-lang.org/compatibility/defmacro.html )这里有一个相关的答案: https : //stackoverflow.com/a/8998055/5581893但它是关于已弃用的
define-macro
( https://docs.racket-lang.org/compatibility/defmacro.html )
Macro can expand to anything, not just lambda
.宏可以扩展到任何东西,而不仅仅是
lambda
。
#lang racket
(require syntax/parse/define)
(define-simple-macro (sp x:id) (set! x (add1 x)))
(define a 0)
(sp a)
(display a)
Or if you prefer to use define-syntax-parser
:或者,如果您更喜欢使用
define-syntax-parser
:
#lang racket
(require syntax/parse/define)
(define-syntax-parser sp
[(_ x:id) #'(set! x (add1 x))])
(define a 0)
(sp a)
(display a)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.