— CPSC 312 – 2021 – Types in Haskell
module Types where
— To run it, try:
— ghci
— :load Types
— Try
— :type True
— :type not
— mynot is an implementation of not
mynot True = False
mynot False = True
— xor1 is exclusive-or
xor1 a b = (a && not b) || (not a && b)
— xor is exclusive-or
xor2 x y = (x || y) && (not x || not y)
— myxor is exclusive-or
xor True x = not x
xor False x = x
— myif condition then_exp else_exp is an implementaion of if-then-else
myif True then_exp else_exp = then_exp
myif False then_exp else_exp = else_exp
— fac n is the factorial of n
fac :: Integer -> Integer
fac n = myif (n==0) 1 (n*fac (n-1))
— gfac n is a generalied factorial that also works for reals
gfac n = myif (n<=0) 1 (n*gfac (n-1))
-- mymax x y returns the maximum of x and y
mymax x y
| x>y = x
| otherwise = y
— notmymax x y returns the maximum of x and y
notmymax x y
| x>y = x
| x
| y >= z = y
| otherwise = z
— max3a x y z returns the maximun of x y z
max3a x y z = mymax x (mymax y z)
— max3a x y z returns the maximun of x y z
max3b x y = mymax (mymax x y)
max3c x y z = mymax (mymax x y) z