从Haskell中的数据获取值

从Haskell中的数据获取值,haskell,Haskell,如何让约翰变老?有没有像(smiths!!0)这样的函数。年龄?还是只有模式匹配才有可能?您可以执行模式匹配: type Name = String type Age = age data Person = P Name Age derieving (eq) type People = [Person] smiths = [P "John" 21, P "Willy" 26] 然后Haskell将自动构造一个“getter”age::Person->a

如何让约翰变老?有没有像(smiths!!0)这样的函数。年龄?还是只有模式匹配才有可能?

您可以执行模式匹配:

type Name = String
type Age = age
data Person = P Name Age derieving (eq)
type People = [Person]

smiths = [P "John" 21, P "Willy" 26]
然后Haskell将自动构造一个“getter”
age::Person->age
,然后您可以通过以下方式访问它:

data Person = P {
    name :: Name
  , age :: Age
  } deriving Eq
age(史密斯!!0)
data Person = P {
    name :: Name
  , age :: Age
  } deriving Eq
age (smiths !! 0)