0

我正在尝试显示一个网格线,其中包含一组填充所述线中的正方形的单元格。

我已经尝试将它们像简单的列表一样连接起来。

这是线的网格。它会自行正确显示。

grid = translate (fromIntegral width * (-0.5)) 
                 (fromIntegral height * (-0.5)) 
                 (pictures (concat [
                                    [line [(i * unitWidth, 0.0)
                                          ,(i * unitWidth, fromIntegral height)]
                                    ,line [(0.0, i * unitHeight)
                                          ,(fromIntegral width, i * unitHeight)]
                                    ]
                                   | i <- [1..gridDimension]]
                            )
                 )

这是在线条之间绘制的一组单位,也可以单独正确显示。

units = pictures [translate ((x*unitWidth - unitWidth/2) + (fromIntegral width*(-0.5))) 
                            ((y*unitHeight - unitHeight/2) + (fromIntegral height*(-0.5)))
                            unit
                 | x <- [1..gridDimension], y <- [1..gridDimension]]

我的主要方法:

main = display window backgroundColor units

我可以在这个地方用网格交换单位,它工作正常。我也试过这个:

main = display window backgroundColor (units++grid)

它抛出了以下错误:

40: error:
    • Couldn't match expected type ‘[a0]’ with actual type ‘Picture’
    • In the first argument of ‘(++)’, namely ‘grid’
      In the third argument of ‘display’, namely ‘(grid ++ units)’
      In the expression: display window backgroundColor (grid ++ units)
   |
10 | main = display window backgroundColor (grid++units)
   |                                        ^^^^

/home/georg/Desktop/THM/6_semester/funktionale_programmierung/my/app/Main.hs:10:40: error:
    • Couldn't match expected type ‘Picture’ with actual type ‘[a0]’
    • In the third argument of ‘display’, namely ‘(grid ++ units)’
      In the expression: display window backgroundColor (grid ++ units)
      In an equation for ‘main’:
          main = display window backgroundColor (grid ++ units)
   |
10 | main = display window backgroundColor (grid++units)
   |                                        ^^^^^^^^^^^

/home/georg/Desktop/THM/6_semester/funktionale_programmierung/my/app/Main.hs:10:46: error:
    • Couldn't match expected type ‘[a0]’ with actual type ‘Picture’
    • In the second argument of ‘(++)’, namely ‘units’
      In the third argument of ‘display’, namely ‘(grid ++ units)’
      In the expression: display window backgroundColor (grid ++ units)
   |
10 | main = display window backgroundColor (grid++units)
   |                                              ^^^^^
4

1 回答 1

2

(++) :: [a] -> [a] -> [a]函数附加两个列表, aPicture不是列表,因此您不能使用该函数。

但是,您可以在(<>) :: Semigroup m => m -> m -> m此处使用,因为 aPicture是 a 的一个实例Semigroup

因此我们可以这样写:

main = display window backgroundColor (units <> grid)

或者您可以pictures :: [Picture] -> Picture再次使用,并包括您的unitsand grid,例如:

main = display window backgroundColor (pictures [units, grid])
于 2019-05-20T10:32:36.257 回答