[erlang-questions] How to understanding recursive inside comprehension lists?
Fri Aug 23 15:37:56 CEST 2019
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"
= [ [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] ]
= [ [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 ;-)
On 23/08/2019 13:03, I Gusti Ngurah Oka Prinarjaya wrote:
> 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]) ].
> 1> lib_misc:perms("123").
> Please enlightenment
> Thank you
> erlang-questions mailing list
More information about the erlang-questions