Imitation range (L, N) in erlang

Early in the morning, playing with Erlan, I got a curious result:

-module(bucle01). -compile(export_all). for(N) when N >=0 -> lists:seq(1,N). for(L,N) when L =< N -> lists:seq(L,N); for(L,N) when L > N -> lists:reverse(for(N,L)). 

When I run the program, I see the following:

 > bucle01:for(1,10). [1,2,3,4,5,6,7,8,9,10] > bucle01:for(10,1). [10,9,8,7,6,5,4,3,2,1] >bucle01:for(7,10). [7,8,9,10] >bucle01:for(8,10). "\b\t\n" %% What that !?! >bucle01:for(10,8). "\n\t\b" %% After all it has some logic ! 

Any "Kool-Aid" for "Don't Drink Too Much" please?

+4
source share
1 answer

Erlang strings are simply lists of ASCII numbers. The Erlang shell tries to determine without metadata if your list is a list of numbers or a string looking for printable characters.

\b (backspace), \t (tab) and \n (newline) are all somewhat common ASCII characters, so the shell shows you a string instead of numbers. However, the internal structure of the list is the same.

This also applies to Erlang's frequently asked questions: Why are number lists not printed correctly?
And here are some ideas to prevent this magic: Can I turn off printed sheets of small integers as strings in the Erlang shell?

+7
source

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


All Articles