Advertisement
Coriic

Untitled

Jun 13th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. data NonEmptyList a = Element a (NonEmptyList a) | LastElement a deriving Show
  2.  
  3. length:: NonEmptyList a -> Int
  4. length (LastElement _) = 1
  5. length (Element _ list) = 1 + Main.length list
  6.  
  7. map:: (a->b)->NonEmptyList a->NonEmptyList b
  8. map f (LastElement a) = LastElement (f a)
  9. map f (Element a list) = Element (f a) (Main.map f list)
  10.  
  11. foldl:: (a->a->a)->a->NonEmptyList a->a
  12. foldl f acc (LastElement a) = f acc a
  13. foldl f acc (Element a list) = Main.foldl f (f acc a) list
  14.  
  15. foldr:: (a->a->a)->a->NonEmptyList a->a
  16. foldr f acc (LastElement a) = f acc a
  17. foldr f acc (Element a list) = f a (Main.foldr f acc list)
  18.  
  19. infixr 5 .++
  20. (.++):: NonEmptyList a -> NonEmptyList a -> NonEmptyList a
  21. (LastElement first) .++ second = Element first second
  22. (Element first firstList) .++ other = Element first (firstList .++ other)
  23.  
  24. elem:: Eq a=>a->NonEmptyList a->Bool
  25. elem a (LastElement b) = (a==b)
  26. elem a (Element head list) = (a==head) || Main.elem a list
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement