Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/10.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
Function si的haskell函数_Function_Haskell_Figure_Chess - Fatal编程技术网

Function si的haskell函数

Function si的haskell函数,function,haskell,figure,chess,Function,Haskell,Figure,Chess,我正在尝试使用toPosition和编写函数render render :: Dimensie -> Figure a -> [[a]] 但我不知道怎么做。你能提出一个办法吗 type Figure a = Pos -> a type Pos = (Double, Double) -- (x, y) chessboard :: Figure Bool chessboard (x, y) = even (round x) == even (round y) type D

我正在尝试使用
toPosition
和编写函数
render

render :: Dimensie -> Figure a -> [[a]] 
但我不知道怎么做。你能提出一个办法吗

type Figure a = Pos -> a

type Pos = (Double, Double) -- (x, y)

chessboard :: Figure Bool
chessboard (x, y) = even (round x) == even (round y)

type Dimensie = (Int, Int)

render :: Dimensie -> Figure a -> [[a]] 
render  = undefined

toPosition :: Dimensie → (Int, Int) → Pos
toPosition d (x, y) = (fromIntegral x ∗ 2 / b − 1, 1 − fromIntegral y ∗ 2 / h)
        where
           b = fromIntegral (fst d − 1)
           h = fromIntegral (snd d − 1)
当我把它叫做

 Figure> render (3,3) chessboard
它应该给

[[True,False,True],[False,True,False],[True,False,True]]
然后我想继续写一个函数,为某个布尔值指定一个字符。例如,对于
True
它应该显示
@
,对于
False它应该显示

boolChar :: Bool -> Char
boolChar = undefined
但是我该怎么写呢? 我不知道如何从
[[a]]
中获得
[[Bool]]
render
函数中,它一直告诉我它
无法将预期的类型“a”与实际的类型“Bool”匹配
'a'是一个严格的类型变量,由类型签名约束:
然后它会告诉哪一行出错。 那么,我应该为
渲染d(x,y)c=..
编写什么来让它显示所请求的结果呢?

这里有一些提示:

  • 编写一个函数,生成包含所有坐标的
    Pos
    表格,即
    [[Pos]]]
    ,如下所示

    generateTable (i,j) = [[(1,1),(1,2),...,(1,j)], ..., [(i,1),...,(i,j)]]
    
    • 要解决这个问题,您可能需要先编写一个辅助函数
      generateRow x j=[(x,1),…,(x,j)]
      ,然后使用它生成每一行
    • 您可以使用递归,也可以使用(可能是嵌套的)列表理解。有很多选择
  • 一旦你有了
    表格::[[Pos]]
    你就可以
    映射(地图图形)表格
    来获得想要的
    [[a]]

    • 或者,您可以更快地使用
      figure
      函数,并避免首先生成对