[erlang-questions] List comprehensions

Liam Clarke <>
Thu Oct 12 09:02:50 CEST 2006


    Hi all,

Just reading the manual on list comprehensions, and looking at the 
comprehension to return all permutations of a sequence:

perms([]) ->[[]];
perms(L) ->
    [[H|T] || H <- L, T <- perms(L--[H])].

I'm having trouble with understanding what's happening. I assume it's 
evaluated left to right from the ||, and I'm sort of grasping the 
general interplay of the generators and the recursion, but I'm also 
missing it at the same point.

I think I get how dual generators work now -
[[H|[T]] || H <- [a,b,c],T <- [1,2,3]] 

will yield

[[a,1],[a,2],[a,3],[b,1],[b,2],[b,3],[c,1],[c,2],[c,3]]

How could I rewrite the permutations comprehension as a function so I 
can sprinkle it with io:format()s so as to understand what's happening 
better?

Regards,

Liam Clarke






More information about the erlang-questions mailing list