创建一个矩形? - Haskell

3
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)

并且Point被定义为

type Point = (Double, Double)

其中:

type Shade = Double
type Coord = (Int, Int)

并且

type Pixel = (Coord, Shade)
type Raster = [Pixel]

错误:

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)])
   |            

不确定我哪里出了问题?可能与Raster是[Pixel]列表有关,如果是这样,有人能帮助我解决这个问题吗?谢谢!


你似乎不太理解“类型”和“数据”的区别。RasterShadeCoord只存在于类型层面,它们只是现有类型的别名。 - Probie
是的,我在发帖后意识到了这一点。我已经编辑了帖子,尝试了一些修改,但仍然出现错误。抱歉,我是Haskell的新手,有时会犯一些愚蠢的错误。 - goat
2个回答

3

不清楚您想做什么,但如果您想编写一个具有给定类型rectangleRaster的函数,则不必涉及Rectangle

看起来最简单的解决方案如下:

rectangleRaster :: Coord -> Coord -> Raster
rectangleRaster a b = [(a, 1), (b, 1)]

这里,我已经硬编码了每个像素的Shade值为1,因为这看起来就像OP中尝试的解决方案。

你可以这样调用函数:

*Q50128894> rectangleRaster (1,2) (3,4)
[((1,2),1.0),((3,4),1.0)]

另一方面,如果你想创建一个矩形,你需要提供两个值,可以像下面的GHCi示例中所示:

*Q50128894> Rectangle (1,2) (3,4)
Rectangle (1.0,2.0) (3.0,4.0)

3

Rectangle是一个数据构造器。根据定义,它创建了一个Shape类型的值。

data Shape = .... | Rectangle Point Point | ....
--   ^^^^^          ^^^^^^^^^ ^^^^^ ^^^^^
--   type           data      type  type
--                constructor

实际上,它的类型为Rectangle :: Point -> Point -> Shape

但是在你的定义中

rectangleRaster :: Coord -> Coord -> Raster
rectangleRaster a b = Rectangle [(a, 1)] [(b, 1)]

您将rectangleRaster声明为返回Raster而不是Shape的函数,因此出现类型不匹配的错误。甚至还提示了这一点。
src\View.hs:70:24: error:
* Couldn't match type `Shape' with `[Pixel]'
  Expected type: Raster
    Actual type: Shape

即它期望找到Raster,与您的声明/类型规范相符,但它实际上找到了一个类型为Shape的值,由数据构造器Rectangle :: Point -> Point -> Shape构造。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接