erlang

pattern match in Erlang

ぃ、小莉子 提交于 2019-12-24 17:46:25
问题 I am trying to learn some Erlang while I got stuck on these several Erlang pattern matching problems. Given the module here: -module(p1). -export([f2/1]). f2([A1, A2 | A1]) -> {A2, A1}; f2([A, true | B]) -> {A, B}; f2([A1, A2 | _]) -> {A1,A2}; f2([_|B]) -> [B]; f2([A]) -> {A}; f2(_) -> nothing_matched. and when I execute p1:f2([x]) , I received an empty list which is [] . I thought it matches the 5th clause? Is that a literal can also be an atom? When I execute p1:f2([[a],[b], a]) , the

What is the best way to include a header file (wx.hrl for example) in a release using rebar

China☆狼群 提交于 2019-12-24 14:23:56
问题 I am preparing a release for an application using rebar, and I wonder what is the usual way to include header file from standard library. In my case, it is the wx.hrl file, which is included with its full absolute path in my code. I guess that it is not the right way :o) 回答1: -include_lib("wx/include/wx.hrl"). This makes the preprocessor look for the latest version of the wx application in the code path. See this question for more details. 来源: https://stackoverflow.com/questions/18363244/what

converting string to tuple in erlang .any pointer?

烈酒焚心 提交于 2019-12-24 14:06:02
问题 how to convert string to tuple in erlang? f.e A="{"hi","how"}" and i want it to converted into B={"hi","how"}. when i call function list_to_tuple(A) it gives output as:-{123,60,60,34,106,105,100,34,62,62,44,34,104,105,34,125} rather than {"hi","how"} 回答1: You should use erl_scan module to tokenize the string and erl_parse to convert the tokens to a erlang term. % Note the '.' at the end of the expression inside string. % The string has to be a valid expression terminated by a '.'. 1> Str = "{

Erlang client-server error

≡放荡痞女 提交于 2019-12-24 13:22:21
问题 I'm reading the book Programming Erlang and on page 253 there is an example for running a client/server connection. The code below suggests to run the following commands in two separate erlang console windows from which I get the following error. 2> socket_examples:nano_client_eval("list_to_tuple([2+3*4,10+20])"). ** exception error: no match of right hand side value {error,econnrefused} in function socket_examples:nano_client_eval/1 (socket_examples.erl, line 28) the command for the server

How do I modify the code search path for a remote Erlang node?

社会主义新天地 提交于 2019-12-24 11:37:35
问题 I'm connected to an Erlang node with -remsh . How do I modify the code path, in order to load a library that wasn't packaged into my release? 回答1: All necessary functions to manipulate code loading, path... are in the code module (see doc at erlang otp code module). You could add the system paths to the list by doing the following: [code:add_pathz(P) || P <- filelib:wildcard("/usr/lib/erlang/lib/*/ebin")]. 回答2: After compiling some test code and connecting to a running node I was able to make

Erlang convert ejabberd XML structure to string

99封情书 提交于 2019-12-24 11:28:35
问题 The Erlang representation of my XML message is like the below: {xmlel,<<"message">>,[{<<"xml:lang">>,<<"en">>},{<<"type">>,<<"chat">>},{<<"to">>,<<"x">>},{<<"id">>,<<"aaf6a">>}],[{xmlcdata,<<"\n">>},{xmlel,<<"body">>,[],[{xmlcdata,<<"wewe">>}]},{xmlcdata,<<"\n">>},{xmlel,<<"active">>,[{<<"xmlns">>,<<"http://jabber.org/protocol/chatstates">>}],[]},{xmlcdata,<<"\n">>},{xmlel,<<"time">>,[{<<"xmlns">>,<<"urn:server-time">>},{<<"stamp">>,"2015-06-23T22:48:24Z"}],[]}]} How can I convert it into

Riak mapreduce error

瘦欲@ 提交于 2019-12-24 09:54:56
问题 I am using the basic sample Riak sample provided. 5> ReFun = fun(O, _, Re) -> case re:run(riak_object:get_value(O), Re, [global]) of 5> {match, Matches} -> [{riak_object:key(O), length(Matches)}]; 5> nomatch -> [{riak_object:key(O), 0}] 5> end end. #Fun<erl_eval.18.50752066> 6> {ok, Re} = re:compile("caremad"). {ok,{re_pattern,0,0,0, <<69,82,67,80,85,0,0,0,0,0,0,0,81,0,0,0,255,255,255,255, 255,255,...>>}} 7> 7> {ok, Riak} = riakc_pb_socket:start_link("127.0.0.1", 8087). {ok,<0.44.0>} 8> riakc

Erlang compiler error

有些话、适合烂在心里 提交于 2019-12-24 09:21:53
问题 I have the following code loop(Data) -> receive {Key, Value} -> [{Key, Value}|Data]; {Key} -> member(Key, Data); 14 loop(Data); stop -> io:format("server_stopped"), ok end . and I get the following error (I put the line number 14 in the code) ./dist_erlang.erl:14: syntax error before: ';' ./dist_erlang.erl:2: function loop/1 undefined ./dist_erlang.erl:28: Warning: function member/2 is unused I am not sure what the syntax problem is with the above code. I have a method called member which is

erlang mysql result to xml

左心房为你撑大大i 提交于 2019-12-24 08:32:36
问题 I have an output : MysqlResult = {selected,["id","first_name","last_name"], [{1,"Matt","Williamson"}, {2,"Matt","Williamson2"}]} how to make it look like : XML = " <result id='1'> <first_name>Matt</first_name> <last_name>Williamson</last_name> </result> <result id='2'> <first_name>Matt</first_name> <last_name>Williamson2</last_name> </result>" I am looking for a smart way for placing it into IQ ( ejabberd ) IQ#iq{type = result, sub_el = [{xmlelement, "result", [{"xmlns", ?NS_NAMES}], [

Erlang: discrepancy of memory usage figures

元气小坏坏 提交于 2019-12-24 08:25:20
问题 When I run my WebSocket test, I found the following interesting memory usage results: Server stated, no connection [{total,573263528}, {processes,17375688}, {processes_used,17360240}, {system,555887840}, {atom,472297}, {atom_used,451576}, {binary,28944}, {code,3774097}, {ets,271016}] 44 processes, System:705M, Erlang Residence:519M 100K Connections [{total,762564512}, {processes,130105104}, {processes_used,130089656}, {system,632459408}, {atom,476337}, {atom_used,456484}, {binary,50160},