Erlang ++运算符。句法糖,还是单独操作? [英] Erlang ++ operator. Syntactic sugar, or separate operation?

查看:102
本文介绍了Erlang ++运算符。句法糖,还是单独操作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简单的问题是:Erlang的 ++ 运算符简单的语法糖为列表:concat 或是不同的操作共?我已经尝试搜索这个,但Google不可能使用++并获得任何有用的东西。恳求要求帮助,但与答案的参考将是美好的。谢谢。

Easy question: is Erlang's ++ operator simply syntactic sugar for lists:concat or is it a different operation altogether? I've tried searching this, but it's impossible to Google for "++" and get anything useful. Hate to be demanding while asking for help, but a reference with the answer would be wonderful. Thanks.

-tjw

推荐答案

这就是列表:concat / 1 在stdlib / lists模块中实现:

This is how the lists:concat/1 is implemented in the stdlib/lists module:

concat(List) ->
    flatmap(fun thing_to_list/1, List).

其中:

flatmap(F, [Hd|Tail]) ->
    F(Hd) ++ flatmap(F, Tail);
flatmap(F, []) when is_function(F, 1) -> [].

和:

thing_to_list(X) when is_integer(X) -> integer_to_list(X);
thing_to_list(X) when is_float(X)   -> float_to_list(X);
thing_to_list(X) when is_atom(X)    -> atom_to_list(X);
thing_to_list(X) when is_list(X)    -> X.   %Assumed to be a string

所以,列表:concat / 1 实际上使用'++'运算符。

So, lists:concat/1 actually uses the '++' operator.

这篇关于Erlang ++运算符。句法糖,还是单独操作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆