将字符串与Erlang中的列表区分开来 [英] differentiate a string from a list in Erlang

查看:423
本文介绍了将字符串与Erlang中的列表区分开来的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Erlang中有一个可打印字符的列表,它是一个字符串,但一个字符串也是一个列表,列表的所有功能都可以应用到一个字符串上。理想情况下,数据结构:字符串不存在于erlang中。


我的一部分代码需要确保某些东西不仅仅是一个列表,而是一个字符串。 (真正的字符串)。它需要分开列表,例如 [1,2,3,a,b,josh] ,来自字符串例如Muzaaya。对于这两个字符串和列表,守卫表达式 is_list / 1 将会说 true 。没有像 is_string / 1 这样的守卫,所以这意味着我需要一个代码段,确保我的数据是一个字符串。在这种情况下,字符串是仅可打印(按字母顺序排列,两种情况,上下)的列表,可能包含数字,例如Muzaaya2536 618 Joshua。我需要一个代码片段(Erlang),它将检查我,并确保该变量是一个字符串,而不仅仅是一个列表。谢谢

解决方案

您可以在模块 io_lib 中有两个功能有用的: io_lib:printable_list / 1 io_lib:printable_unicode_list / 1 ,测试参数是否分别是可打印的 latin1 unicode 字符列表。


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. Ideally, the data structure: string doesn't exist in erlang.

Part of my code needs to be sure that something is not only a list, but its a string. (A real string). It needs to separate lists e.g. [1,2,3,a,b,"josh"] , from string e.g. "Muzaaya" . The guard expression is_list/1 will say true for both strings and lists. There is no such guard as is_string/1 and so this means i need a code snippet will make sure that my data is a string. A string in this case is a list of only printable (alphabetical, both cases, upper and lower), and may contain numbers e.g "Muzaaya2536 618 Joshua". i need a code snippet (Erlang) that will check for me and ensure that the variable is a string , not just a list. thanks

解决方案

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.

这篇关于将字符串与Erlang中的列表区分开来的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆