【发布时间】:2020-01-05 01:53:44
【问题描述】:
我需要使用 List Comprehension 生成一个类似这样的列表:["AaBB", "AbBB", "AcBB", "AdBB", "AeBB", "AfBB","AgBB"]。但我在创建表达式时遇到了一些问题
我尝试创建一个列表,其中每个元素都是字符串连接,类似于“A”+x+“BB”,其中 x 是一系列字母中的一个元素,以“a”开头并以“a”结尾“克”
module C where
genList :: [String]
genList = [ "A" ++ x ++ "BB" | x <- ["a" .. "g"]]
所以,我希望生成一个类似于问题中所问的列表。但是我却得到了这个编译错误:
Prelude> :l exC
[1 of 1] Compiling C ( exC.hs, interpreted )
exC.hs:3:41: error:
• No instance for (Enum [Char])
arising from the arithmetic sequence ‘"a" .. "g"’
• In the expression: ["a" .. "g"]
In a stmt of a list comprehension: x <- ["a" .. "g"]
In the expression: ["A" ++ x ++ "BB" | x <- ["a" .. "g"]]
|
3 | genList = [ "A" ++ x ++ "BB" | x <- ["a" .. "g"]]
| ^^^^^^^^^^^^
Failed, no modules loaded.
【问题讨论】: