module S01 (
  -- * Exercise 3
  n,

  -- * Exercise 4
  last, last',

  -- * Exercise 5
  init, init',

  -- * Exercise 6
  gcd
  ) where
import Prelude hiding (last, init, gcd)

{-| The previous version was
 
> N = a 'div' length xs
>    where
>        a = 10
>      xs  = [1,2,3,4,5]

containing the following errors:

  1. @N@ is not allowed as function name (since it is uppercase).

  2. the proper way of turning a function infix is @\`div\`@ instead of @\'div\'@

  3. the second item of the where-group has to be indented exactly
    like the first one
-}
n :: Int
n = a `div` length xs
  where
    a  = 10
    xs = [1,2,3,4,5]

last :: [a] -> a
last xs  = head (reverse xs)
last' xs = head (drop (length xs - 1) xs)

init :: [a] -> [a]
init xs  = reverse (tail (reverse xs))
init' xs = take (length xs - 1) xs

gcd :: Int -> Int -> Int
gcd m n = gcd' (abs m) (abs n)
  where
    gcd' a 0  =  a
    gcd' a b  =  gcd' b (a `mod` b)