haskell polymorphism and lists

后端 未结 6 1011
温柔的废话
温柔的废话 2021-02-02 13:45

Suppose I have the following:

class Shape a where
    draw a :: a -> IO ()

data Rectangle = Rectangle Int Int

instance Shape Rectangle where
    draw (Recta         


        
6条回答
  •  太阳男子
    2021-02-02 14:44

    Consider using a single type instead of separate types and a typeclass.

    data Shape = Rectangle Int Int
               | Circle Int Int
    
    draw (Rectangle length width) = ...
    draw (Circle center radius)   = ...
    
    shapes = [Circle 5 10, Circle 20 30, Rectangle 10 15]
    

提交回复
热议问题