erlang lists:dropwhile weird result

后端 未结 1 1712
北海茫月
北海茫月 2021-01-18 09:19

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 ?          


        
相关标签:
1条回答
  • 2021-01-18 09:49

    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]).
    
    0 讨论(0)
提交回复
热议问题