Haskell 创建矩形?-哈斯克尔
矩形由两个点定义:Haskell 创建矩形?-哈斯克尔,haskell,types,compiler-errors,type-mismatch,algebraic-data-types,Haskell,Types,Compiler Errors,Type Mismatch,Algebraic Data Types,矩形由两个点定义: rectangleRaster :: Coord -> Coord -> Raster rectangleRaster a b = (Rectangle [(a, 1)] [(b, 1)]) 点的定义是 data Shape = Point Point | Rectangle Point Point | Circle Point Point | Line Point
rectangleRaster :: Coord -> Coord -> Raster
rectangleRaster a b = (Rectangle [(a, 1)] [(b, 1)])
点的定义是
data Shape
= Point Point
| Rectangle Point
Point
| Circle Point
Point
| Line Point
Point
| Polygon [Point]
deriving (Show)
其中:
type Point = (Double, Double)
及
错误:
type Pixel = (Coord, Shade)
type Raster = [Pixel]
不知道我做错了什么?这可能与光栅是[像素]列表有关,如果是,有人能帮我解决这个问题吗?谢谢 不清楚您想做什么,但是如果您想使用为
rectangleRaster
指定的类型编写函数,则不必涉及Rectangle
看起来像OP的最简单解决方案如下:
src\View.hs:70:24: error:
* Couldn't match type `Shape' with `[Pixel]'
Expected type: Raster
Actual type: Shape
* In the expression: (Rectangle [(a, 1)] [(b, 1)])
In an equation for `rectangleRaster':
rectangleRaster a b = (Rectangle [(a, 1)] [(b, 1)])
|
70 | rectangleRaster a b = (Rectangle [(a, 1)] [(b, 1)])
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
src\View.hs:70:34: error:
* Couldn't match type `[(Coord, Integer)]' with `(Double, Double)'
Expected type: Point
Actual type: [(Coord, Integer)]
* In the first argument of `Rectangle', namely `[(a, 1)]'
In the expression: (Rectangle [(a, 1)] [(b, 1)])
In an equation for `rectangleRaster':
rectangleRaster a b = (Rectangle [(a, 1)] [(b, 1)])
|
70 | rectangleRaster a b = (Rectangle [(a, 1)] [(b, 1)])
| ^^^^^^^^
src\View.hs:70:43: error:
* Couldn't match type `[(Coord, Integer)]' with `(Double, Double)'
Expected type: Point
Actual type: [(Coord, Integer)]
* In the second argument of `Rectangle', namely `[(b, 1)]'
In the expression: (Rectangle [(a, 1)] [(b, 1)])
In an equation for `rectangleRaster':
rectangleRaster a b = (Rectangle [(a, 1)] [(b, 1)])
|
70 | rectangleRaster a b = (Rectangle [(a, 1)] [(b, 1)])
|
rectangleRaster :: Coord -> Coord -> Raster
rectangleRaster a b = [(a, 1), (b, 1)]
在这里,我已经将每个像素的Shade
值硬编码为1
,因为这看起来像是OP中尝试的解决方案
您可以这样调用函数:
src\View.hs:70:24: error:
* Couldn't match type `Shape' with `[Pixel]'
Expected type: Raster
Actual type: Shape
* In the expression: (Rectangle [(a, 1)] [(b, 1)])
In an equation for `rectangleRaster':
rectangleRaster a b = (Rectangle [(a, 1)] [(b, 1)])
|
70 | rectangleRaster a b = (Rectangle [(a, 1)] [(b, 1)])
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
src\View.hs:70:34: error:
* Couldn't match type `[(Coord, Integer)]' with `(Double, Double)'
Expected type: Point
Actual type: [(Coord, Integer)]
* In the first argument of `Rectangle', namely `[(a, 1)]'
In the expression: (Rectangle [(a, 1)] [(b, 1)])
In an equation for `rectangleRaster':
rectangleRaster a b = (Rectangle [(a, 1)] [(b, 1)])
|
70 | rectangleRaster a b = (Rectangle [(a, 1)] [(b, 1)])
| ^^^^^^^^
src\View.hs:70:43: error:
* Couldn't match type `[(Coord, Integer)]' with `(Double, Double)'
Expected type: Point
Actual type: [(Coord, Integer)]
* In the second argument of `Rectangle', namely `[(b, 1)]'
In the expression: (Rectangle [(a, 1)] [(b, 1)])
In an equation for `rectangleRaster':
rectangleRaster a b = (Rectangle [(a, 1)] [(b, 1)])
|
70 | rectangleRaster a b = (Rectangle [(a, 1)] [(b, 1)])
|
rectangleRaster :: Coord -> Coord -> Raster
rectangleRaster a b = [(a, 1), (b, 1)]
另一方面,如果要创建一个矩形
,则需要提供两个点
值,如以下GHCi示例所示:
*Q50128894> rectangleRaster (1,2) (3,4)
[((1,2),1.0),((3,4),1.0)]
Rectangle
是一个数据构造函数。根据定义,它创建类型为Shape
的值
*Q50128894> Rectangle (1,2) (3,4)
Rectangle (1.0,2.0) (3.0,4.0)
data Shape = .... | Rectangle Point Point | ....
-- ^^^^^ ^^^^^^^^^ ^^^^^ ^^^^^
-- type data type type
-- constructor
它实际上有类型Rectangle::Point->Point->Shape
但根据你的定义
*Q50128894> Rectangle (1,2) (3,4)
Rectangle (1.0,2.0) (3.0,4.0)
data Shape = .... | Rectangle Point Point | ....
-- ^^^^^ ^^^^^^^^^ ^^^^^ ^^^^^
-- type data type type
-- constructor
您已将矩形光栅
声明为返回光栅
的函数,而不是形状
。因此出现了类型不匹配错误。甚至说,
rectangleRaster :: Coord -> Coord -> Raster
rectangleRaster a b = Rectangle [(a, 1)] [(b, 1)]
i、 e.它希望根据您的声明/类型规范找到光栅
,但实际上它找到了一个类型为形状
,由数据构造函数矩形::点->点->形状
构建的值。您似乎不太理解类型
和数据
之间的区别<代码>光栅
,阴影
和坐标
除了在类型级别之外,在任何地方都不存在,它们只是现有类型的别名。当我发表这篇文章时,我意识到这一点。我已经将文章编辑成我尝试过的内容,但仍然出现错误。对不起,我是哈斯克尔的新手,我犯了一些愚蠢的错误。