I have a list. I need to create a a new list, like in example below:
[3, 3, 1, 3] to [3, 3, 3, 1, 1, 3, 3].
can anybody tell what is wrong with my code?
add xs
= let
adding (x : xs) as
=
if x == head(xs) && length(xs) >= 1
then adding xs (x : as)
else adding xs (x : x : as)
adding _ as
= as
in
adding xs []
ghci tells that there is always empty list is xs, but i have xs length control.
[3, 3, 1, 3]turns into[3, 3, 3, 1, 1, 3, 3]. That's more than one element added. Did you mean that each element should be duplicated, resulting in a list of twice the original length? Your example doesn't show that either. Did you intend that the result should be[3, 3, 3, 3, 1, 1, 3, 3]?[1,2,3],[2,2,2],[],[1].concatMap (\(x:xs) -> x:x:xs)) . group.