[erlang-questions] How can I make a list of this

Richard A. O'Keefe <>
Wed Aug 19 04:57:26 CEST 2015


On 19/08/2015, at 6:13 am, Roelof Wobben <> wrote:

> Hello,
> 
> Im still trying to solve exercise 2 of chapter 8 of Programming erlang.
> 
> Now I succeed in finding all functions that are exported by a module.
> 
> I see now this output :
> 
> [{get_rc,0}, {close,1}, {peername,1}, {setpeername,2}, {peernames,...}, {...}|...], [{module,2},{module_info,0},{module_info,1}], [{new,0},{is_set,1},{size,...},{...}|...], [{server,3},{interfaces,...},{...}|...], [{init_it,...},{...}|...], [{...}|...], [...]|...]
> 
> So I have list which are containing more list which contains one of more tuples
> 
> Is it possible to convert this to for example [get_rc/0, peername/1, peernames/? , module/2 ]

There is no get_rc/0 data structure.
When you have F/N in a declaration, it is converted to {F,N}.
[{get_rc,0}, {peername,1}, ...] is as close as you are going to get.

> I need the nice output because I have to sort them and then count them.

No you don't.
You can sort a list of tuples.
1> lists:sort([{c,3},{a,2},{b,4},{a,1},{c,2}]).
[{a,1},{a,2},{b,4},{c,2},{c,3}]

Given a sorted list,

sorted_list_unique_count(L) ->
   sorted_list_unique_count(L, 0).

sorted_list_unique_count([X|Xs=[X|_]], N) -> % duplicate
    sorted_list_unique_count(Xs, N);
sorted_list_unique_count([_|Xs], N) ->       % non-duplicate
    sorted_list_unique_count(Xs, N+1);
sorted_list_unique_count([], N) ->
    N.

You can print such a list prettily if you want:

format_functor_list(Fs) ->
    format_functor_list(Fs, "").

format_functor_list([{F,N}|Fs], Sep) ->
    io:format("~s~w/~w", [Sep, F,N]),
    format_functor_list(Fs, ", ");
format_functor_list([], _) ->
    ok.





More information about the erlang-questions mailing list