Guest User

Untitled

a guest
Jan 17th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. any (>3) [1,2,3,4,5]
  2.  
  3. any and[(>3),(<5)] [1,2,3,4,5]
  4.  
  5. all or[(<2),(>4)] [1,2,3,4,5]
  6.  
  7. any (x -> x > 3 && x < 5) [1..5]
  8.  
  9. any (x -> x < 2 || x > 4) [1..5]
  10.  
  11. infixr 3 &&&
  12. infixr 3 |||
  13.  
  14. (&&&) :: (a -> Bool) -> (a -> Bool) -> (a -> Bool)
  15. (f &&& g) x = f x && g x
  16.  
  17. (|||) :: (a -> Bool) -> (a -> Bool) -> (a -> Bool)
  18. (f ||| g) x = f x || g x
  19.  
  20. any ((>3) &&& (<5)) [1..5]
  21. any ((<2) ||| (>4)) [1..5]
  22.  
  23. andP :: [a -> Bool] -> a -> Bool
  24. andP ps x = all ($ x) ps
  25.  
  26. andP [(>3), (<5)] x = x > 3 && x < 5
  27.  
  28. any (andP [(>3), (<5)]) [1,2,3,4,5]
  29.  
  30. between :: (Ord a) => a -> a -> a -> Bool
  31. between lo hi x = lo < x && x < hi
  32.  
  33. any (between 3 5) [1,2,3,4,5]
  34.  
  35. > import Data.Monoid
  36. > getAll $ mconcat [All True, All True, All False]
  37. False
  38. > getAll $ mconcat [All True, All True, All True]
  39. True
  40. > getAny $ mconcat [Any True, Any True, Any False]
  41. True
  42.  
  43. instance Monoid b => Monoid (a -> b) where
  44. mempty _ = mempty
  45. mappend f g x = f x `mappend` g x
  46.  
  47. > :t All
  48. All :: Bool -> All
  49. > :t (<5)
  50. (<5) :: (Num a, Ord a) => a -> Bool
  51. > :t All . (<5)
  52. All . (<5) :: (Num a, Ord a) => a -> All
  53. > :t ((All . (<5)) <> (All . (>3)))
  54. ((All . (<5)) <> (All . (>3))) :: (Num a, Ord a) => a -> All
  55. > getAll $ ((All . (<5)) <> (All . (>3))) 4
  56. True
  57.  
  58. > getAll $ mconcat [(All. (<5)), (All . (>3))] $ 4
  59. True
  60. > getAll $ mconcat (map (All .) [(<5), (>3)]) $ 4
  61. True
  62.  
  63. > import Data.Foldable
  64. > getAll $ foldMap (All .) [(<5), (>3)] $ 4
  65. True
  66.  
  67. > map (getAll . foldMap (All .) [(<5), (>3)]) $ [1..5]
  68. [False,False,False,True,False]
  69. > Prelude.or $ map (getAll . foldMap (All .) [(<5), (>3)]) $ [1..5]
  70. True
  71.  
  72. test = any (andP [(>3),(<5)]) [1,2,3,4,5]
  73.  
  74. andP :: [a -> Bool] -> a -> Bool
  75. andP ps = getAll . mconcat (map (All.) ps)
Add Comment
Please, Sign In to add comment