开发者

Variable values in haskell

开发者 https://www.devze.com 2023-02-18 00:36 出处:网络
Lets say i have data A = B Char Integer and I have a variable called v开发者_开发技巧ar in type A.

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

精彩评论

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