多重列表理解Haskell

多重列表理解Haskell,haskell,list-comprehension,Haskell,List Comprehension,我试图用zipWith(++)连接两个列表,但我得到了一个错误,因为列表列表1是[[String]],列表列表2是[[Char]] temp (n:ns) = n : temp (ns) list1 = [ take 10 (repeat (show(n))) | n <- temp ['AA'..]] list2 = infinite list of ['word'...] temp(n:ns)=n:temp(ns) 列表1=[以10(重复(显示(n)))|n为

我试图用zipWith(++)连接两个列表,但我得到了一个错误,因为列表列表1是[[String]],列表列表2是[[Char]]

   temp (n:ns) = n : temp (ns)
   list1 = [ take 10 (repeat (show(n))) | n <- temp ['AA'..]]  
   list2 = infinite list of ['word'...] 
temp(n:ns)=n:temp(ns)
列表1=[以10(重复(显示(n)))|n为例,根据您的选择,列表如下:

b = [["random", "random", "random"], ["eggs", "eggs", "eggs"], ["bacon", "bacon", "bacon"]]
a = ["hello", "hi", "howdy"]
Prelude> prepending ["hello", "hi", "howdy"] [["random", "random", "random"], ["eggs", "eggs", "eggs"], ["bacon", "bacon", "bacon"]]
[["hellorandom","hirandom","howdyrandom"],["helloeggs","hieggs","howdyeggs"],["hellobacon","hibacon","howdybacon"]]
您想用
a
中相应的字符串在
b
的子列表中预先添加项目。我们可以使用
map
zipWith
的组合来实现这一点:

prepending :: [[a]] -> [[[a]]] -> [[[a]]]
prepending = map . zipWith (++)
以下简称:

prepending :: [[a]] -> [[[a]]] -> [[[a]]]
prepending a b = map (zipWith (++) a) b
例如:

b = [["random", "random", "random"], ["eggs", "eggs", "eggs"], ["bacon", "bacon", "bacon"]]
a = ["hello", "hi", "howdy"]
Prelude> prepending ["hello", "hi", "howdy"] [["random", "random", "random"], ["eggs", "eggs", "eggs"], ["bacon", "bacon", "bacon"]]
[["hellorandom","hirandom","howdyrandom"],["helloeggs","hieggs","howdyeggs"],["hellobacon","hibacon","howdybacon"]]
但是,如果
b
只是字符串列表,如
[“随机”、“鸡蛋”、“培根”]
,则可以使用两个映射:

prepending :: [[a]] -> [[a]] -> [[[a]]]
prepending a b = map ((`map` b) . (++)) a
然后产生:

Prelude> prepending ["hello", "hi", "howdy"] ["random", "eggs", "bacon"]
[["hellorandom","helloeggs","hellobacon"],["hirandom","hieggs","hibacon"],["howdyrandom","howdyeggs","howdybacon"]]

temp
在这里做什么?看起来这只是返回相同的列表。
'AA'
也是无效语法,使用
A
B
作为变量也是无效语法。一般来说,不清楚你的目标是什么。你不想让
zipWith
处于顶层。使用
map
或一些列表理解ike
[zipWith(++)xs[“word”,“other”]|xs@chi谢谢。你能解释更多吗?我只是在学习Haskell。@Zuckerbrenner:
'AA'
不是有效的字符串,也不能是字符。你还不能在
[“AA.”中使用它
由于字符串不是
Enum
@WillemVanOnsem的实例,这些只是示例,但我不知道它们不会被认为是有效的,因此感谢您的澄清。我只是尝试将一个字符串中的单词组合到另一个字符串中,但列表b中的所有单词都应用于a。因此,如果b是[[“随机”,“随机”,“random”]、[“eggs”、“eggs”、“eggs”]、[“bacon”、“bacon”、“bacon”、“bacon”]和a是[“hello”、“hi”、“howdy”]它将返回[“hellorandom”、“hirandom”、“howdyrandom”]、[“helloeggs”、“hieggs”、“howdyeggs”]……如果我们实际上不关心在最后得到多个列表(这个问题并不清楚),一种更简单的方法是
prepending=liftA2(+)