DEV Community

Discussion on: No duplicates challenge in Elm

Collapse
antonrich profile image
Anton Author

Interesting this code

noDupes : List a -> List a
noDupes list =
    case list of
        [] -> []

        [x] -> [x]

        x::y::xs ->
            if x == y then
                x :: noDupes xs
            else
                [x]

when applied to

noDupes [ 2, 2, 2, 1, 1, 1 ]

returns

[2, 2]
Collapse
1hko profile image
1hko • Edited on

the x::y::xs -> case does not recur in the else 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.