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

Joe Armstrong erlang@REDACTED
Wed Aug 19 17:22:07 CEST 2015

On Wed, Aug 19, 2015 at 1:10 PM, Roelof Wobben <r.wobben@REDACTED> wrote:
> Op 19-8-2015 om 4:57 schreef Richard A. O'Keefe:
>> On 19/08/2015, at 6:13 am, Roelof Wobben <r.wobben@REDACTED> 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.
> Still my question stands. How do I get a nice list of the ourcome of the
> exports of the functions ot more then 1 module.

Deep breath - long answer coming up ...

Your original question said:

>>> Is it possible to convert this to for example [get_rc/0, peername/1,
>>> peernames/? , module/2 ]

The answer is NO since [get_rc/0, peername/1, ...] etc is not a valid
Erlang term
(this is possibly badly explained, something like [foo/1,bar/2] is a
syntactic form
that can occur in Erlang programs (for example -import([foo/1,bar/2])) but it
is not in itself a valid Erlang term. An Erlang term is something that an Erlang
function can return. Terms are very simple, they are atomic (things
like integers,
floats, atoms, pids etc) or they are composed using a tuple or list constructor.

A function *cannot* return [foo/1, bar/2] since it is not a valid Erlang term.

So you have to make a design decision do you want to return

["foo/1", "bar/1"] which *is* an Erlang term, or "foo/1, bar/1" which is
also an Erlang term.

Let's assume the former and write module with a single test case:


test() ->
    ["bar/1", "foo/1"] = f1([{bar,1}, {foo,1}]),

Now we have to write f1

f1(L) ->
    [atom_to_list(Name) ++ "/" ++ integer_to_list(Int) ||
{Name, Int} <- L].

Add this to the module compile and run:

    $ erl
    Eshell V6.2  (abort with ^G)
    1> c(t1).
    2> t1:test().

Now *extend the test case*

test() ->
    ["bar/1", "foo/1"] = f1([{bar,1}, {foo,1}]),
    ["bar/1", "foo/1"] = f1([{foo,1}, {bar,1}]),

Compile and run

    3> c(t1).
    4> t1:test().
   ** exception error: no match of right hand side value ["foo/1","bar/1"]
       in function  t1:test/0 (t1.erl, line 6)

perhaps we need to sort the inputs?

This is called test driven development (TDD) write a test case, make it
work, write the next test case, and so on.

The *problem* with you original question was that it was imprecise.
Words like "nice list" don't really mean much.

In this forum you'll often see questions that remain unanswered. One
reason for this is that often the person who write the question thinks
the question is precise but in fact it's so vague that nobody can
answer, and then indeed nobody does answer.

On other occasions you see very long answers and threads arising from
vague questions, firstly because the question provides an opportunity
to explain some things that might not be obvious, secondly because
Mr Google will remember the answer forever, so hopefully one day
some person will ask Mr Google the same of similar question and find
the answer buried in the archives of this mailing list.

I'd hazard a guess that a large fraction of all beginners Erlang
questions can be found by Googling this list - you'll even find the
best ever explanation of rounding errors in performing floating point
operations written by one of our Antipodean friends ...



> I now have a list of list of tuples where every list is the outcome of 1
> module.
> Roelof
> ---
> Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
> https://www.avast.com/antivirus
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions

More information about the erlang-questions mailing list