Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 如何在haskell中组合两种不同类型的列表_String_List_Haskell_Combinations - Fatal编程技术网

String 如何在haskell中组合两种不同类型的列表

String 如何在haskell中组合两种不同类型的列表,string,list,haskell,combinations,String,List,Haskell,Combinations,如何组合两种不同类型的列表并在Haskell中遍历结果 例如: input: [1,2,3] ['A','B','C'], output: ["A1","A2","A3","B1","B2","B3","C1","C2","C3"]. 我试着用Int和Char做一个例子,比如: combine :: Int -> Char -> String combine a b = show b ++ show a 但是,这不起作用,因为如果我对combine3'A'使用此函数,输出将是“

如何组合两种不同类型的列表并在Haskell中遍历结果

例如:

input: [1,2,3]  ['A','B','C'],
output: ["A1","A2","A3","B1","B2","B3","C1","C2","C3"].
我试着用
Int
Char
做一个例子,比如:

combine :: Int -> Char -> String
combine a b = show b ++ show a

但是,这不起作用,因为如果我对
combine3'A'
使用此函数,输出将是
“'A'3”
,而不是
“A3”

show::Char->String
确实会在字符周围加上单引号:

*Main> show 'A'
"'A'"
但是由于
类型String=[Char]
,我们可以使用:

combine :: Int -> Char -> String
combine i c = c : show i
因此,这里我们构造了一个字符列表,其中
c
作为头(第一个字符),
show i
(整数的表示)作为尾

现在我们可以使用列表理解来组合两个列表:

combine_list :: [Int] -> [Char] -> [String]
combine_list is cs = [combine i c | c <- cs, i <- is]
你可以这样做

combiner :: [Char] -> [Int] -> [String]
combiner cs xs = (:) <$> cs <*> (show <$> xs)

*Main> combiner ['A','B','C'] [1,2,3]
["A1","A2","A3","B1","B2","B3","C1","C2","C3"]
组合器::[Char]->[Int]->[String] 组合器cs-xs=(:)cs(show-xs) *Main>combiner['A','B','C'][1,2,3] [“A1”、“A2”、“A3”、“B1”、“B2”、“B3”、“C1”、“C2”、“C3”]
这里,
(:)cs
(其中
是中缀
fmap
)将构造一个应用列表函子,而
show xs
就像
map show xs
产生
[“1”、“2”、“3”]
我们只需将应用列表应用到
[“1”、“2”、“3”
生成的
[“A1”、“A2”、“A3”、“B1”、“B2”、“B3”,“C1”、“C2”、“C3”]

combiner :: [Char] -> [Int] -> [String]
combiner cs xs = (:) <$> cs <*> (show <$> xs)

*Main> combiner ['A','B','C'] [1,2,3]
["A1","A2","A3","B1","B2","B3","C1","C2","C3"]