I need a hint in list processing

Hi all If I have a number of list example list1 = [2,3] list2 = [1,2] list3 = [2,3,4] list4 = [1,2,3] I want to create a list from the list above with n elements, non-repeating and each elements index represents 1 of the elements from the corresponding list so for the above input I would get. a = [3,2,4,1] ofcourse there may be several set that will satisfy the problem, so a list of list that satisfies would be good. How do I do this in haskell? or is there a code snippet that seems to work similarly? thanks fernan -- http://www.fernski.com

Fernan Bolando wrote:
Hi all
If I have a number of list example list1 = [2,3] list2 = [1,2] list3 = [2,3,4] list4 = [1,2,3]
I want to create a list from the list above with n elements, non-repeating and each elements index represents 1 of the elements from the corresponding list so for the above input I would get.
a = [3,2,4,1]
ofcourse there may be several set that will satisfy the problem, so a list of list that satisfies would be good.
How do I do this in haskell? or is there a code snippet that seems to work similarly?
Well you could simply concatenate all the lists using the (++) operator and then use Data.List.nub: http://haskell.org/ghc/docs/latest/html/libraries/base/Data-List.html#v:nub to remove duplicates. Erik -- ---------------------------------------------------------------------- Erik de Castro Lopo http://www.mega-nerd.com/

On Sun, Jun 14, 2009 at 4:13 PM, Erik de Castro
Lopo
Fernan Bolando wrote:
Hi all
If I have a number of list example list1 = [2,3] list2 = [1,2] list3 = [2,3,4] list4 = [1,2,3]
I want to create a list from the list above with n elements, non-repeating and each elements index represents 1 of the elements from the corresponding list so for the above input I would get.
a = [3,2,4,1]
ofcourse there may be several set that will satisfy the problem, so a list of list that satisfies would be good.
How do I do this in haskell? or is there a code snippet that seems to work similarly?
Well you could simply concatenate all the lists using the (++) operator and then use Data.List.nub:
http://haskell.org/ghc/docs/latest/html/libraries/base/Data-List.html#v:nub
to remove duplicates.
Using Data.List.nub Data.List> nub [2,3,1,2,2,3,4,1,2,3] [2,3,1,4] that is not exactly what I want. list1 only has 2,3 has elements and list2 only has elements 1,2...this means the first element should only be 2 or 3 and the second element should only have 1 or 2...etc fernan -- http://www.fernski.com

ghci> map reverse $ foldM (\answer list -> [x:answer | x <- list, not $ x `elem` answer]) [] [[2,3], [1,2], [2,3,4], [1,2,3]] [[2,1,4,3],[3,1,4,2],[3,2,4,1]] On 14 Jun 2009, at 12:06, Fernan Bolando wrote:
Hi all
If I have a number of list example list1 = [2,3] list2 = [1,2] list3 = [2,3,4] list4 = [1,2,3]
I want to create a list from the list above with n elements, non-repeating and each elements index represents 1 of the elements from the corresponding list so for the above input I would get.
a = [3,2,4,1]
ofcourse there may be several set that will satisfy the problem, so a list of list that satisfies would be good.
How do I do this in haskell? or is there a code snippet that seems to work similarly?
thanks fernan
-- http://www.fernski.com _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe

On Sun, Jun 14, 2009 at 4:51 PM, Miguel Mitrofanov
ghci> map reverse $ foldM (\answer list -> [x:answer | x <- list, not $ x `elem` answer]) [] [[2,3], [1,2], [2,3,4], [1,2,3]] [[2,1,4,3],[3,1,4,2],[3,2,4,1]]
On 14 Jun 2009, at 12:06, Fernan Bolando wrote:
Hi all
If I have a number of list example list1 = [2,3] list2 = [1,2] list3 = [2,3,4] list4 = [1,2,3]
I want to create a list from the list above with n elements, non-repeating and each elements index represents 1 of the elements from the corresponding list so for the above input I would get.
a = [3,2,4,1]
ofcourse there may be several set that will satisfy the problem, so a list of list that satisfies would be good.
How do I do this in haskell? or is there a code snippet that seems to work similarly?
thanks fernan
-- http://www.fernski.com _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
thanks miguel this is the one that I needed. thanks also to everyone fernan -- http://www.fernski.com

On 14 Jun 2009, at 8:06 pm, Fernan Bolando wrote:
Hi all
If I have a number of list example list1 = [2,3] list2 = [1,2] list3 = [2,3,4] list4 = [1,2,3]
I want to create a list from the list above with n elements, non-repeating and each elements index represents 1 of the elements from the corresponding list so for the above input I would get.
a = [3,2,4,1]
I have been staring at this off and on all day, and I haven't the faintest idea what you want. What is "n". What is it that doesn't repeat? How does the index of an element represent 1 element? Which list corresponds to what? I'm beginning to suspect that what you want is a "choice" function: f [s1,...,sn] = [x1,...,xn] when each xi is an element of the corresponding si and no two xs are the same. Instead of finding one answer, let's find them all. all_choices :: Eq a => [[a]] -> [[a]] all_choices [] = [[]] all_choices (set:sets) = [x:xs | xs <- all_choices sets, x <- set, not(x `elem` xs)] The test case all_choices [[2,3], [1,2], [2,3,4], [1,2,3]] has the answer [[3,2,4,1], [3,1,4,2], [2,1,4,3]] and you probably want to use it something like case all_choices sets of [] -> there are no such choices (first_choice:_) -> first_choice is one such choice For inputs like [[1,2],[2,1],[1]] there is of course no such choice function.

nub . concat ? Richard O'Keefe wrote:
On 14 Jun 2009, at 8:06 pm, Fernan Bolando wrote:
Hi all
If I have a number of list example list1 = [2,3] list2 = [1,2] list3 = [2,3,4] list4 = [1,2,3]
I want to create a list from the list above with n elements, non-repeating and each elements index represents 1 of the elements from the corresponding list so for the above input I would get.
a = [3,2,4,1]
I have been staring at this off and on all day, and I haven't the faintest idea what you want.
What is "n". What is it that doesn't repeat? How does the index of an element represent 1 element? Which list corresponds to what?
I'm beginning to suspect that what you want is a "choice" function: f [s1,...,sn] = [x1,...,xn] when each xi is an element of the corresponding si and no two xs are the same.
Instead of finding one answer, let's find them all.
all_choices :: Eq a => [[a]] -> [[a]] all_choices [] = [[]] all_choices (set:sets) = [x:xs | xs <- all_choices sets, x <- set, not(x `elem` xs)]
The test case
all_choices [[2,3], [1,2], [2,3,4], [1,2,3]]
has the answer
[[3,2,4,1], [3,1,4,2], [2,1,4,3]]
and you probably want to use it something like
case all_choices sets of [] -> there are no such choices (first_choice:_) -> first_choice is one such choice
For inputs like [[1,2],[2,1],[1]] there is of course no such choice function.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Tony Morris http://tmorris.net/

On Sun, Jun 14, 2009 at 2:06 AM, Fernan Bolando
Hi all
If I have a number of list example list1 = [2,3] list2 = [1,2] list3 = [2,3,4] list4 = [1,2,3]
I want to create a list from the list above with n elements, non-repeating and each elements index represents 1 of the elements from the corresponding list so for the above input I would get.
a = [3,2,4,1]
ofcourse there may be several set that will satisfy the problem, so a list of list that satisfies would be good.
I will rephrase the problem, since some folks seem to be having a hard time understanding it. You want a list of four elements. The first element comes from list1, the second from list2, .... And there should be no duplicates in the solution. To start you off, here is a selection function, which computes all such lists, but does not remove duplicates. select :: [[a]] -> [[a]] select [] = [[]] select (xs:xss) = [ x:rest | y <- xs; rest <- select xss ] Filtering out the duplicate-free lists should not be a problem. Note: this is not an efficient solution, it is just to get you started. Making it efficient will require understanding and modifying the above select function, which ought to be an educational experience :-) Luke
How do I do this in haskell? or is there a code snippet that seems to work similarly?
thanks fernan
-- http://www.fernski.com _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
participants (6)
-
Erik de Castro Lopo
-
Fernan Bolando
-
Luke Palmer
-
Miguel Mitrofanov
-
Richard O'Keefe
-
Tony Morris