Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.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
Haskell 创建矩形?-哈斯克尔_Haskell_Types_Compiler Errors_Type Mismatch_Algebraic Data Types - Fatal编程技术网

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.它希望根据您的声明/类型规范找到
光栅
,但实际上它找到了一个类型为
形状
,由数据构造函数
矩形::点->点->形状

构建的值。您似乎不太理解
类型
数据
之间的区别<代码>光栅
阴影
坐标
除了在类型级别之外,在任何地方都不存在,它们只是现有类型的别名。当我发表这篇文章时,我意识到这一点。我已经将文章编辑成我尝试过的内容,但仍然出现错误。对不起,我是哈斯克尔的新手,我犯了一些愚蠢的错误。