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

查看:102
本文介绍了表达式中的'或'的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?

推荐答案

没有办法为不同的模式分享相同的右手边。但是,您通常可以通过使用警卫而不是模式来解决此问题,例如

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          = ...

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

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