fork(1) download
  1. import Data.Foldable (foldMap)
  2. import Data.Monoid (First(First, getFirst))
  3. import Data.Maybe (fromMaybe)
  4.  
  5. data Fruit = Fruit { color :: String, weight :: Int }
  6.  
  7. theFruitIsRed :: Fruit -> Maybe String
  8. theFruitIsRed (Fruit "red" _) = Nothing
  9. theFruitIsRed _ = Just "Fruit not red"
  10.  
  11. theFruitIsNotTooHeavy :: Fruit -> Maybe String
  12. theFruitIsNotTooHeavy (Fruit _ w)
  13. | w < 500 = Nothing
  14. | otherwise = Just "Too heavy"
  15.  
  16. checkRules :: Fruit -> Maybe String
  17. checkRules = getFirst . foldMap (First .)
  18. [ theFruitIsRed
  19. , theFruitIsNotTooHeavy
  20. ]
  21.  
  22. main = mapM (putStrLn . fromMaybe "No problems!" . checkRules)
  23. [ Fruit "red" 1000
  24. , Fruit "red" 10
  25. , Fruit "blue" 600
  26. , Fruit "blue" 100
  27. ]
Success #stdin #stdout 0s 8388607KB
stdin
Standard input is empty
stdout
Too heavy
No problems!
Fruit not red
Fruit not red