-- Problem 9
-- Pack consecutive duplicates of list elements into sublists. If a list
-- contains repeated elements they should be placed in separate sublists.

pack :: Eq a => [a] -> [[a]]
pack = foldr collect [[]]
    where collect :: (Eq a) => a -> [[a]] -> [[a]]
          collect x (a:acc)
            | null a      = [x]:acc
            | x == head a = (x:a):acc
            | otherwise   = [x]:a:acc

-- Problem 10
-- Run-length encoding of a list. Use the result of problem P09 to implement
-- the so-called run-length encoding data compression method. Consecutive
-- duplicates of elements are encoded as lists (N E) where N is the number of
-- duplicates of the element E. 
pack :: Eq a => [a] -> [[a]]
pack = foldr collect [[]]
    where collect :: (Eq a) => a -> [[a]] -> [[a]]
          collect x (a:acc)
            | null a      = [x]:acc
            | x == head a = (x:a):acc
            | otherwise   = [x]:a:acc

-- Problem 10
-- Run-length encoding of a list. Use the result of problem P09 to implement
-- the so-called run-length encoding data compression method. Consecutive
-- duplicates of elements are encoded as lists (N E) where N is the number of
-- duplicates of the element E. 
pack = foldr collect [[]]
    where collect :: (Eq a) => a -> [[a]] -> [[a]]
          collect x (a:acc)
            | null a      = [x]:acc
            | x == head a = (x:a):acc
            | otherwise   = [x]:a:acc

-- Problem 10
-- Run-length encoding of a list. Use the result of problem P09 to implement
-- the so-called run-length encoding data compression method. Consecutive
-- duplicates of elements are encoded as lists (N E) where N is the number of
-- duplicates of the element E. 