SHARE
TWEET

Untitled

a guest Feb 20th, 2019 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. data Nat = Zero | Suc Nat
  2.  
  3. fromNat :: Nat -> Integer
  4. fromNat Zero = 0
  5. fromNat (Suc n) = fromNat n + 1
  6.  
  7. add :: Nat -> Nat -> Nat
  8. add Zero Zero = Zero
  9. add Zero x = x
  10. add x Zero = x
  11. add (Suc xs) b@(Suc ys) = add (xs) (Suc b)
  12.  
  13. mul :: Nat -> Nat -> Nat
  14. mul Zero _ = Zero
  15. mul _ Zero = Zero
  16. mul a b = let
  17.     f _ Zero res = res
  18.     f Zero _ res = res
  19.     f (Suc xs) b res = f xs b (add b res)
  20.     in f a b Zero
  21.  
  22. fac :: Nat -> Nat
  23. fac Zero = Suc Zero
  24. fac x = let
  25.     f (Suc Zero) res = res
  26.     f (Suc ys) res = f ys (mul res ys)
  27.     in mul (f x (Suc Zero)) x
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top