Erlang lists: weird result

Can someone please help me understand what is happening 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 
+6
source share
1 answer

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

You can always print the actual integer list using

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

Source: https://habr.com/ru/post/912592/


All Articles