Erlang lists with single numbers over 8?

后端 未结 3 749
说谎
说谎 2020-12-02 00:45

in some weird way all the numbers over 8, single, in a list becomes some kind of ascii?

[8] -> [\"\\b\"]

Please tr

相关标签:
3条回答
  • 2020-12-02 01:25

    print it with ~w instead of ~p, and your issue should go away.

    ~p tries to interpret the elements in the list as ascii. ~w does not

    0 讨论(0)
  • 2020-12-02 01:36

    String is not a data type in Erlang, it's just a list of integers. But Erlang shell try to display lists as strings if possible:

    1> S = [65, 66, 67, 68, 69, 70].
    "ABCDEF"
    2> S = "ABCDEF".
    "ABCDEF"
    3> io:write(S).
    [65,66,67,68,69,70]ok
    4> [65, 66].
    "AB"
    5> [65, 66, 1].
    [65,66,1]
    
    0 讨论(0)
  • 2020-12-02 01:39

    From documentation: http://www.erlang.org/doc/reference_manual/data_types.html

    2.11 String

    Strings are enclosed in double quotes ("), but is not a data type in Erlang. Instead a string "hello" is shorthand for the list [$h,$e,$l,$l,$o], that is [104,101,108,108,111].

    Two adjacent string literals are concatenated into one. This is done at compile-time and does not incur any runtime overhead. Example:

    "string" "42"

    is equivalent to

    "string42"

    0 讨论(0)
提交回复
热议问题