String Haskell整型元组到字符串的列表

String Haskell整型元组到字符串的列表,string,haskell,tuples,String,Haskell,Tuples,我正试图从以下位置写出我的元组列表: [(8,7),(5,6),(3,3),(9,4),(5,4)] 致: 我已经走了这么远: (更新) 我知道我想把这个函数映射到我列表中的所有元素,但我似乎不能正确地得到它 (更新) 它起作用了,但引号仍然有问题 电路板的类型也包括: type Board = [(Int,Int)] 使用模式匹配: type Board = [(Int, Int)] showTuples :: Board -> String showTuples [] = ""

我正试图从以下位置写出我的元组列表:

[(8,7),(5,6),(3,3),(9,4),(5,4)]
致:

我已经走了这么远: (更新)

我知道我想把这个函数映射到我列表中的所有元素,但我似乎不能正确地得到它

(更新) 它起作用了,但引号仍然有问题

电路板的类型也包括:

type Board = [(Int,Int)]

使用模式匹配:

type Board = [(Int, Int)]

showTuples :: Board -> String
showTuples [] = ""
showTuples (x:[]) = show(fst(x)) ++ " " ++ show(snd(x))
showTuples (x:xs) = show(fst(x)) ++ " " ++ show(snd(x)) ++ " " ++ showTuples xs

main :: IO ()
main = putStrLn . showTuples $ [(8, 7), (5, 6), (3, 3), (9, 4), (5, 4)] -- 8 7 5 6 3 3 9 4 5 4

这也可以通过
foldl
实现

Prelude> foldl (\r (x,y) -> r ++ " " ++ show x ++ " " ++ show y) "" [(8,7),(5,6),(3,3),(9,4),(5,4)]
" 8 7 5 6 3 3 9 4 5 4"

如果你不想要前面的空白,那就像
tail$foldl(\r(x,y)->…

你应该试试
unwords.fmap show.concatMap(\(x,y)->[x,y])
谢谢你,效果很好。。但是我仍然有“不应该
showTuple
有类型
(Int,Int)->String
,仅基于名称?是的,我现在更改了它。如果您不想在打印字符串时看到引号,则需要使用
putStrLn
而不是
print
。如果列表为空,则无需为具有单个元素的列表添加模式pattern@MrTsjolder你真的需要这个额外的箱子,因为你在这些数字之间插入额外的空间。谢谢你的时间,它是固定的!@鲁贝尔,如果这个答案对你有帮助,请考虑接受ITI如果它做了OFC,但是@ GalaIS实际上帮助了我。我只是感谢这篇文章,因为它是一个替代的解决方案。使用<代码> FoLDL > <代码>(++)将非常昂贵,不是吗?我看不出有任何理由这不能成为一个
foldr
type Board = [(Int, Int)]

showTuples :: Board -> String
showTuples [] = ""
showTuples (x:[]) = show(fst(x)) ++ " " ++ show(snd(x))
showTuples (x:xs) = show(fst(x)) ++ " " ++ show(snd(x)) ++ " " ++ showTuples xs

main :: IO ()
main = putStrLn . showTuples $ [(8, 7), (5, 6), (3, 3), (9, 4), (5, 4)] -- 8 7 5 6 3 3 9 4 5 4
Prelude> foldl (\r (x,y) -> r ++ " " ++ show x ++ " " ++ show y) "" [(8,7),(5,6),(3,3),(9,4),(5,4)]
" 8 7 5 6 3 3 9 4 5 4"