You do it like this:

unique : List a -> List a
unique l =
let
incUnique : a -> List a -> List a
intUnique elem lst =
case List.member elem lst of
True -> lst
False -> elem :: lst
in
List.foldr incUnique [] l

There you go. Have fun!

Oh, still here? Let’s see what this is doing then.

So, you have a list in Elm and you want to get the unique values in it. And there is no way to write a simple imperative loop to iterate over the list. List.fold to the rescue. We…

Diogo Silva

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store