https://wiki.haskell.org/api.php?action=feedcontributions&user=Flowerking&feedformat=atomHaskellWiki - User contributions [en]2024-03-28T15:23:39ZUser contributionsMediaWiki 1.35.5https://wiki.haskell.org/index.php?title=99_questions/Solutions/8&diff=4138199 questions/Solutions/82011-08-01T01:38:20Z<p>Flowerking: </p>
<hr />
<div>(**) Eliminate consecutive duplicates of list elements.<br />
<br />
<haskell><br />
compress :: Eq a => [a] -> [a]<br />
compress = map head . group<br />
</haskell><br />
<br />
We simply group equal values together (using Data.List.group), then take the head of each. <br />
Note that (with GHC) we must give an explicit type to ''compress'' otherwise we get:<br />
<br />
<haskell><br />
Ambiguous type variable `a' in the constraint:<br />
`Eq a'<br />
arising from use of `group' <br />
Possible cause: the monomorphism restriction applied to the following:<br />
compress :: [a] -> [a]<br />
Probable fix: give these definition(s) an explicit type signature<br />
or use -fno-monomorphism-restriction<br />
</haskell><br />
<br />
We can circumvent the monomorphism restriction by writing ''compress'' this way (See: section 4.5.4 of [http://haskell.org/onlinereport the report]):<br />
<br />
<haskell>compress xs = map head $ group xs</haskell><br />
<br />
An alternative solution is<br />
<br />
<haskell><br />
compress (x:ys@(y:_))<br />
| x == y = compress ys<br />
| otherwise = x : compress ys<br />
compress ys = ys<br />
</haskell><br />
<br />
Another possibility using foldr<br />
<br />
<haskell><br />
compress :: (Eq a) => [a] -> [a]<br />
compress = foldr skipDups []<br />
where skipDups x [] = [x]<br />
skipDups x acc<br />
| x == head acc = acc<br />
| otherwise = x : acc<br />
</haskell><br />
<br />
A very simple approach:<br />
<br />
<haskell><br />
compress [] = []<br />
compress (x:xs) = [x] ++ (compress $ dropWhile (== x) xs)<br />
</haskell><br />
<br />
Another approach, using foldr<br />
<br />
<haskell><br />
compress :: Eq a => [a] -> [a]<br />
compress x = foldr (\a b -> if a == (head b) then b else a:b) [last x] x<br />
</haskell></div>Flowerkinghttps://wiki.haskell.org/index.php?title=99_questions/Solutions/8&diff=4135399 questions/Solutions/82011-07-29T03:22:32Z<p>Flowerking: </p>
<hr />
<div>(**) Eliminate consecutive duplicates of list elements.<br />
<br />
<haskell><br />
compress :: Eq a => [a] -> [a]<br />
compress = map head . group<br />
</haskell><br />
<br />
We simply group equal values together (using Data.List.group), then take the head of each. <br />
Note that (with GHC) we must give an explicit type to ''compress'' otherwise we get:<br />
<br />
<haskell><br />
Ambiguous type variable `a' in the constraint:<br />
`Eq a'<br />
arising from use of `group' <br />
Possible cause: the monomorphism restriction applied to the following:<br />
compress :: [a] -> [a]<br />
Probable fix: give these definition(s) an explicit type signature<br />
or use -fno-monomorphism-restriction<br />
</haskell><br />
<br />
We can circumvent the monomorphism restriction by writing ''compress'' this way (See: section 4.5.4 of [http://haskell.org/onlinereport the report]):<br />
<br />
<haskell>compress xs = map head $ group xs</haskell><br />
<br />
An alternative solution is<br />
<br />
<haskell><br />
compress (x:ys@(y:_))<br />
| x == y = compress ys<br />
| otherwise = x : compress ys<br />
compress ys = ys<br />
</haskell><br />
<br />
Another possibility using foldr<br />
<br />
<haskell><br />
compress :: (Eq a) => [a] -> [a]<br />
compress = foldr skipDups []<br />
where skipDups x [] = [x]<br />
skipDups x acc<br />
| x == head acc = acc<br />
| otherwise = x : acc<br />
</haskell><br />
<br />
A very simple approach:<br />
<br />
<haskell><br />
compress [] = []<br />
compress (x:xs) = [x] ++ (compress $ dropWhile (== x) xs)<br />
</haskell><br />
<br />
Another approach, using foldr<br />
<br />
<haskell><br />
compress :: Eq a => [a] -> [a]<br />
compress x = foldr (\a b -> if a == (head b) then b else a:b) [last x] x<br />
</haskell><br />
<br />
Very plain:<br />
<br />
<haskell><br />
compress :: (Eq a) => [a] -> [a]<br />
compress [] = []<br />
compress [x] = [x]<br />
compress (x:y:xs)<br />
| x == y = compress (x:xs)<br />
| otherwise = x: compress (y:xs)</div>Flowerking