Quick actions

cmd+k|ctrl+k

Navigation

Languages

Peano numbers

Snippet info

Language

Haskell

Visibility

public

Author

homam

Created

2016-09-13T11:14:21Z

Updated

2016-09-13T13:29:55Z

data P = Zero | Succ P deriving (Show, Read, Eq)
   
(.+.) :: P -> P -> P
Zero .+. y = y
Succ x .+. y = Succ (x .+. y)

one :: P
one = Succ Zero

two :: P
two = Succ one

toInt :: P -> Int
toInt Zero = 0
toInt (Succ x) = 1 + toInt x

fromInt :: Int -> P
fromInt 0 = Zero
fromInt i = Succ $ fromInt (i - 1)

main = do 
    print $ Zero .+. Zero
    print $ one .+. one
    print $ one .+. one == two
    print $ toInt $ two .+. one
    print $ fromInt 5
INFO