3

Trying to generate a list through comprehension and at some point I start seeing strange character strings. Unable to explain their presence at this point (guessing the escape chars to be ASCII codes - but why?):

45> [[round(math:pow(X,2))] ++ [Y]|| X <- lists:seq(5,10), Y <- lists:seq(5,10)].                                     
[[25,5],
 [25,6],
 [25,7],
 [25,8],
 [25,9],
 [25,10],
 [36,5],
 [36,6],
 [36,7],
 "$\b","$\t","$\n",
 [49,5],
 [49,6],
 [49,7],
 "1\b","1\t","1\n",
 [64,5],
 [64,6],
 [64,7],
 "@\b","@\t","@\n",
 [81,5],
 [81,6],
 [81,7],
 "Q\b",
 [...]|...]
rks
  • 451
  • 1
  • 8
  • 16
  • See [this question](http://stackoverflow.com/questions/7371955/erlang-lists-with-single-numbers-over-8?lq=1), [this question](http://stackoverflow.com/questions/25978873/avoid-converting-numbers-to-characters-in-erlang) and [this question](http://stackoverflow.com/questions/2348087/can-i-disable-printing-lists-of-small-integers-as-strings-in-erlang-shell). – legoscia Oct 17 '14 at 13:31
  • Does this answer your question? [Can I disable printing lists of small integers as strings in Erlang shell?](https://stackoverflow.com/questions/2348087/can-i-disable-printing-lists-of-small-integers-as-strings-in-erlang-shell) – 2240 Jan 26 '21 at 12:06

1 Answers1

5

In Erlang all strings are just list of small integers (like chars in C). And shell to help you out a little tries to interpret any list as printable string. So what you get are numbers, they are just printed in a way you would not expect.

If you would like to change this behaviour you can look at this answer.

Community
  • 1
  • 1
mpm
  • 3,534
  • 23
  • 33
  • Well that's the problem because changing the global variable will affect how the strings are actually printed. But why only escape and control characters picked and other numbers appear as number and not char. – rks Oct 17 '14 at 17:56
  • 1
    Still, you would change only the way shell is printing return values; all `io:formats` would stay the same. Regarding other numbers, shell only treats as string lists with "printable integers" based on [ASCII table](http://www.asciitable.com/). So `[104,101,108,108,111]` will become `"hello"`. In your pairs you also can see that 36 is changed to `$` and 49 to `1`. And some low numbers like 7, 8, 9 are changed to bell, tab and new line. Almost all others from 0 to 32 do not have "printable" representation. – mpm Oct 17 '14 at 20:02