I want to match a struct test
, and build a struct from that, but how can I do that?
I can't even use eval
to do this?
Please reference to the code,
#lang racket
(struct test (num) #:prefab)
(define s `((AK= #(struct:test 100))
(AV)
))
(define ns (make-base-namespace))
(define (set-state-space-vals!)
(namespace-set-variable-value! 'test test #f ns)
)
(match s
[`((AK= #(struct:test ,val) (AV))) ;; can't match
(displayln val)
]
[`((AK= ,val) (AV)) ;; can match, but how to build the struct?
(struct? (eval val ns))
(match-define (struct test (num)) (eval val ns)) ;this will fail
(displayln num)
] )
I think you were looking for something like this:
(struct test (num) #:prefab)
(define s '((AK= #s(test 100)) (AV)))
(match s
[`((AK= ,(struct test (val))) (AV))
(displayln val)])
Also, in general using eval
in a situation like this is highly inadvisable. When in doubt, do not use eval
. See this blog post for a longer explanation. You might also want to see the Guide entries on prefabs and pattern matching .
此外,虽然它不在匹配的文档中,`((AK = #s(test,val))(AV))是一个有效的模式。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.