【问题标题】:Creating a rectangle? - Haskell创建一个矩形? - 哈斯克尔
【发布时间】:2018-10-12 05:22:47
【问题描述】:
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] 的列表有关,如果是这样,有人可以帮我解决这个问题吗?谢谢!

【问题讨论】:

  • 您似乎不太了解typedata 之间的区别。 RasterShadeCoord 只存在于类型级别,它们只是现有类型的别名
  • 是的,我在发帖时意识到了这一点。我已将帖子编辑为我尝试过的内容,但仍然出现错误。抱歉,我是 Haskell 的新手,我犯了一些愚蠢的错误。

标签: haskell types compiler-errors type-mismatch algebraic-data-types


【解决方案1】:

不清楚你想做什么,但是如果你想用rectangleRaster给定的类型编写一个函数,你不必涉及Rectangle

看起来像 OP 的最简单的解决方案是这样的:

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

在这里,我将每个 PixelShade 值硬编码为 1,因为这看起来像是 OP 中尝试的解决方案。

你可以这样调用函数:

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

另一方面,如果您想创建一个 Rectangle,则需要提供两个 Point 值,您可以像以下 GHCi 示例中那样做:

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

【讨论】:

    【解决方案2】:

    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的值,由数据构造函数@987654332构造@。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多