How to represent this clause in one line using Fun.
perms([]) -> [[]];
perms(L) -> [[H|T] || H <- L, T <- perms(L--[H])].
How to represent this clause in one line using Fun.
perms([]) -> [[]];
perms(L) -> [[H|T] || H <- L, T <- perms(L--[H])].
I believe what you are seeking is for a fun to be "self-recursive".
The fun syntax is not able to refer to itself inside the fun body, so one need to use a trick where the fun to call is a parameter. This is commonly referred to as the ycombinator.
Some example code will likely describe it better:
permutator() ->
fun
([], _F) ->
[[]];
(L, F) ->
[ [H|T] || H <- L, T <- F(L--[H], F)]
end.
do_permutate(L) ->
P = permutator(),
P(L, P).
As you can see this is quite awkward. If you just wanted to refer to the perms functions of yours, you can use the code: fun perms/1
.
I also got another answer similar to Christian.
5> Perms = fun(X) -> Fun = fun([],F) -> [[]]; (L,F) -> [[H|T] || H <- L, T <- F(L--[H],F)] end, Fun(X, Fun) end.
#Fun<erl_eval.6.13229925>
6> Perms("cat").
["cat","cta","act","atc","tca","tac"]