繁体   English   中英

如何匹配结构

[英]How to match a struct

我想匹配一个struct test ,并从中构建一个结构,但我该怎么做呢?
我甚至不能用eval来做这件事?

请参考代码,

#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)
    ]  )

我想你正在寻找这样的东西:

(struct test (num) #:prefab)

(define s '((AK= #s(test 100)) (AV)))

(match s
  [`((AK= ,(struct test (val))) (AV))
   (displayln val)])

此外,通常在这种情况下使用eval是非常不可取的。 如有疑问,请勿使用eval 有关详细说明,请参阅此博客文章 您可能还希望在预制件模式匹配上看到“指南”条目。

此外,虽然它不在匹配的文档中,`((AK = #s(test,val))(AV))是一个有效的模式。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM