我想编写一个函数,它接受两个Maybe Int参数,如果它们都是Just number,则返回它们中的最小值,如果其中一个是 ,则返回“另一个” Nothing。我对我的第一次尝试不满意:
maybeMin :: Maybe Int -> Maybe Int -> Maybe Int
maybeMin Nothing arr = arr
maybeMin ell Nothing = ell
maybeMin ell@(Just l) arr@(Just r) = if l < r then ell else arr
Run Code Online (Sandbox Code Playgroud)
作为优化,我不想在第三种情况下创建新值;即,我不想写
maybeMin ell@(Just l) arr@(Just r) = Just $ if l < r then l else r
Run Code Online (Sandbox Code Playgroud)
上面的代码看起来很笨拙,在我看来,我应该能够利用, orMaybe的实例这一事实。然而,我最好的尝试去高阶并没有做同样的事情:FunctorApplicativeMonad
maybeMin ell arr = ell >>= (\l -> arr >>= (\r -> if l < r then ell else arr))
Run Code Online (Sandbox Code Playgroud)
因为Nothing如果任一操作数是,它将返回Nothing。
有没有一种优雅的方式来做我想做的事?
您查看了Functor、Applicative和Monad,但您可能想查看一下Alternative。作为其使用的一个例子,Just 3 <|> Nothingwill yieldJust 3而 not Nothing。
对于您的特定用途,如果您想要单线,您可以尝试:
maybeMin l r = min l r <|> l <|> r
Run Code Online (Sandbox Code Playgroud)
只是为了打破下来,我们首先计算min l r,它使用Ord的实例Maybe给最低的l和r如果两者都Just值。如果这有效,那么计算就在那里停止,但如果有一个不是Just,那么我们检查是否l是一个Just值。如果是,那么这就是结果,如果不是,我们最终r作为结果返回。