Advertisement
Arina0904

Практика 16.11.22

Nov 16th, 2022 (edited)
1,149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import Control.Monad
  2. --1
  3. a = [Just 2, Just 3, Nothing]
  4. b = a++[Nothing]
  5. c = map (fmap (^2)) a
  6. --2
  7. d = Just [1, 2, 3]
  8. e = fmap (map (^2)) d
  9. --3
  10. f = ([1, 2, 3], Just 2)
  11. g = fmap (fmap(^2)) f
  12. --4
  13. h = [Left "Hello", Right 7, Right 3, Left "Oops"]
  14. i = map (fmap (^2)) h
  15. --5
  16. j = Right [1, 2, 3]
  17. k = fmap (map (^2)) j
  18. --6
  19. l :: ([Maybe Int], Either String Int)
  20. l = ([Just 2], Right 5)
  21. m = fmap (fmap(^2)) l
  22. --7
  23. n = [Left "hello", Right (Just 5), Right Nothing, Right (Just 8)]
  24. o = fmap (fmap(fmap(^2))) n
  25. --8 Опр-ть представителя класса типов функтор для сл.типа:
  26. data Point3d a = Point3d a a a deriving Show
  27. instance Functor Point3d where
  28.     fmap f (Point3d x y z) = Point3d (f x) (f y) (f z)
  29. --9 Опр-ть представителя класса типов функтор для типа GeomPrimitive
  30. data GeomPrimitive a = Point (Point3d a) | LineSegment (Point3d a) (Point3d a) deriving Show
  31. instance Functor GeomPrimitive where
  32.     fmap f (Point (Point3d x y z)) = Point (Point3d (f x) (f y) (f z))
  33.     fmap f (LineSegment x y) = LineSegment (fmap f x) (fmap f y)
  34. --10
  35. ff = (\x -> map (2^) [0..x]) >=> (\x -> [0..x]) --первая и вторая стрелки Клейсли
  36. gg = \x -> map (2^) [0..x]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement