Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. module PeanoSum where
  2. -- Written by Marcelo Camargo <marcelocamargo@linuxmail.org> on
  3. -- Sat Oct 22 2016
  4.  
  5. -- Represent natural numbers
  6. data Nat = Zero
  7. | Succ Nat
  8.  
  9. -- Let neutral element of Monoid be id
  10. -- Let (Functor a) b become Functor (a .+. b)
  11. infixl .+.
  12. (.+.) :: Nat -> Nat -> Nat
  13. (.+.) Zero b = b
  14. (.+.) (Succ a) b = Succ ((.+.) a b)
  15.  
  16. -- Built only for visual representation with pipes
  17. instance Show Nat where
  18. show Zero = ""
  19. show (Succ n) = "|" ++ (show n)
  20.  
  21. -- Create aliases for 1 up to 9
  22. zero, one, two, three, four, five, six, seven, eight, nine :: Nat
  23. zero = Zero
  24. one = Succ zero
  25. two = Succ one
  26. three = Succ two
  27. four = Succ three
  28. five = Succ four
  29. six = Succ five
  30. seven = Succ six
  31. eight = Succ seven
  32. nine = Succ eight
  33.  
  34. -- Test our function definitions with pipes
  35. main = putStrLn . show $ eight .+. nine
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement