# Posted By

rtperson on 09/08/11

# Statistics

Viewed 205 times
Favorited by 0 user(s)

# Haskell 99 Problems, numbers 1 through 9

/ Published in: Haskell Save to your folder(s) I had originally started these problems from #10 (Run-length encoding). I went back and did 1-8 for completeness.

Copy this code and paste it in your HTML
`import Data.List list1 = [1,2,3,4,5,6] -- Problem 1: find the last element of a listlastList :: [a] -> alastList [] = error "list cannot be empty"lastList (x:[]) = xlastList (x:xs) = lastList xs -- Problem 2: Find the last but one element of a list. lastButOne :: [a] -> alastButOne [] = error "list cannot be empty"lastButOne (x:[]) = error "list must be larger than one"lastButOne [x,_] = xlastButOne (_:xs) = lastButOne xs  -- Problem 3: Find the K'th element of a list. The first element in the list is number 1. elementAt :: [a] -> Int -> aelementAt [] n = error "list cannot be empty"elementAt s@(x:xs) n     | n > (length s) = error "n cannot be larger than list"    | otherwise = if (length s) == n then x else elementAt xs n  -- Problem 4: find the number of elements of a listcountList :: [a] -> IntcountList [] = 0countList (x:xs) = 1 + countList xs -- Problem 5: reverse a list (I assume, not using "reverse")reverse' :: [a] -> [a]reverse' [] = []reverse' (x:xs) = reverse' xs ++ (x : []) -- Problem 6: find out whether a list is a palindromeisPalindrome :: (Eq a) => [a] -> BoolisPalindrome [] = TrueisPalindrome xs = let xs2 = reverse xs                       in (xs2 == xs) -- Problem 7: flatten a nested list structure-- NOTE: This doesn't strictly solve the problem. May want to revisit later.flatten :: [[a]] -> [a]flatten = foldr (++) [] {-    Problem 8: Eliminate consecutive duplicates of list elements.     If a list contains repeated elements they should be replaced with     a single copy of the element. The order of the elements should not be changed.         > compress ["a","a","a","a","b","c","c","a","a","d","e","e","e","e"]        ["a","b","c","a","d","e"] -} -- excellent opportunity to play around with mapAccumLcompress :: (Eq x) => [x] -> [x]compress xs = fst \$ mapAccumL (\ys s -> if (length ys > 0) && (last ys == s) then (ys, s) else (ys++s:[], s)) [] xs -- the simplest solution from the Wiki compress' :: Eq a => [a] -> [a]compress' = map head . group {-    Problem 9:  Pack consecutive duplicates of list elements into sublists.     If a list contains repeated elements they should be placed in separate sublists.        *Main> pack ['a', 'a', 'a', 'a', 'b', 'c', 'c', 'a', 'a', 'd', 'e', 'e', 'e', 'e']        ["aaaa","b","cc","aa","d","eeee"]-} pack :: (Eq a) => [a] -> [[a]]pack = group`

## Comments Subscribe to comments

You need to login to post a comment.