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
函数,并避免首先生成对