4

I am trying to learn Erlang and had gotten no further than operators when I ran into a problem:

5> TheList = [2,4,6,8]. 
[2,4,6,8]
6> 
6> [N || N <- TheList, N rem 3 =:= 0]. 
[6]
7> 
7> TheList. 
[2,4,6,8]
8> 
8> [2*N || N <- TheList, N rem 3 =:= 0]. 
"\f"
9> 

Why do I get "\f" in the last operation? Shouldn't it be [12]? What does "\f" mean? Thanks.

2240
  • 1,547
  • 2
  • 12
  • 30
ElToro1966
  • 831
  • 1
  • 8
  • 20
  • 1
    use shell:strings(false) and get the expected result. It happens due to the fact that the "\f" is the same as [$\f] which is [12]. –  Jan 18 '16 at 15:03
  • 2
    Possible duplicate of [Erlang lists with single numbers over 8?](http://stackoverflow.com/questions/7371955/erlang-lists-with-single-numbers-over-8) – legoscia Jan 18 '16 at 15:11
  • And possibly [this](http://stackoverflow.com/q/26425435/113848), [this](http://stackoverflow.com/q/25978873/113848), [this](http://stackoverflow.com/q/20792966/113848) and [this](http://stackoverflow.com/questions/2348087/can-i-disable-printing-lists-of-small-integers-as-strings-in-erlang-shell) question. (Linking these to bring them towards the top of the [frequent Erlang questions list](http://stackoverflow.com/questions/tagged/erlang?sort=frequent&pageSize=30)) – legoscia Jan 18 '16 at 15:14

2 Answers2

4

It is explained here for example:

Erlang has no separate string type. Strings are usually represented by lists of integers (and the string module of the standard library manipulates such lists). Each integer represents the ASCII (or other character set encoding) value of the character in the string. For convenience, a string of characters enclosed in double quotes (") is equivalent to a list of the numerical values of those characters.

You can use io:format functions:

1> io:format("~w~n", [[2*N || N <- [2,4,6,8], N rem 3 =:= 0]]).
[12]

or disble this behaviour with shell:strings/1 function starting with Erlang R16B:

2> shell:strings(false).
true
3> [2*N || N <- [2,4,6,8], N rem 3 =:= 0].
[12]
P_A
  • 1,804
  • 1
  • 19
  • 33
0

As @Atomic_alarm has mentioned in the comment, it is due to erlang printing out the answer using string syntax rather than the list of integer. The default value is true, where as to see [12], you want the value set as false. The documentation for it is here.

liam_g
  • 314
  • 1
  • 5
  • 15