Haskell: Data type containing other Data Types

前端 未结 2 1150
孤城傲影
孤城傲影 2021-01-24 23:56

if I have two data structures

data Tri = Tri {a :: Int, b :: Int , c :: Int} deriving Show
data Quad = Quad {w :: Int, x :: Int, y :: Int, z :: Int} deriving Sh         


        
2条回答
  •  佛祖请我去吃肉
    2021-01-25 00:32

    @talex's answer is correct. Here are some variations (mainly just different syntax).

    Without record syntax:

    data Shape = ShapeTri Tri | ShapeQuad Quad 
      deriving Show
    

    It may make more sense to combine Shape, Tri, and Quad:

    data Shape = Tri  {a :: Int, b :: Int , c :: Int} 
               | Quad {w :: Int, x :: Int, y :: Int, z :: Int}
      deriving Show
    

提交回复
热议问题