如何在 OCaml 中编写案例列表模式匹配?

问题描述

我想做如下 Go 代码

 switch c {
    case ' ','\t','\n','\f','\r':
        return true
 }

但是我在 OCaml 手册中找不到示例。

这在 OCaml 中可行吗?

解决方法

OCaml 没有 switch 语句。它具有“模式匹配”,这比 switch 语句更通用。

您可以阅读更多关于此enter image description here

你的 Go 代码可以翻译成这样(从我的头顶):

match c with 
| ' ' | '\t' | '\n' | '\012' (* '\f' *) | '\r' -> true
| _ -> false