在Erlang中将此转换为"helloworld"
到["h","e","l","l","o","w","o","r","l","d"]
的最常用方法是什么?
答案 0 :(得分:3)
字符串是字符列表
1> [$h, $e, $l, $l, $o, $w, $o, $r, $l, $d].
"helloworld"
所以,如果你问
将字符串转换为字符的最惯用方法是什么? 二郎?
答案是没有,它已经是一个字符列表,你不必转换它。
如果你问,如何将一些函数应用于字符串的字符,例如如何减去32。
2> [ X - 32 || X <- "helloworld" ].
"HELLOWORLD"
或者,如果你问如何获得一个字符串的列表
3> [[X] || X <- "helloworld"].
["h","e","l","l","o","w","o","r","l","d"]
答案 1 :(得分:1)
在这个简单的情况下,列表推导的替代方法是来自lists模块的map函数:
String = "helloworld",
lists:map(fun(X) -> [X] end, String).
答案 2 :(得分:0)
您可以尝试使用以下代码:
1> [[X] || X <- "helloworld"].
["h","e","l","l","o","w","o","r","l","d"]