Data.These 언급, 아마도 최선의 선택,하지만 난 내 자신의 롤한다면, 나는 그것을 할 거라고하고있다 : 당신이 "제로를 원한다면,
import Control.Applicative ((<$>), (<*>))
type These a b = Either (Either a b) (a, b)
maybeA :: These a b -> Maybe a
maybeA (Left (Left a)) = Just a
maybeA (Right (a, _)) = Just a
maybeA _ = Nothing
maybeB :: These a b -> Maybe b
maybeB (Left (Right b)) = Just b
maybeB (Right (_, b)) = Just b
maybeB _ = Nothing
eitherA :: These a b -> Either a b
eitherA (Left (Left a)) = Left a
eitherA (Right (a, _)) = Left a
eitherA (Left (Right b)) = Right b
eitherB :: These a b -> Either a b
eitherB (Left (Right b)) = Right b
eitherB (Right (_, b)) = Right b
eitherB (Left (Left a)) = Left a
hasBoth, hasJustA, hasJustB, hasA, hasB :: These a b -> Bool
hasBoth (Right _) = True
hasBoth _ = False
hasJustA (Left (Left _)) = True
hasJustA _ = False
hasJustB (Left (Right _)) = True
hasJustB _ = False
hasA = (||) <$> hasBoth <*> hasJustA
hasB = (||) <$> hasBoth <*> hasJustB
이유는 두 가지 유형에서 중지 :
결합 운영자는 재미? 삼 사, ...? :-) –
"1, A + B + A * B = (1 + A) * (1 + B)'또는'(어쩌면 A, 아마 B)'. – rampion