Skip to content

Instantly share code, notes, and snippets.

@shkesar
Created June 5, 2019 08:04
Show Gist options
  • Save shkesar/148f8e9c2cfe8756ead370a96bf66f2a to your computer and use it in GitHub Desktop.
Save shkesar/148f8e9c2cfe8756ead370a96bf66f2a to your computer and use it in GitHub Desktop.
Haskell Natural Numbers
-- Natural Numbers
data Nat = Zero | Succ Nat
instance Show Nat where
show Zero = "Zero"
show (Succ m) = printf "Succ (%s)" (show m)
nat2int :: Nat -> Int
nat2int Zero = 0
nat2int (Succ n) = 1 + nat2int n
int2nat :: Int -> Nat
int2nat 0 = Zero
int2nat n = Succ (int2nat (n-1))
add' :: Nat -> Nat -> Nat
add' Zero n = n
add' (Succ m) n = Succ (add' m n)
-- addNat :: Nat -> Nat -> Nat
-- addNat m n = int2nat (nat2int m + nat2int n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment