There is a 99 Elm problems book (originally was for Prolog).
Write a function to remove consecutive duplicates of list elements.
noDupes [1, 1, ...
For further actions, you may consider blocking this person and/or reporting abuse
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.How do I pattern match on the list with two elements?
And in the case of two element how do you add that to the rest of the list? As you can see I use "list" in the second case. I don't think it's correct.
Now I have some missing possibilities:
You cases check for 1) an empty list, 2) a list with exactly two elements, and 3) a list with 2 elements and a tail. You forgot to check for the singleton list (a list with exactly one element) which is what the compiler is warning you about. Note, checking for a list with exactly two elements is not effective for this particular program.
I'm taking a note of "checking for a list with exactly two elements is not effective for this particular program."
Interesting this code
when applied to
returns
the
x::y::xs ->
case does not recur in theelse
branch, so an input of[1,2,2,2,3,3,3]
will simply return[1]
when the first two elements of the input do not match - leaving the[2,2,2,3,3,3]
portion completely unprocessed.Consider this implementation
When we test it
I had this noDupes (a :: more) in the else branch when experimented with the code but didn't consider putting that in the first branch.
Thank you. I appreciate your help.
Haha,I like to think I know FP well, but, when I try to do recursive functions like this, my whole brain freezes up 😅
It's all about mastering inductive reasoning.
Or
Pattern matching streamlines this for us by allowing us to express the base case and any induction steps using a nice syntax
Or