开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜