[Haskell-cafe] I need a hint in list processing

Richard O'Keefe ok at cs.otago.ac.nz
Sun Jun 14 23:38:44 EDT 2009


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.




More information about the Haskell-Cafe mailing list