All permutations of a list

Richard van de Stadt stadt at cs.utwente.nl
Sun Oct 22 21:26:13 EDT 2000


Rainer Deyke wrote:
> 
> "Richard van de Stadt" <stadt at cs.utwente.nl> wrote in message
> news:39F1FB22.6E9246E7 at cs.utwente.nl...
> > Then a collegue of mine impressed me with a recursive program
> > in the functional language Miranda/Amanda:
> >
> > ins a []     = [[a]]
> > ins a (x:xs) = (a:x:xs) : map (x:) (ins a xs)
> >
> > perms []     = [[]]
> > perms (x:xs) = concat (map (ins x) (perms xs))

[...]

> def ins(a, list):
>   return [list[:i] + [a] + list[i:] for i in range(len(list) + 1)]

After having seen this, my colleague came up with this two-line solution, 
a version that doesn't need an ins-part:

perms [] = [[]]
perms xs = concat [ map (x:) (perms (xs--[x])) | x <- xs ] 

How would that be in Python? I guess it's about time I start
downloading v2.0.

Anyway, this guy's now also interested in Python :-)

Richard.



More information about the Python-list mailing list