>>> maybe False odd (Just 3) True
>>> maybe False odd Nothing FalseRead an integer from a string using readMaybe. If we succeed, return twice the integer; that is, apply (*2) to it. If instead we fail to parse an integer, return 0 by default:
>>> import Text.Read ( readMaybe ) >>> maybe 0 (*2) (readMaybe "5") 10 >>> maybe 0 (*2) (readMaybe "") 0Apply show to a Maybe Int. If we have Just n, we want to show the underlying Int n. But if we have Nothing, we return the empty string instead of (for example) "Nothing":
>>> maybe "" show (Just 5) "5" >>> maybe "" show Nothing ""
maybeHasX 42 = Just 42 maybeHasX (XException msg) = Nothing maybeHasX (3, XException msg) = Nothing maybeHasX (XException msg, _|_) = _|_ maybeHasX (_|_, XException msg) = _|_ maybeHasX (3, _|_) = _|_ maybeHasX _|_ = _|_
maybeIsX 42 = Just 42 maybeIsX (XException msg) = Nothing maybeIsX (3, XException msg) = Just (3, XException msg) maybeIsX (3, _|_) = Just (3, _|_) maybeIsX _|_ = _|_
sometimes1 clk rst en = s where s = register clk rst en Nothing (switch <$> s) switch Nothing = Just 1 switch _ = Nothing countSometimes clk rst en = s where s = regMaybe clk rst en 0 (plusM (pure <$> s) (sometimes1 clk rst en)) plusM = liftA2 (liftA2 (+))We get:
>>> sampleN 9 (sometimes1 systemClockGen resetGen enableGen) [Nothing,Nothing,Just 1,Nothing,Just 1,Nothing,Just 1,Nothing,Just 1] >>> sampleN 9 (count systemClockGen resetGen enableGen) [0,0,0,1,1,2,2,3,3]
sometimes1 = s where s = register Nothing (switch <$> s) switch Nothing = Just 1 switch _ = Nothing countSometimes = s where s = regMaybe 0 (plusM (pure <$> s) sometimes1) plusM = liftA2 (liftA2 (+))We get:
>>> sampleN @System 9 sometimes1 [Nothing,Nothing,Just 1,Nothing,Just 1,Nothing,Just 1,Nothing,Just 1] >>> sampleN @System 9 countSometimes [0,0,0,1,1,2,2,3,3]