{-
    Opgaver på ugeseddel 1
-}
module Uge1 where
{-
    NB: Jvf. opgave 2.1 burde modulet hedde UsePictures
    i stedet for som her Uge1
-}
import Pictures hiding (rotate)

-- Opgave 2.1

-- blackHorse (p. 8)
blackHorse :: Picture
blackHorse = invertColour horse

-- rotate (p. 11)
rotate :: Picture -> Picture
rotate = flipH . flipV

-- rotateHorse (p. 11)
rotateHorse :: Picture
rotateHorse = rotate horse

-- Opgave 2.2

black :: Picture
black = superimpose (invertColour horse) (horse)

-- Opgave 2.3

white :: Picture
white = invertColour black

chess2x2 :: Picture
chess2x2 = above (sideBySide white black) (sideBySide black white)

-- dette kan laves rekursivt vha. følgende funktion

pic2x2 :: Picture -> Picture
pic2x2 pic = above (sideBySide pic pic) (sideBySide pic pic)

chess8x8 :: Picture
chess8x8 = (pic2x2 . pic2x2) chess2x2

-- Opgave 2.4

fourHorses1 :: Picture
fourHorses1 = above (sideBySide horse blackHorse) 
                    (sideBySide blackHorse horse)

fourHorses2 :: Picture
fourHorses2 = above (sideBySide horse blackHorse) 
                    (sideBySide (flipV blackHorse) (flipV horse))

fourHorses3 :: Picture
fourHorses3 = above (sideBySide horse blackHorse)
                    (sideBySide (rotate blackHorse) (rotate horse))


-- Opgave 4.1

-- først maxThree fra p. 39
maxThree :: Int -> Int -> Int -> Int
maxThree x y z
  | x >= y && x >= z    = x
  | y >= z              = y
  | otherwise           = z

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

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

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

-- Opgave 4.2

weakAscendingOrder :: Int -> Int -> Int -> Bool
weakAscendingOrder a b c = (a <= b) && (b <= c)

between :: Int -> Int -> Int -> Bool
between a b c =
  (weakAscendingOrder a b c) || (weakAscendingOrder c b a) 

-- Opgave 4.3

howManyEqual ::  Int -> Int -> Int -> Int
howManyEqual a b c
  | (a == b) && (a == c)               = 3
  | (a == b) || (a == c) || (b == c)   = 2
  | otherwise                          = 0

-- Opgave 4.8

integerSqrt :: Int -> Int
integerSqrt n
  | n == 0    = 0
  | n > 0     = if (m+1)^2 <= n then (m+1) else m
  where
    m = integerSqrt (n-1)

-- Opgave 4.10

f :: Int -> Int
f x = (x - 7)^2

anyZeros :: Int -> Bool
anyZeros n
  | n < 0     = False
  | f n == 0  = True
  | otherwise = anyZeros (n-1)

-- Opgave 4.14

powerTwo :: Int -> Int
powerTwo n
  | n == 0     = 1
  | even n     = (powerTwo (n `div` 2)) ^ 2
  | otherwise  = (powerTwo (n - 1)) * 2

