DEV Community

Discussion on: No duplicates challenge in Elm

Collapse
 
avalander profile image
Avalander • Edited

You're forgetting to handle the case of a list with a single element.

Also, in the case x :: y :: xs, when x is not equal to y you should return x :: (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.

Collapse
 
antonrich profile image
Anton

Now I have some missing possibilities:

This `case` does not have branches for all possibilities:

 9|>    case list of
10|>        [] -> []
11|>        x::y::[]->
12|>            if x == y then
13|>                x :: list
14|>            else
15|>                x :: y :: list
16|>        x::y::xs ->
17|>            if x == y then
18|>                x :: noDupes xs
19|>            else
20|>                x :: (noDupes (y::xs))

Missing possibilities include:

    [_]

I would have to crash if I saw one of those. Add branches for them!

Hint: If you want to write the code for each branch later, use `Debug.todo` as a
placeholder. Read <https://elm-lang.org/0.19.0/missing-patterns> for more
guidance on this workflow.

Collapse
 
1hko profile image
1hko • Edited

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.

Thread Thread
 
antonrich profile image
Anton

I'm taking a note of "checking for a list with exactly two elements is not effective for this particular program."

Collapse
 
antonrich profile image
Anton

How do I pattern match on the list with two elements?

noDupes : List a -> List a
noDupes list =
    case list of
        [] -> []
        x::y ->
            if x == y then
                x :: list
            else
                x :: y :: list
        x::y::xs ->
            if x == y then
                x :: noDupes xs
            else
                x :: (noDupes y::xs)

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.