differentiate a string from a list in Erlang

前端 未结 2 1817
逝去的感伤
逝去的感伤 2020-12-19 03:11

In Erlang when you have a list of printable characters, its a string, but a string is also a list of items and all functions of a list can be applied onto a string. Really,

相关标签:
2条回答
  • 2020-12-19 03:29

    You have two functions in the module io_lib which can be helpful: io_lib:printable_list/1 and io_lib:printable_unicode_list/1 which test if the argument is a list of printable latin1 or unicode characters respectively.

    0 讨论(0)
  • 2020-12-19 03:37

    using the isprint(3) definition of printable characters --

    isprint(X) when X >= 32, X < 127 -> true;
    isprint(_) -> false.
    
    is_string(List) when is_list(List) -> lists:all(fun isprint/1, List);
    is_string(_) -> false.
    

    you won't be able to use it as a guard, though.

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