[Haskell-beginners] List combination function?

Alexander Dunlap alexander.dunlap at gmail.com
Sat Aug 15 20:16:45 EDT 2009


On Sat, Aug 15, 2009 at 5:12 PM, Ian Duncan<iand675 at gmail.com> 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
> Beginners at haskell.org
> 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


More information about the Beginners mailing list