Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
228 views
in Technique[技术] by (71.8m points)

haskell - How to use higher-order constructs in a particular case

I want to write a function that takes two Maybe Int parameters and returns the minimum of them if they are both Just number, and 'the other' if either of them is Nothing. I'm not satisfied with my first attempt:

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

As an optimisation I don't want to create a new value in the third case; i.e., I don't want to write

maybeMin ell@(Just l) arr@(Just r) = Just $ if l < r then l else r

The above code seems clunky and it seems to me that I ought to be able to exploit the fact that Maybe is an instance of Functor, Applicative or Monad. However, my best attempt to go higher-order doesn't do the same thing:

maybeMin ell arr = ell >>= (l -> arr >>= (
 -> if l < r then ell else arr))

because it will return Nothing if either operand be Nothing.

Is there an elegant way to do what I want?

question from:https://stackoverflow.com/questions/65649226/how-to-use-higher-order-constructs-in-a-particular-case

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You looked at Functor, Applicative, and Monad, but you may want to check out Alternative. As an example of its use, Just 3 <|> Nothing will yield Just 3 and not Nothing.

For your particular use, if you want a one-liner, you could try:

maybeMin l r = min l r <|> l <|> r

Just to break that down, we first calculate min l r, which uses the Ord instance of Maybe to give the minimum of l and r if both are Just values. If this works, then the computation stops there, but if either one isn't Just, then we check to see if l is a Just value. If it is, then that is the result, and if not, we end up returning r as the result.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...