如何很好地表示这三个条件的替代方案? [英] How to nicely denote the alternative of three conditions?

查看:45
本文介绍了如何很好地表示这三个条件的替代方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个字符 c :: Char .现在,我想看看它是否等于 a isDigit :

Assume I have a character c :: Char . Now I want to see if it's equal to a or isDigit:

isAOrDigit = (||) <$> (=='a') <*> (isDigit)

到目前为止,一切都很好.但是现在我想看看它是否等于 a isDigit 或介于 d g 之间.不幸的是,由于 || 仅接受2个参数,所以我不能说(||)< $>.(=='a')< *>(isDigit)< *>(`elem [['d'..'g']).

So far so good. But now I want to see if it's equal to a, isDigit or is between d and g. Unfortunately, since || only accepts 2 arguments, I can't say (||) <$> (=='a') <*> (isDigit) <*> (`elem`['d'..'g']).

有什么好方法可以写这个吗?还是我不得不回到这个问题上?

Is there any nice way to write this or do i have to fall back to this:

isACOrDigitOrDG c = c == 'a' || isDigit c || c `elem` ['d'..'g']

?

推荐答案

您可以取消(||)运算符:

(<||>) = liftA2 (||)

> :t (== 'a') <||> isDigit <||> (`elem` ['d'..'g'])
(== 'a') <||> isDigit <||> (`elem` ['d'..'g']) :: Char -> Bool

> (== 'a') <||> isDigit <||> (`elem` ['d'..'g']) $ 'a'
True

这篇关于如何很好地表示这三个条件的替代方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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