C
cnb
I am trying to translate this elegant Erlang-code for finding all the
permutations of a list.
I think it is the same function as is but it doesn't work in Python.
-- is upd in Python. It works as it should.
perms([]) -> [[]];
perms(L) -> [[H|T] || H <- L, T <- perms(L--[H])].
def perms(lista):
if lista == []:
return [[]]
else:
for h in lista:
return [([h]+[t]) for t in perms(upd(lista, h))]
def upd(lista, elem, acc=tuple([])):
lista = tuple(lista)
if lista == ():
return list(acc)
if lista[0] == elem:
return list(acc + tuple(lista[1:]))
else:
return upd(lista[1:], elem, acc + tuple([lista[0]]))
permutations of a list.
I think it is the same function as is but it doesn't work in Python.
-- is upd in Python. It works as it should.
perms([]) -> [[]];
perms(L) -> [[H|T] || H <- L, T <- perms(L--[H])].
def perms(lista):
if lista == []:
return [[]]
else:
for h in lista:
return [([h]+[t]) for t in perms(upd(lista, h))]
def upd(lista, elem, acc=tuple([])):
lista = tuple(lista)
if lista == ():
return list(acc)
if lista[0] == elem:
return list(acc + tuple(lista[1:]))
else:
return upd(lista[1:], elem, acc + tuple([lista[0]]))