为什么这个程序会产生错误?

时间:2017-10-25 17:00:22

标签: haskell types

我正在尝试Haskell并做到了这一点:

data Point  = Point Float Float            deriving (Show)
data Vector = Vector Float Float           deriving (Show)
data Line   = Line Point Point             deriving (Show)
data Rect   = Rect Point Point             deriving (Show)
data QGon   = QGon Point Point Point Point deriving (Show)
data Circle = Circle Point Float           deriving (Show)

makeList (Point x y)        = [ x, y ]
makeList (Vector x y)       = [ x, y ]
makeList (Line p1 p2)       = [ makeList p1, makeList p2 ]
makeList (Rect p1 p2)       = [ makeList p1, makeList p2 ]
makeList (QGon p1 p2 p3 p4) = [ makeList p1, makeList p2
                              , makeList p3, makeList p4 ]

它产生了一个错误说:

Couldn't match type ‘[Float]’ with ‘Float’
    Expected type: Point -> Float
      Actual type: Point -> [Float]
  |
8 | makeList (Point x y)  = [ x, y ]

不幸的是,我无法理解这一点。

1 个答案:

答案 0 :(得分:1)

您的函数makeList的案例类型不同:

  • makeList (Point x y)的类型为[Float]
  • makeList (Line p1 p2)的类型为[[Float]]

您可能希望使用concat将列表列表转换为简单列表。