erlang lists:dropwhile weird result

你说的曾经没有我的故事 提交于 2019-12-19 18:22:21

问题


can someone please help me understand what's going on here

lists:dropwhile(fun(X) -> X < 8 end, lists:seq(1,10)).

"\b\t\n" % ??? what is this ? why not [8,9,10]

lists:dropwhile(fun(X) -> X < 7 end, lists:seq(1,10)).  

[7,8,9,10] % this is correct

回答1:


Your results are actually correct in both cases. The unexpected string in the first case is due to the fact that in Erlang strings are just lists of integers. Therefore, Erlang chooses to interpret your first list as a string, since it contains only printable ASCII codes. In the second case the list contains the code 7, which is not printable, so Erlang is forced to interpret it as an integer list.

You can always print the actual integer list by using

MyList = lists:dropwhile(fun(X) -> X < 8 end, lists:seq(1,10)),
io:format("~w", [MyList]).


来源:https://stackoverflow.com/questions/10043141/erlang-listsdropwhile-weird-result

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!