99-haskell-problems

commit d7367934f46739ad276a6af12ea3c78d6713d1fe

tree

parent:
1795c06d91a38ef3e3f3b2461f49197aaab94259

Nick Mykins <nickmykins@transfix.io>

2018-01-25T11:34:50-05:00

more descriptive problem titles

diff --git a/99_problems.hs b/99_problems.hs
index ce54b689ac89d5efccfb7041d4f46635d43c7ff7..964ce2a8c7c28fa98d27420335e7b18f0856a1cd 100644
--- a/99_problems.hs
+++ b/99_problems.hs
@@ -1,4 +1,4 @@
--- Problem 1
+-- 1. Implement `last`
 myLast :: [a] -> a
 myLast [] = error "Empty list"
 myLast [x] = x
@@ -8,7 +8,7 @@ myLast' :: [a] -> a
 myLast' = head . reverse
 
 
--- Problem 2
+-- 2. Write a function that returns the penultimate item in a list
 myButLast :: [a] -> a
 myButLast xs
     | len < 2 = error "Too few elements!"
@@ -16,7 +16,7 @@     | otherwise = xs !! (len - 2)
     where len = length xs
 
 
--- Problem 3
+-- 3. Implement `!!`
 elementAt :: [a] -> Int -> a
 elementAt list index
     | index < 1 = error "Index must be a positive number!"
@@ -33,25 +33,25 @@     where elementAtHead = drop (index - 1) list
     -- This works fine for infinite lists
 
 
--- Problem 4
+-- 4. Implement `length`
 myLength :: [a] -> Int
 myLength [] = 0
 myLength (x:xs) = 1 + myLength xs
 
 
--- Problem 5
+-- 5. Implement `reverse`
 myReverse :: [a] -> [a]
 myReverse [] = []
 myReverse [x] = [x]
 myReverse list = (myLast list):(myReverse $ init list)
 
 
--- Problem 6
+-- 6. Write a function to determine whether a list is a palindrome
 isPalindrome :: Eq a => [a] -> Bool
 isPalindrome list = myReverse list == list
 
 
--- Problem 7
+-- 7. Flatten an arbitrarily-nested list
 data NestedList a = Elem a | List [NestedList a]
 
 flatten :: NestedList a -> [a]
@@ -60,7 +60,7 @@ flatten (List []) = [] 
 flatten (List (x:xs)) = (flatten x) ++ (flatten $ List xs)
 
 
--- Problem 8
+-- 8. Collapse adjacent identical list elements to a single element
 compress :: Eq a => [a] -> [a]
 compress [] = []
 compress [x] = [x]
@@ -69,12 +69,13 @@                   then compress xs 
                   else x:(compress xs)
 
 
--- Problem 9
+-- 9. Separate runs of identical elements into sublists
 pack :: Eq a => [a] -> [[a]]
 pack [] = []
 pack (x:xs) = (x : (takeWhile (== x) xs)) : pack (dropWhile (== x) xs)
 
 
--- Problem 10
+-- 10. Encode runs of identical elements into tuples: 
+--     (<number of elements in run>, <element>)
 encode :: Eq a => [a]-> [(Int, a)]
 encode = map (\l -> (length l, head l)) . pack