differentiate a string from a list in Erlang

前端 未结 2 1816
逝去的感伤
逝去的感伤 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: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.

提交回复
热议问题