【发布时间】:2016-08-09 03:43:19
【问题描述】:
我可以将多个变量一起定义如下:
(match-define (list a b c) (list 1 2 3))
a
b
c
输出:
1
2
3
>
但是我以后如何在程序中重新定义这些变量呢?
本质上,我正在寻找“匹配重新定义”函数或宏。
以下不起作用:
(set! (list a b c) (list 10 20 30))
set!: not an identifier in: (list a b c)
我也试过地图功能:
> (map set! (list a b c) (list 5 6 7))
. set!: bad syntax in: set!
>
我需要为它写一个函数还是有一些简单的内置方法?
编辑:我尝试了以下 match-redefine 宏,写在 https://docs.racket-lang.org/guide/pattern-macros.html 上的交换宏行上:
(match-define (list a b c) (list 1 2 3))
(println "------- sl --------")
(define sl (list a b c))
(println sl)
(println "------- vl --------")
(define vl (list 5 6 7))
(println vl)
(define-syntax-rule (match-redefine slist vlist)
(for ((i slist) (j vlist))
(set! i j)
)
)
(println "----- expecting sl to become 5 6 7 ----------")
(match-redefine sl vl)
(println sl)
但它不起作用。输出为:
"------- sl --------"
'(1 2 3)
"------- vl --------"
'(5 6 7)
"----- expecting sl to become 5 6 7 ----------"
'(1 2 3)
编辑:我发现 match-let 允许使用列表重新分配:
(define a 1)
(define b 2)
(define c 3)
(match-let (( (list a b c) (list 100 200 300) ))
(println a)
(println b)
(println c)
)
输出:
100
200
300
但它不是“定义”或“设置!”用于所有剩余的源文件。
【问题讨论】: