2

I learn Erlang and list comprehensions now and have discovered a weird (as for me) issue. So I have a simple list comprehension with a simple formula and without filter:

gen_list(List)->
[N*N || N <- List].

The output is correct as I expected: gen_list([2,3,4]). [4,9,16] Then I do this:

 gen_list(List)->
[N*N*N*N || N <- List].

And the output is correct again: gen_list([2,3,4]). [16,81,256]. But when I define formula as:gen_list(List)-> [N*N*N || N <- List]. I got next output:gen_list([2,3,4]). "\b\e@".

What is this:"\b\e@"?? Why I got it only when I have three N? I can even write the formula like this:N*N*N*N*N*N*N*N, and the output again will be as I expected. But with three N I always got such a weird result. Can someone explain this for me?

I use ArchLinux and GNU Emacs.

MainstreamDeveloper00
  • 8,436
  • 15
  • 56
  • 102

2 Answers2

3

"\b\e@" is Erlang's way of representing a list of integers when those integers fall in the ASCII-displayable range.

To make sure this is a list, pattern match it like so:

1> [A, B, C] = "\b\e@".
"\b\e@"
2> {A, B, C}.
{8,27,64}
fenollp
  • 2,406
  • 1
  • 15
  • 14
  • Thanks for a quick response! So you mean instead of list I should use a `tuple` for this, am i right? – MainstreamDeveloper00 Sep 23 '14 at 21:44
  • You could also look into [this question](http://stackoverflow.com/questions/25978873/avoid-converting-numbers-to-characters-in-erlang) to change the way shell display those sudo-stings. – mpm Sep 23 '14 at 23:19
  • @HarryDeveloper1212: Don't use tuples. Just bind the result to a variable and display it like this: io:format("~w", [Result]). "~w" will display the list as you expect. – tkowal Sep 24 '14 at 06:30
1

In Erlang, string is actually a list, so the "\b\e@" is the same as [8,27,64]. Is's just because these numbers are ascii-printable, the erlang shell will print it as strings.

1>[8,27,64].
"\b\e\@"

you can view the ascii code of these symbols with a $ sign before it.

2> $\b.
8
3> $\e.
27
4> $\@.
64
wudeng
  • 795
  • 5
  • 8