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

Rick Pettit rpettit@REDACTED
Tue Aug 18 21:20:17 CEST 2015


Unless the exercise requires you to output in fun/arity format, that isn’t required in order to simply sort and count the elements in the list of lists you have below.

-Rick

P.S. I’m not suggesting the following code is the best way to do this, nor that lists should be used over binaries, but here’s one way to the conversion assuming it is required for the exercise (here I’m using the sorted list of exported functions from the “inet” module as an example):

12> [lists:flatten(io_lib:format("~s/~s", [atom_to_list(Fun), integer_to_list(Arity)])) || {Fun,Arity} <- lists:keysort(1, inet:module_info(exports))].
["close/1","connect_options/2","fdopen/6","format_error/1",
 "get_rc/0","getaddr/3","getaddr/2","getaddr_tm/3",
 "getaddrs/2","getaddrs/3","getaddrs_tm/3","getfd/1",
 "gethostbyaddr/1","gethostbyaddr/2","gethostbyaddr_tm/2",
 "gethostbyname/2","gethostbyname/3","gethostbyname/1",
 "gethostbyname_self/2","gethostbyname_string/2",
 "gethostbyname_tm/3","gethostname/1","gethostname/0",
 "getif/0","getif/1","getifaddrs/1","getifaddrs/0",
 "getiflist/1",
 [...]|…]


suggesting
Now if you are just going to output the resulting list using something like io:format/2, then you don’t even need to lists:flatten/1:

15> io:format([io_lib:format("~s/~s\n", [atom_to_list(Fun), integer_to_list(Arity)]) || {Fun,Arity} <- lists:keysort(1, inet:module_info(exports))]).
close/1
connect_options/2
fdopen/6
format_error/1
get_rc/0
getaddr/3
getaddr/2
getaddr_tm/3
getaddrs/2
getaddrs/3
getaddrs_tm/3
getfd/1
gethostbyaddr/1
gethostbyaddr/2
gethostbyaddr_tm/2
gethostbyname/2
gethostbyname/3
gethostbyname/1
gethostbyname_self/2
gethostbyname_string/2
gethostbyname_tm/3
gethostname/1
…
...


> On Aug 18, 2015, at 1:13 PM, 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 ]
> 
> I tried with lists:flatten( name_of_list) but that one do not give me the right output.
> 
> I need the nice output because I have to sort them and then count them.
> 
> 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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150818/33dbf8c6/attachment.htm>


More information about the erlang-questions mailing list