racket - 我怎样才能匹配一个基准?
问题描述
我想写一个只匹配单个数据的模式,比如'a
or'hello
但不匹配其他任何东西。我不认为以下内容适用于所有内容(如(list 1 2 3)
),对吗?
(define (f x)
(match x (e (printf "hi~n"))))
解决方案
基准可用作模式。这是一个例子:
#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
它是一个模式变量并且匹配所有内容。
推荐阅读
- javascript - ajax 发布请求 - 跨域读取阻塞 (CORB) 阻止跨域响应 CORS
- wordpress - 我网站的输入字段在 iphone、ipad 和 mac 上变为白色
- python - 在 Python 中不显示输入
- python - 从 Flightradar24 抓取数据
- visual-studio-code - Is there a way to save all files, commit and upload in one command in Visual Studio Code
- google-compute-engine - GCE 实例停止工作 1 小时
- c++ - 'const' 关键字是如何工作的?
- java - Javax 持久性在生产级别将 IDENTITY 更改为 SEQUENCE
- python-2.x - 理解 Python 2.x 中的深拷贝和浅拷贝
- pip - Airflow 1.10 安装失败