Variable values in haskell
Lets say i have
data A = B Char Integer
and I have a variable called v开发者_开发技巧ar in
type A
.
How can I access the integer value of var
variable and use it?
Is there any operator to get it? I don't know, something like var->int
would give me it?
You can define a function for this using pattern matching:
getIntegerFromA :: A -> Integer
getIntegerFromA (B _ int) = int
Though depending on where you want to use it, you can probably get the value using pattern matching right there instead of defining a separate function.
The other way is to use "record syntax", which generates the accessor functions automatically, but many people consider ugly and un-Haskell like.
精彩评论