Advertisement
Coriic

Untitled

Jun 11th, 2017
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --list
  2. data List a = Empty | Node a (List a)
  3.  
  4. map':: (a->b)->List a->List b
  5. map' f Empty = Empty
  6. map' f (Node a list) = Node (f a) (map' f list)
  7.  
  8. foldr':: (a->b->b)->b->List a->b
  9. foldr' f acc Empty = acc
  10. foldr' f acc (Node a list) = f a (foldr' f acc list)
  11.  
  12. foldl':: (b->a->b)->b->List a->b
  13. foldl' f acc Empty = acc
  14. foldl' f acc (Node a list) = foldl' f (f acc a) list
  15.  
  16. zipWith'::(a->b->c) -> List a -> List b -> List c
  17. zipWith' _ Empty _ = Empty
  18. zipWith' _ _ Empty = Empty
  19. zipWith' f (Node first firstList) (Node second secondList) = Node (f first second) (zipWith' f firstList secondList)
  20.  
  21. infixr 5 .++
  22. (.++):: List a -> List a -> List a
  23. Empty .++ list = list
  24. (Node a list) .++ other = Node a (list .++ other)
  25.  
  26. fromNormalList:: [a]->List a
  27. fromNormalList [] = Empty
  28. fromNormalList (x:xs) = Node x (fromNormalList xs)
  29.  
  30. toNormalList:: List a -> [a]
  31. toNormalList Empty = []
  32. toNormalList (Node a list) = a : toNormalList (list)
  33.  
  34. instance Functor List where
  35.    fmap = map'
  36.  
  37. instance Applicative List where
  38.     pure a = Node a Empty
  39.     fm <*> xm = fromNormalList [f x | f <- toNormalList fm, x <- toNormalList xm]
  40.  
  41. --tree
  42. data Tree a = Tree a [Tree a]
  43.  
  44. generateTree:: a->Int->Int->Tree a
  45. generateTree node 0 _ = Tree node []
  46. generateTree node depth branches = Tree node (replicate branches (generateTree node (depth-1) branches))
  47.  
  48. instance Functor Tree where
  49.     fmap f (Tree node []) = Tree (f node) []
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement