是否有一种无条件的方式将条件检查转换为输入的Maybe类型? [英] Is there a point-free way to convert a conditional check into a Maybe type of the input?
问题描述
我只是在通过haskell中的一些简单练习,想知道是否有一种将if-then-else语句转换为 Maybe
类型的无点方法:如果条件为false,则返回 Nothing
;如果条件为真,则返回 Just
输入。
总之,给出一些:
maybeIf ::(a - > Bool ) - > a - >也许一个
maybeIf cond a =如果cond a然后一个其他没有
关于 a
的免费实现?我也一直在看更具体的版本, a - >也许是一个
,并且感觉在 Control.Arrow
中可能有答案。但是,由于 Maybe
是一种数据类型,而if-else语句控制数据流,所以我不确定是否有干净的方式来执行它。
您可以从 函数 I am just working through some simple exercises in haskell and was wondering if there was a point-free way of converting an if-then-else statement into a In short, given some: Is there an implementation that is point-free with respect to You can import The function 这篇关于是否有一种无条件的方式将条件检查转换为输入的Maybe类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! Data.Foldable $ c中导入
find
$ c $>然后它很简单:
$ p $ import Data.Foldable(find)
maybeIf cond =找到cond。只需
find
并不复杂,你可以非常容易地定义它,你可以用 Maybe
的方式来定义它,但它与你自己实现 maybeIf
,所以你可能得不到多少,这取决于你为什么要这样做。
Maybe
type: Nothing
being returned if the condition is false, and Just
the input if the condition is true.maybeIf :: (a -> Bool) -> a -> Maybe a
maybeIf cond a = if cond a then Just a else Nothing
a
? I've also been looking at a more concrete version, a -> Maybe a
, and feel like there may be an answer somewhere in Control.Arrow
. However, since Maybe
is a data type and if-else statements control data flow, I'm unsure if there is a clean way of doing it.find
from Data.Foldable
and then it's quite simply:import Data.Foldable(find)
maybeIf cond = find cond . Just
find
is not complicated so you could quite easily define it yourself less generically, in terms of Maybe
, but it isn't actually so different from your own implementation of maybeIf
so you might not gain much, depending on why you wanted to do it.