Created
June 5, 2019 08:04
-
-
Save shkesar/148f8e9c2cfe8756ead370a96bf66f2a to your computer and use it in GitHub Desktop.
Haskell Natural Numbers
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- 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