Search code examples
listerlangoperatorserlang-shell

Number in list printed as "\f"


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.


Solution

  • 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]