Question: Question:
Is there a more concise way to define your own Named Fields and others in the Record Syntax as shown below and use some of the Fields?
In the code below, the part of fNumField Foo{hoge = x} = x ^ 2
feels crude.
data Foo = Foo{ bar :: String , hoge::Integer} deriving (Show)
main = do
print $ fNumField a
where
fNumField Foo{hoge = x} = x ^ 2
a = Foo{ bar = "piyo", hoge = 575 }
* (Although another question may be better) I feel that "named fields" don't suit Haskell, unlike languages like F # and OCaml. What is it usually like?
* If you say "The naming conventions and terms are strange in Haskell!", Please give us feedback in the comments and edits!
Postscript: 2015-01-26
I tried to correct it referring to the answer.
data Foo = Foo{ bar :: String , hoge::Integer} deriving (Show)
main = print $ hoge a ^ 2
where
a = Foo{ bar = "piyo", hoge = 575 }
* When the definition of hoge was covered, I was angry with Multiple declarations at compile time, so I found it difficult to use.
Answer: Answer:
When using Record syntax, a function with the same name is created. You can also create values in a notation that does not define the data type using Record syntax.
Is it like this if you write it as it is?
data Foo = Foo { bar :: String, hoge :: Integer} deriving (Show)
-- hoge :: Foo -> Integer が定義される
main = print $ fNumField a
where
fNumField = (^2) . hoge
a = Foo "piyo" 575