如果我有像这样的(define s (hi,there)),那么我怎么能像(match s [(,h , ,t)] ...)一样在match中写,但它不工作,因为match需要,,我该怎么做呢?
发布于 2012-07-27 16:08:20
首先请注意,逗号,是一个特殊的阅读器缩写。(hi,there)是作为(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)因此,您需要的模式是'(list h (list 'unquote t))‘。
> (define s '(hi,there))
> (match s [(list h (list 'unquote t)) (list h t)])
(list 'hi 'there)发布于 2012-07-27 21:16:57
如果要在带引号的部分内使用逗号作为符号,请使用反斜杠:
> (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))不是有效球拍。
发布于 2012-07-28 05:55:18
我想您可能会对哪里需要逗号感到困惑。在球拍中,不使用逗号分隔列表中的元素。相反,您只需使用空格。如果这是错误的,请告诉我,但我想象的是,您正在尝试匹配一个类似(define s '(hi there))的表达式。要做到这一点,您可以使用
(match s
[`(,h ,t) ...])然后,在省略号所在的区域中,变量h的值为'hi,变量t的值为'there
https://stackoverflow.com/questions/11680935
复制相似问题