Assume that we have next data:
data Value =
IntVal Int
| BoolVal Bool
and function
f :: Value -> Int
How can I separate cases on different constructors on argument of type Value? So f (IntVal 1)
has one behaviour and f (BoolVal True)
has some another behaviour.
There are a few different methods a popular one is to use pattern matching in a function parameter.
negateValue :: Value -> Value
negateValue (IntVal n) = IntVal (-n)
negateValue (BoolVal p) = BoolVal (not p)
Another method would be to use case patterns.
Using case:
negateValue val = case val of
IntVal n -> IntVal (-n)
BoolVal p -> BoolVal (not p)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.