Erlang - Can one use Lists:append for adding an element to a string? -


here function parses addition equation.

expr_print({num,x}) -> x;  expr_print({plus,x,y})->              lists:append("(",expr_print(x),"+",expr_print(y),")"). 

once executed in terminal should (but doesn't @ moment):

>math_erlang: expr_print({plus,{num,5},{num,7}}). >(5+7) 

actually 1 that, not work way wish in x in {num, x} number , not string representation of number.

strings in erlang lists of numbers. , if numbers in wright range can printed string. should able find detail explenation here. first thing wold make sure call expr_print({num, 3}). return "3" , not 3. should able find solution here.

second thing lists:append takes 1 argument, list of list. code this

expra_print({num,x}) ->     lists:flatten(io_lib:format("~p", [x])); expr_print({plus,x,y})->      lists:append(["(", expr_print(x),"+",expr_print(y), ")"]). 

and should produce nice flat string/list.

another thing might not need flat list. if planning writing file, or sending on tcp might want use iolist, easier create (you drop append , flatten calls) , faster.


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -

php - $params->set Array between square bracket -