我正试图以正确的方式打印出一块电路板,如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
1. . . . . . . . . . . . . . . . . .
2. . . . . . . . . . . . . . . . . .
3. . . . . . . . . . . . . . . . . .
4. . . . . . . …Run Code Online (Sandbox Code Playgroud) 我正在尝试从以下位置写出我的元组列表:
[(8,7),(5,6),(3,3),(9,4),(5,4)]
Run Code Online (Sandbox Code Playgroud)
到:
8 7 5 6 3 3 9 4 5 4
Run Code Online (Sandbox Code Playgroud)
这是我走了多远:(更新)
showTuples :: Board -> String
showTuples = unwords . fmap show . concatMap (\(x,y) -> [x,y])
Run Code Online (Sandbox Code Playgroud)
我知道我想把这个函数映射到我列表的所有元素,但我似乎做对了。
(更新)它有效,但仍然有引号问题
董事会的类型也是:
type Board = [(Int,Int)]
Run Code Online (Sandbox Code Playgroud) 这在ghci中运行良好:
printRobot (fight killerRobot gentleGiant)
Run Code Online (Sandbox Code Playgroud)
但这会让我看到"没有显示实例"错误,我似乎无法理解为什么.
threeRoundFight a b =
(\a b -> printRobot (fight a b))
Run Code Online (Sandbox Code Playgroud)
这是错误:
• No instance for (Show
((((t40, t50, t50) -> t50) -> t60)
-> (((t20, t10, t60) -> ((t20, t10, t60) -> t0) -> t0)
-> (([Char], a10, a0) -> [Char]) -> t30)
-> t30))
arising from a use of ‘print’
(maybe you haven't applied a function to enough arguments?)
• In the first argument of ‘print’, namely ‘it’
In a stmt of …Run Code Online (Sandbox Code Playgroud) 我试图以这种形式从整数列表中设置一个元组列表:(a,b)a < - [1..4]和b < - xs.但我不断得到声明的所有不同组合.
okTup :: [Int] -> [(Int,Int)]
okTup xs = [(i,j) | i <- [1..4], j <- xs]
Run Code Online (Sandbox Code Playgroud)
输入:okTup [3,1,4,2]
我得到的是:[(1,3),(1,1),(1,4),(1,2),(2,3),(2,1),(2,4),( 2,2),(3,3),(3,1),(3,4),(3,2),(4,3),(4,1),(4,4),(4, 2)]
但我只想这样:[(1,3),(2,1),(3,4),(4,2)]