2017-01-16 4 views

Antwort

1
get_pos(L,E) -> 
    {R,_} = lists:foldl(fun(X,{Acc,Pos}) when X == E -> {[Pos|Acc],Pos+1}; (_,{Acc,Pos}) -> {Acc,Pos+1} end, {[],1},L), 
    R. 
1

Kühl und Phantasie:

get_pos(L, E) -> 
    [ P || 
     {X, P} <- lists:zip(L, lists:seq(1, length(L))), 
     X =:= E ]. 

Alt und langweilig:

get_pos(L, E) -> 
    F = fun(X, {Acc, Pos}) -> 
      A = if X =:= E -> [Pos|Acc]; true -> Acc end, 
      {A, Pos+1} 
     end, 
    {R, _} = lists:foldl(F, {[], 1}, L), 
    R. 

Schnell und effizient:

get_pos(L, E) -> get_pos(L, E, 1). 

get_pos([], _, _) -> []; 
get_pos([H|T], H, Pos) -> 
    [Pos | get_pos(T, H, Pos+1)]; 
get_pos([_|T], E, Pos) -> 
    get_pos(T, E, Pos+1). 
Verwandte Themen