Haskell: Data type containing other Data Types

前端 未结 2 1145
孤城傲影
孤城傲影 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
    
    0 讨论(0)
  • 2021-01-25 00:39

    You have to give names to data constructors:

    data Shape = ShapeTri  { shapeTri  :: Tri }
               | ShapeQuad { shapeQuad :: Quad } 
                     deriving Show
    
    0 讨论(0)
提交回复
热议问题