我有一个字符串"abcdefghijklmnopqrstuvwxyz"
。我想构建一个包含字符及其索引位置的元组列表。
例如,如果
string = "abcdefghijklmnopqrstuvxwyz"
我想构建一个包含
的列表slist = [(0,'a'), (1, 'b'), (2, 'c')...]
如何在Elm
中执行此操作?
答案 0 :(得分:3)
Elm中的字符串不是列表,因此首先必须使用String.toList
进行转换。然后,您可以使用内置的List.indexedMap
函数将索引压缩起来。
import List exposing (indexedMap)
import String exposing (toList)
getIndexedCharacters : String -> List (Int, Char)
getIndexedCharacters =
indexedMap (,) << toList