Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unique elements in a haskell list

Tags:

list

haskell

okay, this is probably going to be in the prelude, but: is there a standard library function for finding the unique elements in a list? my (re)implementation, for clarification, is:

has :: (Eq a) => [a] -> a -> Bool has [] _ = False has (x:xs) a   | x == a    = True   | otherwise = has xs a  unique :: (Eq a) => [a] -> [a] unique [] = [] unique (x:xs)   | has xs x  = unique xs   | otherwise = x : unique xs 
like image 912
muhmuhten Avatar asked Jun 23 '10 01:06

muhmuhten


People also ask

How do you check for duplicates in a list Haskell?

if it is just about knowing if the list contains duplicates you can use the nub function in Data. List like so: hasDup l = nub l == l (nub removes all duplicates... so this will be true only if there are no duplicates in the list.)

Can Haskell lists have different types?

Haskell also incorporates polymorphic types---types that are universally quantified in some way over all types. Polymorphic type expressions essentially describe families of types. For example, (forall a)[a] is the family of types consisting of, for every type a, the type of lists of a.


1 Answers

I searched for (Eq a) => [a] -> [a] on Hoogle.

First result was nub (remove duplicate elements from a list).

Hoogle is awesome.

like image 117
Artelius Avatar answered Oct 18 '22 03:10

Artelius