Taner
Taner

Reputation: 1

Haskell: How to get rid of doubled or tripled elements in a list

Objective: Finding the permutation of a list such as ["abc", "bc", "acc"]

Problem: My permutation contains doubled and tripled elements and I want to get rid of those elements.

The result that I obtained from the list above is :

[["abc","bc","acc"],["abc","bc"],["abc","acc","bc"],["abc","acc"],["abc","acc"],["abc"],["bc","abc","acc"],["bc","abc"],["bc","acc","abc"],["bc","acc"],["bc","acc"],["bc"],["acc","abc","bc"],["acc","abc"],["acc","bc","abc"],["acc","bc"],["acc","bc"],["acc"],["bc","acc"],["bc"],["acc","bc"],["acc"],["acc"],[]]

The code that I wrote in order to get rid of those doubled elements is stated below:

fct [] = []
fct (xs)
    | (head xs) `elem` xs = fct (delete (head xs) xs)
    | otherwise = fct xs 

Here I wanted to take the first element of the list and compare it with the rest of the list. Can you help me to find a solution to my problem.

Upvotes: 0

Views: 250

Answers (2)

Landei
Landei

Reputation: 54584

You can use the function nub to remove duplicates from a list.

Upvotes: 0

augustss
augustss

Reputation: 23014

Those are not permutations, because some of them don't contain all the original elements. It seems to be a combination of permutations and selections.

In ghci:

Prelude Data.List Control.Monad> concatMap permutations $ filterM (const [False, True])["abc", "bc", "acc"]
[[],["acc"],["bc"],["bc","acc"],["acc","bc"],["abc"],["abc","acc"],["acc","abc"], ["abc","bc"],["bc","abc"],["abc","bc","acc"],["bc","abc","acc"],["acc","bc","abc"],["bc","acc","abc"],["acc","abc","bc"],["abc","acc","bc"]]

Upvotes: 2

Related Questions