{- File: PracticeAnswers.hs Desc: some practice stuff for basic haskell and functional programming concepts -} module PracticeAnswers where {- Function: myflip takes a list and reverses every other element if the list is of odd length it will leave the last element alone -} myflip (x:y:xs) = y:x:myflip(xs) myflip x = x {- Function: removeplace removes an element in a list at position i -} removeplace ([], i) = [] removeplace (x:xs, i) = if i == 1 then removeplace (xs, 0) else x:removeplace(xs, i - 1) {- Function: member takes an element n returns true if it is in the list, false if not -} member (n, []) = False member (n, x:xs) = if x == n then True else member(n, xs) {- Function: summation given a list of integers, add them together must use recursion and not the built in function -} summation [] = 0 summation (x:xs) = x + summation(xs) {- Function: fac find the factorial of a given integer -} fac 0 = 1 fac x = x * fac( x - 1) {- Function: addone given a list of integers, add 1 to each item of the list -} add x y = x + y addonetoall (x:xs) = map (add 1) (x:xs) {- Function: myreverse return a list of items in reverse order -} myreverse :: [a] -> [a] myreverse (x:[]) = [] myreverse (x:xs) = xs ++ [x]