For my uni homework we have to make a PvZ like simple game in Haskell.
I have a Plant type (the integer represents it's health)
data Plant = Peashooter Int | Sunflower Int | Walnut Int | CherryBomb Int deriving (Show, Eq, Ord)
and I would like to make a function, that takes said Plant type, and returns a boolean value based on if it's health is positive or not.
isPlantAlive :: Plant -> Bool
I tried some solutions, and what works is the following pattern-matching:
isPlantAlive :: Plant -> Bool
isPlantAlive (Peashooter health) = health /= 0
isPlantAlive (Sunflower health) = health /= 0
isPlantAlive (Walnut health) = health /= 0
isPlantAlive (CherryBomb health) = health /= 0
My question is basically this:
Is there a cleaner/better way to do this?
What I expected to also work, was something like this, to match all Plant types, no matter the constructor:
isPlantAlive :: Plant -> Bool
isPlantAlive (Plant health) = health /= 0
Thank you for your help.