List combination function?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

List combination function?

Ian Duncan
Hello all,

I'm trying to build a function that takes a string such as "123" and
gives me permutations including permutations with lesser list lengths.
I'm not sure how to phrase it, but here is what the output could look like:

foo "123" => ["123","213","321","231","312","132", "12", "13", "21",
"23", "31", "32", "1", "2", "3", ""]

The ordering doesn't matter, and that null list at the end doesn't
particularly matter, but I don't really know the mathematical phrasing
of what I'm asking for. I'm trying to build a scrabble helper that can
find the optimal score given a set of letters to work with.

Thanks for your help,

Ian Duncan
Reply | Threaded
Open this post in threaded view
|

List combination function?

Alexander Dunlap
On Sat, Aug 15, 2009 at 5:12 PM, Ian Duncan<[hidden email]> wrote:

> Hello all,
>
> I'm trying to build a function that takes a string such as "123" and gives
> me permutations including permutations with lesser list lengths. I'm not
> sure how to phrase it, but here is what the output could look like:
>
> foo "123" => ["123","213","321","231","312","132", "12", "13", "21", "23",
> "31", "32", "1", "2", "3", ""]
>
> The ordering doesn't matter, and that null list at the end doesn't
> particularly matter, but I don't really know the mathematical phrasing of
> what I'm asking for. I'm trying to build a scrabble helper that can find the
> optimal score given a set of letters to work with.
>
> Thanks for your help,
>
> Ian Duncan
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://www.haskell.org/mailman/listinfo/beginners
>

Import Data.List, then the function you want is

concatMap permutations . subsequences

subsequences returns a list of all subsequences of the original list in order.

permutations returns a list of all possible orderings of the list.

concatMap applies permutations to every member of the subsequence list
and then flattens the list down to a single list again.

Hope that helps,
Alex