case 表达式中“或"的 Haskell 语法 [英] Haskell syntax for 'or' in case expressions

查看:18
本文介绍了case 表达式中“或"的 Haskell 语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 F# 中,我可以在模式匹配时使用 | 对案例进行分组.例如,

In F#, I can use | to group cases when pattern matching. For example,

let rec factorial n = 
  match n with
  | 0 | 1 -> 1                 // like in this line
  | _ -> n * factorial (n - 1)

相同的 Haskell 语法是什么?

What's the Haskell syntax for the same?

推荐答案

对于不同的模式,无法共享同一个右手边.但是,您通常可以通过使用守卫而不是模式来解决这个问题,例如使用 elem.

There is no way of sharing the same right hand side for different patterns. However, you can usually get around this by using guards instead of patterns, for example with elem.

foo x | x `elem` [A, C, G] = ...
      | x `elem` [B, D, E] = ...
      | otherwise          = ...

这篇关于case 表达式中“或"的 Haskell 语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆