gpt4 book ai didi

haskell - 创建一个矩形? - haskell

转载 作者:行者123 更新时间:2023-12-02 16:08:10 24 4
gpt4 key购买 nike

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 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] 列表有关,如果是这样,任何人都可以帮我解决这个问题吗?谢谢!

最佳答案

目前尚不清楚您想要做什么,但如果您想编写一个具有 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)]

另一方面,如果您想要创建一个矩形,则需要提供两个Point 值,您可以像下面的 GHCi 中那样执行操作示例:

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

关于haskell - 创建一个矩形? - haskell ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50128894/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com