There is a 99 Elm problems book (originally was for Prolog).
Write a function to remove consecutive duplicates of list elements.
noDupes [1, 1, 2, 2, 3, 3, 3, 4, 5, 4, 4, 4, 4]
== [1, 2, 3, 4, 5, 4]
I'm doing something wrong here. Am I on the track or completely off track?
noDupes : List a -> List a
noDupes list =
case list of
[] -> []
x::y::xs ->
if x == y then
x :: noDupes xs
else
x::y::xs
x::xs ->
if x == xs then
x :: xs
else
x
The compiler already gave me a heads up that I'm comparing an element to a list:
17| if x == xs then
^^^^^^^
The left side of (==) is:
a
But the right side is:
List a
Different types can never be equal though! Which side is messed up?
Hint: Did you forget to add [] around it?
Interesting I just wrapped the x in [x]. But I now have the second the last case to deal with.
You're forgetting to handle the case of a list with a single element.
Also, in the case
x :: y :: xs
, whenx
is not equal toy
you should returnx :: (noDupes y :: xs)
to make sure the rest of the list gets deduplicated.I don't think you need to handle the case
x :: xs
at all, but you might need to handle the case of a list with two elements.