Is there any way of doing something like this in haskell ?
data Fruits = Apple Int | Orange Int deriving (Eq, Show)
basket = [Apple 2, Orange 4]
from_basket t (x:basket) =
case x of
(t i) -> i
_ -> from_basket t basket
Now i want to get the 'apple' from the list of fruits ( basket )
from_basket Apple basket
Without an explicit pattern match
case x of
Apple i -> ...
Orange i -> ...
_ ->
One way would be to define your own helper function isApple
and then do filtering:
isApple (Apple _) = True
isApple _ = False
getApples = filter isApple
Pattern matching is the tool of your choice, I don't know whether you can simplify this any further. But apart from some dirty template Haskell, I don't see any other way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With