我正在尝试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 ]
不幸的是,我无法理解这一点。
答案 0 :(得分:1)
您的函数makeList
的案例类型不同:
makeList (Point x y)
的类型为[Float]
makeList (Line p1 p2)
的类型为[[Float]]
您可能希望使用concat
将列表列表转换为简单列表。