在Haskell中访问自定义数据类型的成员 [英] Accessing members of a custom data type in Haskell
问题描述
$ p $ data $ Person = Person {first_name :: String,
last_name :: String,
age :: Int
}派生(Eq,Ord,Show)
如果我想创建一个函数 print_age
来打印一个Person的年龄,就像这样: print_age(PersonJohn Smith21)
,我如何写 print_age
来访问年龄参数?我是一个面向对象的人,所以我不在这里。我基本上在寻找相当于Person.age的功能。
函数应用程序是前缀,所以年龄人
将对应于OOP语言中的 person.age()
常见。 print_age
函数可以通过函数组合自由定义
print_age = print 。年龄
或点满
print_age person = print(age person)
Say I have the following custom data type and function in Haskell:
data Person = Person { first_name :: String,
last_name :: String,
age :: Int
} deriving (Eq, Ord, Show)
If I want to create a function print_age
to print a Person's age, like so: print_age (Person "John" "Smith" 21)
, how would I write print_age
to access the age parameter? I'm an Object Oriented guy, so I'm out of my element here. I'm basically looking for the equivalent of Person.age.
Function application is prefix, so age person
would correspond to the person.age()
common in OOP languages. The print_age
function could be defined pointfree by function composition
print_age = print . age
or point-full
print_age person = print (age person)
这篇关于在Haskell中访问自定义数据类型的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!