nth :: [a] -> Integer -> a
nth (h:t) 1 = h
nth (h:t) n = nth t (n-1)

{--
nth [3,4,5,6] 3
--}

nth' :: [a] -> Integer -> a
nth' l n = case l of (h:t) -> if n == 1 
                                 then h 
                                 else nth' t (n-1)

{--Run-time errors in case pattern-matching
nth' [] 1
nth' [1] 2
--}

length' :: [a] -> Integer
length' []    = 0
length' (h:t) = 1 + length' t

{-
length' [3,4,5,6]
-}


length'c :: [a] -> Integer
length'c l = case l of []    -> 0
                       (h:t) -> 1 + length'c t
                     
{-
length'c [3,4,5,6]
-}

length'' :: [a] -> Integer
length'' (h:t) = 1 + length'' t
length'' l = 0

{--
length'' [3,4,5,6]
--}

length''' (h:t) = 1 + length''' t
length''' l = 0

{--
length''' [3,4,5,6]
--}


length'''c :: [a] -> Integer
length'''c l = case l of (h:t) -> 1 + length'''c t
                         l -> 0

{--
length'''c [3,4,5,6]
--}


squareList :: [Integer] -> [Integer]
squareList [] = []
squareList (h:t) = h*h : squareList t

{--
squareList [3,4,5,6]
--}

mapList :: (a -> b) -> [a] -> [b]
mapList f [] = []
mapList f (h:t) = f h : mapList f t

{--
mapList (\x -> x*x) [3,4,5,6]
mapList (\x -> 2*x) [3,4,5,6]
--}
