确定项目是否是Erlang中的字符串或列表 [英] Determining if an item is a string or a list in Erlang

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

问题描述

我正在编写一个可以使用列表或字符串作为参数的程序。如何在Erlang中以编程方式说出字符串和列表之间的区别。如下所示:

  print(List) - >列表; 
print(String) - >串。


解决方案

io_lib:printable_list可能是您正在寻找的。但是它不会处理unicode only latin-1编码。如果你需要检测unicode字符串我认为你可能是运气不好。最好的选择是伪造如下列表:{string,[$ a,$ b,$ c]}。一种构建你的类型。



在is_list(L) - > {string,L}时使用像这样的字符串(L)的构造函数。



,只需通过你的应用程序使用该键入结构。



另一方面,您可以将所有字符串视为只是列表而不是做出区分。


I am writing a program that can have either a list or a string as an argument. How can I tell the difference between a string and a list programmatically in Erlang. Something like:

print(List) -> list; 
print(String) -> string.

解决方案

io_lib:printable_list might be what you are looking for. However it doesn't handle unicode only latin-1 encodings. If you need to detect unicode strings I think you might be out of luck. The best bet is pseudo typing your lists like so: {string, [$a, $b, $c]}. Kind of a build your types.

use a constructor like so string(L) when is_list(L) -> {string, L}.

and just use that typing construct all through your app.

On the other hand you could just treat all strings as just lists and not make the distinction.

这篇关于确定项目是否是Erlang中的字符串或列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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