如何匹配,在Racket中匹配?

6 scheme racket

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

soe*_*ard 7

首先请注意,逗号,是特殊的读者缩写.这(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)
Run Code Online (Sandbox Code Playgroud)

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

> (define s '(hi,there))
> (match s [(list h (list 'unquote t)) (list h t)])
(list 'hi 'there)
Run Code Online (Sandbox Code Playgroud)