-- Computes the factorial of a number. fac :: Integer -> Integer fac n = if n == 0 then 1 else fac (n - 1) * n ---- Enumeration types -- Boolean values: -- data Bool = False | True -- Color type: data Color = Red | Blue | Yellow aColor :: Color aColor = Yellow colorValue :: Color -> Int colorValue Red = 0 colorValue Blue = 1 colorValue Yellow = 2 --- Record types: one constructor with arguments -- Complex numbers data Complex = Complex Float Float deriving Show aC :: Complex aC = Complex 1.5 2.4 -- Addition on complex numbers: addC :: Complex -> Complex -> Complex addC (Complex r1 i1) (Complex r2 i2) = Complex (r1 + r2) (i1 + i2) --- Mixed data types -- Integer lists: data IntList = Nil | Cons Int IntList deriving Show -- 1,2,3: list123 :: IntList list123 = Cons 1 (Cons 2 (Cons 3 Nil)) -- Concatenation of two integer lists: append :: IntList -> IntList -> IntList append Nil ys = ys append (Cons x xs) ys = Cons x (append xs ys)