Personal tools

99 questions/Solutions/9

From HaskellWiki

< 99 questions | Solutions(Difference between revisions)
Jump to: navigation, search
 
(8 intermediate revisions by 6 users not shown)
Line 1: Line 1:
 
(**) Pack consecutive duplicates of list elements into sublists.
 
(**) Pack consecutive duplicates of list elements into sublists.
 +
 
If a list contains repeated elements they should be placed in separate sublists.
 
If a list contains repeated elements they should be placed in separate sublists.
  
Line 7: Line 8:
 
pack [] = []
 
pack [] = []
 
</haskell>
 
</haskell>
 +
 +
This is implemented as <hask>group</hask>&nbsp;in <hask>Data.List</hask>.
  
 
A more verbose solution is
 
A more verbose solution is
 +
 
<haskell>
 
<haskell>
 
pack :: Eq a => [a] -> [[a]]
 
pack :: Eq a => [a] -> [[a]]
Line 21: Line 25:
 
</haskell>
 
</haskell>
  
This is implemented as <hask>group</hask> in <hask>Data.List</hask>.
+
Similarly, using <hask>splitAt</hask>&nbsp;and <hask>findIndex</hask>:
 +
 
 +
<haskell>
 +
pack :: Eq a => [a] -> [[a]]
 +
pack [] = []
 +
pack (x:xs) = (x:reps) : (pack rest)
 +
    where
 +
        (reps, rest) = maybe (xs,[]) (\i -> splitAt i xs)
 +
                        (findIndex (/=x) xs)
 +
</haskell>
 +
 
 +
Another solution using <hask>takeWhile</hask>&nbsp;and <hask>dropWhile</hask>:
 +
 
 +
<haskell>
 +
pack :: (Eq a) => [a] -> [[a]]
 +
pack [] = []
 +
pack (x:xs) = (x : takeWhile (==x) xs) : pack (dropWhile (==x) xs)
 +
</haskell>
 +
 
 +
Or we can use <hask>foldr</hask>&nbsp;to implement this:
 +
 
 +
<haskell>
 +
pack :: (Eq a) => [a] -> [[a]]
 +
pack = foldr func []
 +
    where func x []    = [[x]]
 +
          func x (y:xs) =
 +
              if x == (head y) then ((x:y):xs) else ([x]:y:xs)   
 +
</haskell>
 +
 
 +
A simple solution:
 +
<haskell>
 +
pack :: (Eq a) => [a] -> [[a]]
 +
pack [] = []
 +
pack [x] = [[x]]
 +
pack (x:xs) = if x `elem` (head (pack xs))
 +
              then (x:(head (pack xs))):(tail (pack xs))
 +
              else [x]:(pack xs)
 +
 
 +
pack' [] = []
 +
pack' [x] = [[x]]
 +
pack' (x:xs)
 +
    | x == head  h_p_xs = (x:h_p_xs):t_p_hs
 +
    | otherwise        = [x]:p_xs
 +
    where p_xs@(h_p_xs:t_p_hs) = pack' xs
 +
</haskell>
 +
 
 +
<haskell>
 +
myPack [] = []
 +
myPack (y:ys) = impl ys [[y]]
 +
where
 +
impl [] packed = packed
 +
impl (x:xs) packed
 +
| x == (head (last packed)) = impl xs ((init packed) ++ [x:(last packed)])
 +
| otherwise     = impl xs (packed ++ [[x]])
 +
 
 +
myPack' [] = []
 +
myPack' (y:ys) = reverse $ impl ys [[y]]
 +
where
 +
impl [] packed = packed
 +
impl (x:xs) p@(z:zs)
 +
| x == (head z) = impl xs ((x:z):zs)
 +
| otherwise    = impl xs ([x]:p)
 +
</haskell>

Revision as of 13:33, 24 June 2012

(**) Pack consecutive duplicates of list elements into sublists.

If a list contains repeated elements they should be placed in separate sublists.

pack (x:xs) = let (first,rest) = span (==x) xs
               in (x:first) : pack rest
pack [] = []
This is implemented as
group
 in
Data.List
.

A more verbose solution is

pack :: Eq a => [a] -> [[a]]
pack [] = []
pack (x:xs) = (x:first) : pack rest
         where
           getReps [] = ([], [])
           getReps (y:ys)
                   | y == x = let (f,r) = getReps ys in (y:f, r)
                   | otherwise = ([], (y:ys))
           (first,rest) = getReps xs
Similarly, using
splitAt
 and
findIndex
:
pack :: Eq a => [a] -> [[a]]
pack [] = []
pack (x:xs) = (x:reps) : (pack rest)
    where
        (reps, rest) = maybe (xs,[]) (\i -> splitAt i xs)
                         (findIndex (/=x) xs)
Another solution using
takeWhile
 and
dropWhile
:
pack :: (Eq a) => [a] -> [[a]]
pack [] = []
pack (x:xs) = (x : takeWhile (==x) xs) : pack (dropWhile (==x) xs)
Or we can use
foldr
 to implement this:
pack :: (Eq a) => [a] -> [[a]]
pack = foldr func []
    where func x []     = [[x]]
          func x (y:xs) =
              if x == (head y) then ((x:y):xs) else ([x]:y:xs)

A simple solution:

pack :: (Eq a) => [a] -> [[a]]
pack [] = []
pack [x] = [[x]]
pack (x:xs) = if x `elem` (head (pack xs))
              then (x:(head (pack xs))):(tail (pack xs))
              else [x]:(pack xs)
 
pack' [] = []
pack' [x] = [[x]]
pack' (x:xs)
    | x == head  h_p_xs = (x:h_p_xs):t_p_hs
    | otherwise         = [x]:p_xs
    where p_xs@(h_p_xs:t_p_hs) = pack' xs
myPack [] = []
myPack (y:ys) = impl ys [[y]]
	where
		impl [] packed = packed
		impl (x:xs) packed 
			| x == (head (last packed)) = impl xs ((init packed) ++ [x:(last packed)])
			| otherwise		    = impl xs (packed ++ [[x]])
 
myPack' [] = []									
myPack' (y:ys) = reverse $ impl ys [[y]]
	where
		impl [] packed = packed
		impl (x:xs) p@(z:zs) 
			| x == (head z) = impl xs ((x:z):zs) 
			| otherwise     = impl xs ([x]:p)