{1,2,{3,4}} (就像 Haskell 中的read-6ren">
gpt4 book ai didi

erlang - "string_to_term/1"?

转载 作者:行者123 更新时间:2023-12-02 11:50:25 25 4
gpt4 key购买 nike

Erlang 中是否有具有这种行为的函数?:

a_function_id_like_to_have("{1,2,{3,4}}") % => {1,2,{3,4}}

(就像 Haskell 中的read)

最佳答案

看一下this presentation的幻灯片25 .

它利用 erl_eval , erl_parse ,和erl_scan .

1> {ok, Tokens, _} = erl_scan:string("{1,2,{3,4}}.").
{ok,[{'{',1},
{integer,1,1},
{',',1},
{integer,1,2},
{',',1},
{'{',1},
{integer,1,3},
{',',1},
{integer,1,4},
{'}',1},
{'}',1},
{dot,1}],
1}
2> {ok, Abstract} = erl_parse:parse_exprs(Tokens).
{ok,[{tuple,1,
[{integer,1,1},
{integer,1,2},
{tuple,1,[{integer,1,3},{integer,1,4}]}]}]}
3> Bindings = erl_eval:new_bindings().
[]
4> {value, Value, _} = erl_eval:exprs(Abstract, Bindings).
{value,{1,2,{3,4}},[]}
5> erlang:display(Value).
{1,2,{3,4}}
true

关于erlang - "string_to_term/1"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15444909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com