import Char

-- 3.13
charToNum :: Char -> Int
charToNum c
    | isDigit c = ord c - ord '0'
    | otherwise = 0

-- 3.14
averageThree ::  Integer -> Integer -> Integer -> Float
averageThree x y z = fromInteger(x + y + z) / 3

-- 4.1
minThree :: Int -> Int -> Int -> Int
minThree x y z = min x (min y z)

maxThree ::  Int -> Int -> Int -> Int
maxThree x y z
    | x >= y && x >= z = x
    | y >= z = y
    | otherwise = z

maxFourVOne :: Int -> Int -> Int -> Int -> Int
maxFourVOne a b c d
    | a >= b && a >= c && a >= d = a
    | b >= c && b >= d = b
    | c >= d = c
    | otherwise = d

maxFourVTwo :: Int -> Int -> Int -> Int -> Int
maxFourVTwo a b c d = max a (max b (max c d))

maxFourVThree :: Int -> Int -> Int -> Int -> Int
maxFourVThree a b c d = max a (maxThree b c d)

-- The better method  is to use 'max' version  because it's quite easy
-- to write 'maxFour' using 'max'

-- 4.7
times :: Int -> Int -> Int
times x y
    | y == 0 = 0
    | otherwise = times x (y - 1) + x

power :: Int -> Int -> Int
power x y
    | y == 0 = 1
    | otherwise = times (power x (y - 1)) x

year :: (Int, String, Int) -> Int
year (day, month, year) = year

-- 5.2
between :: Int -> Int -> Int -> Bool
between x y z = ( x >= y && x <= z) || ( x >= z && x <= y)

middle :: Int -> Int -> Int -> Int
middle x y z
    | between x y z = x
    | between y x z  = y
    | otherwise = z

orderTriple :: (Int, Int, Int) -> (Int, Int, Int)
orderTriple (x, y, z) = (minThree x y z, middle x y z, maxThree x y z)

-- List comprehensions

-- Main> [0, 0.1 .. 1]
-- [0.0,0.1,0.2,0.3,0.4,0.5,0.6,0.7,0.8,0.9,1.0]

doubleall :: [Int] -> [Int]
doubleall x = [ 2 * a | a <- x ]

divisors :: Int -> [Int]
divisors n = [ d | d <- [1..n], n `mod` d == 0 ]

squares :: [Int] -> [Int]
squares l = [ d * d | d <- l ]

trebleall :: [Int] -> [Int]
trebleall l = [ 3 * d | d <- l, d < 10 ]

-- 5.11
matches :: Int -> [Int] -> [Int]
matches a l = [ d | d <- l, d == a ]

myelem :: Int -> [Int] -> Bool
myelem x l = (matches x l) /= []

