A Shape must be a Circle or a Square. In Scala, I can write
def findCircles(shapes: List[Shape]): List[Circle]
In Haskell (and other FP languages), I'm stuck with:
findCircles :: [Shape] -> [Shape]
Is there a way to achieve the expressivity of the former if you lack subtypes?
def findCircles(shapes: List[Shape]): List[Circle]
In Haskell (and other FP languages), I'm stuck with:
findCircles :: [Shape] -> [Shape]
Is there a way to achieve the expressivity of the former if you lack subtypes?
Comments
data Circle = Circle { radius :: Double }
data Square = Square { side :: Double }
data Shape where
CircleShape :: Circle -> Shape
SquareShape :: Square -> Shape
filterCircles :: [Shape] -> [Circle]
[ShapeCircle (Circle 3.0), ShapeSquare (Square 4.0)]