{-
 -  Naam    : R.A.J. Wacanno
 -  UvAnetID: 11741163
 -  Studie  : BSc Informatica
 -
 -  Puzzles.hs
 -  - Various rewritten haskell functions based on certain conditions.
 -}
module Puzzles
where

--1
-- Returns the length of a given list.
length' :: [a] -> Int
length' list = foldr (\_ lastResult -> lastResult + 1) 0 list

--2
-- Performs the or operation on a combination of all elements in a
-- given list.
or' :: [Bool] -> Bool
or' list = foldr (\element lastResult -> element || lastResult) False list

--3
-- Determines whether a given list contains a given element.
elem' :: Eq a => a -> [a] -> Bool
elem' elementW list = foldr (\element lastResult -> (elementW == element) ||
                                                    lastResult) False list

--4
-- Rewritten map function.
map' :: (a -> b) -> [a] -> [b]
map' function list = foldr (\element lastResult -> (function element) :
                                                   lastResult)
                           []
                           list

--5
-- Combines two given lists into one.
pp :: [a] -> [a] -> [a]
pp listX listY = foldr (\element lastResult -> element : lastResult)
                       listY
                       listX

--6
-- Reverses a given list using foldr.
reverseR :: [a] -> [a]
reverseR list = foldr (\element lastResult -> lastResult ++ [element]) [] list

--7
-- Reverses a given list using foldl.
reverseL :: [a] -> [a]
reverseL list = foldl (\lastResult element -> element : lastResult) [] list

--8
-- Returns the first item of a tuple.
fst' :: (a, b, c) -> a
fst' (a, _, _) = a

-- Helper functione which returns a tuple with the given element as
-- its first element when the interegs, its second and third element,
-- are equal. The tuples second element is incremented each time.
exex' :: (a, Int, Int) -> a -> (a, Int, Int)
exex' (wanElem, curIndex, index) element
    | curIndex == index = (element, (curIndex + 1), index)
    | otherwise         = (wanElem, (curIndex + 1), index)

-- Returns an element from a given list kept at a given index.
exex :: [a] -> Int -> a
exex list index = fst' (foldl exex' ((head list), 0, index) list)

--9
-- Determines wheter a given list is a palindrome.
isPalindrome :: [a] -> Bool
isPalindrome string = string == reverse string

--10
-- Returns the Fibonacci sequence as a list.
fib :: [Integer]
fib = scanl (+) 0 (1:fib)
