繁体   English   中英

如何匹配,在Racket中匹配?

[英]How to match , in match in Racket?

如果我有这样的东西(define s (hi,there))那么我怎么能写匹配(match s [(,h , ,t)] ...)但它不起作用,因为match需要,那怎么能这样呢?

首先注意逗号,是一个特殊的读者缩写。 (hi,there)是一个读(hi (unquote there)) as (hi (unquote there)) 这很难发现 - 因为默认打印机以特殊方式打印第一个元素是unquote列表。

Welcome to DrRacket, version 5.3.0.14--2012-07-24(f8f24ff2/d) [3m].
Language: racket.
> (list 'hi (list 'unquote 'there))
'(hi ,there)

因此,您需要的模式是'(列表h(列表'unquote t))'。

> (define s '(hi,there))
> (match s [(list h (list 'unquote t)) (list h t)])
(list 'hi 'there)

如果要在引用部分中使用逗号作为符号,请使用反斜杠:

> (define s '(hi \, there))
> (match s [(list h c t) (symbol->string c)])
","

并使用'|,| 对于独立的逗号符号。

> (match s [(list h '|,| t) (list h t)])
'(hi there)

在任何一种情况下,你真的应该使用空格来分隔事物,并使用列表。

(define s (hi,there))无效的Racket。

我想你可能会对你需要逗号的地方感到困惑。 在Racket中,您不使用逗号分隔列表中的元素。 相反,你只需使用空格。 告诉我这是不是错了,但我想象的是你想要匹配一个表达式(define s '(hi there)) 要做到这一点,你会使用

(match s
  [`(,h ,t) ...])

然后,在elipses所在的区域中,变量h的值为'hi ,变量t的值为'there

暂无
暂无

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

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