首页 > 解决方案 > 我怎样才能匹配一个基准?

问题描述

我想写一个只匹配单个数据的模式,比如'aor'hello但不匹配其他任何东西。我不认为以下内容适用于所有内容(如(list 1 2 3)),对吗?

(define (f x)
  (match x (e (printf "hi~n"))))

标签: racket

解决方案


基准可用作模式。这是一个例子:

#lang racket

(define (f x)
  (match x
    ["hello" "x is hello"]
    [_       "x is not hello"]))

(f "hello")
(f 42)

结果是:

"x is hello"
"x is not hello"

此外,如果您想使用固定符号作为模式使用'hello而不仅仅是hello. 第一个'hello匹配单个符号,但hello它是一个模式变量并且匹配所有内容。


推荐阅读