【问题标题】:Couldnt Match Integer With Double? - Creating a Rectangle无法将整数与双精度匹配? - 创建一个矩形
【发布时间】:2018-03-30 19:11:28
【问题描述】:
shapeToPic :: Shape -> Picture
shapeToPic (Line a b) = polyline [a, b]
shapeToPic (Rectangle a b) = rectangle a b

形状由

定义
data Shape = Rectangle Side Side
       | Ellipse   Side Side
       | Polygon   [Point]
       | Line      Point Point
 deriving (Show)

我得到的错误:

<interactive>:55:41: error: * Couldn't match type(Integer, Integer)' 与Double' Expected type: Side Actual type: (Integer, Integer) * In the second argument ofRectangle',即(5, 7)' In the first argument ofshapeToPic',即 (Rectangle (3, 3) (5, 7))' In the first argument ofdrawingOf',即 `(shapeToPic (矩形 (3, 3) (5, 7)))'

对 Haskell 来说有点新,我知道我需要放置一个“Side”类型,因为矩形需要 2 个点,但是,不确定问题出在哪里。我有2分?任何解决此问题的帮助将不胜感激!

【问题讨论】:

    标签: haskell


    【解决方案1】:

    也许您打错了字,并且用逗号混淆了点?

    let x = (1.1) :: Double -- type checks
    let x = (1,1) :: Double -- same error
    

    【讨论】:

    • 具体在哪里?我应该说shapeToPic (Line a b) = polyline [a, b] 工作正常。它只是矩形代码。
    【解决方案2】:

    看起来Side 被定义为Double,而不是一对。所以,Rectangle 13.45 12.32 可以,而Rectangle (x1,y1) (x2,y2) 不行。

    显然,“矩形”形状只包含宽度和高度,而不包含“原点”。这很奇怪,因为线和多边形确实包含Shape 类型的原点。这看起来是潜在的设计不一致。

    您应该在您的问题中提供Side 的定义,否则我们只能推测。

    【讨论】:

    • 我的错!感谢您的回复!边是这样定义的:type Side = Double
    • @goat 所以我猜对了,根据你对形状的定义,矩形是一对边长,而不是一对点。
    猜你喜欢
    • 1970-01-01
    • 2013-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-21
    • 2012-09-08
    相关资源
    最近更新 更多