Haskell从字符串获取字符数组? [英] Haskell get character array from string?
问题描述
是否可以给定一个字符串,让我获得组成该字符串的每个字符?
Is it possible if given a string I could get each character composing that string?
推荐答案
在Haskell中,字符串只是(喜欢的)字符列表.你可以找到线
In Haskell, strings are just (liked) lists of characters; you can find the line
type String = [Char]
每个Haskell实现的源代码中的某个位置.这使得诸如查找某个特定字符的首次出现( elemIndex'a'mystring
)或计算每个字符的出现频率( map(head&& length))等任务成为可能.组.排序
)无关紧要.
somewhere in the source of every Haskell implementation. That makes tasks such as finding the first occurence of a certain character (elemIndex 'a' mystring
) or calculating the frequency of each character (map (head &&& length) . group . sort
) trivial.
因此,您也可以对带有字符串的列表使用常规语法.实际上,"foo"
只是 ['f','o','o']
的糖,而这反过来又是'f'的糖:'o':'o':[]
.您可以根据需要在它们上进行图案匹配,映射和折叠.例如,如果要使元素位于 mystring
的 n
位置,则可以使用 mystring!n
,前提是 0< = n<长度为mystring
.
Because of this, you can use the usual syntax for lists with strings, too. Actually, "foo"
is just sugar for ['f','o','o']
, which in turn is just sugar for 'f' : 'o' : 'o' : []
. You can pattern match, map and fold on them as you like. For instance, if you want to get the element at position n
of mystring
, you could use mystring !! n
, provided that 0 <= n < length mystring
.
这篇关于Haskell从字符串获取字符数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!