Racket 中的条件模式匹配
由于指南中的所有示例都带有列表,因此我发现很难了解如何在 Racket 中使用模式匹配来编写像 OCaml 那样的条件匹配,例如:
read ~var_a var_b s = match s.[0] with
| _ when var_b >= var_a + 4 ->
(* Do something *)
| "a" when is_negative var_b ->
(* Do something else *)
...
我如何在 Racket 中编写类似的内容?
谢谢。
Since all the examples in the guide are with lists, I find it difficult to see how to use pattern matching in Racket to write conditional matching like OCaml does, for example:
read ~var_a var_b s = match s.[0] with
| _ when var_b >= var_a + 4 ->
(* Do something *)
| "a" when is_negative var_b ->
(* Do something else *)
...
How would I write something similar in Racket?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
racket/match
库包含可以通过以下方式使用任意谓词的模式匹配:?
模式。与和
一起,您应该能够让 Racket 的匹配器正常工作。虽然我的 OCaml 能力有点弱,但我认为上面代码的以下翻译与其含义相符:?
匹配器内嵌了隐式and
,因此代码可以更简洁地表达为:在这两种情况下,其中的 lambda 不观察匹配的内容,因此我只是将它们命名为
_
来表示不关心。但是您可以想象更复杂的模式,其中谓词可以深入关心确切匹配的内容。Eli 建议在这里使用通用的
cond
,因为代码中没有任何重要的模式匹配。我同意。代码如下所示:The
racket/match
library includes pattern matching that can use arbitrary predicates through the?
pattern. Along withand
, you should be able to get Racket's matcher to behave. Although I'm a little weak in my OCaml, I think the following translation of the code above matches its meaning:The
?
matcher has an implicitand
embedded within it, so the code can be expressed slightly more succinctly as:In both, the lambdas in there aren't watching what got matched, so I just named them
_
to denote a don't-care. But you can imagine more sophisticated patterns where the predicates could care deeply about what exactly got matched.Eli suggests using a general
cond
here, since there isn't any significant pattern matching in the code. I agree. The code would look like this:模式匹配可以很容易地转化为一系列测试,没有一种语言不能做到这一点。
OCaml(可能还有 Haskell)模式匹配的优点在于,编译器在可能的情况下将代码转换为最佳测试序列(即程序永远不会测试两次相同的条件,至少当您避免
当
守卫时)。Pattern-matching can easily be translated into a sequence of tests, there is no language where you cannot do that.
What's great with OCaml (and probably Haskell) pattern-matching is that the compiler translates the code into the optimal sequence of tests when it is possible (i.e. the program will never test twice the same condition, at least when you avoid the
when
guards).