# [erlang-questions] How to understanding recursive inside comprehension lists?

Fred Youhanaie fly@REDACTED
Fri Aug 23 15:37:56 CEST 2019

```Hi

Try expanding it by hand with shorter lists, i.e. perms("1") and perms("12")

To start you off, and using [a, b, c], instead of "123"

perms([a])
= [ [H|T] || H <- [a], T <- perms([a]--H) ]
= [ [a | T] T <- perms([a]--[a])]  %% replace H, one item
= [ [a | T] T <- perms([])] ]
= [ [a | T] T <- [[]] ] %% replace T
= [ [a | []] ]
= [ [a] ]

perms([a, b])
= [ [H|T] || H <- [a, b], T <- perms([a, b]--H) ]
= [ [a|T] || T <- perms([b]),
[b|T] || T <- perms([a]) ] %% replace H, two items
= [ [a|T] || T <- [[b]],
[b|T] || T <- [[a]] ]
= ...

I'll let you complete the rest yourself ;-)

HTH

Cheers,
Fred

On 23/08/2019 13:03, I Gusti Ngurah Oka Prinarjaya wrote:
> Hi,
>
> Now I read Joe's book titled Programming Erlang 2nd Edition. I practice some functions such as for/3, quicksort/1, pythag/1, and perms/1, and perms/1 is the function that hard to understand.
>
> I understand comprehension lists, I fully understand for/3, I fully understand quicksort/1, pythag/1. But it's really hard for me to understand perms/1. Please teach me how to read and understand this
> perms/1 function.
>
> perms([]) -> [[]];
> perms(List) -> [ [H|T] || H <- List, T <- perms(List--[H]) ].
>
> Output:
> 1> lib_misc:perms("123").
> n
>
>
> Thank you
>
>
>
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>

```