如何获得自定义数据类型成员?

zyt*_*hon 1 haskell

这是我到目前为止所尝试的.我想Info用一个String和两个Ints 做一个类型.现在我想访问该String类型的给定实例.我已阅读在Haskell中访问自定义数据类型的成员.

我没想到这会起作用,但无法搜索我正在寻找的东西:

Prelude> data Info = Info String Int Int
Prelude> aylmao = Info "aylmao" 2 3
Prelude> aylmao . String 

<interactive>:4:1: error:
    • Couldn't match expected type ‘b0 -> c’ with actual type ‘Info’
    • In the first argument of ‘(.)’, namely ‘aylmao’
      In the expression: aylmao . String
      In an equation for ‘it’: it = aylmao . String
    • Relevant bindings include
        it :: a -> c (bound at <interactive>:4:1)

<interactive>:4:10: error:
    Data constructor not in scope: String :: a -> b0
Prelude> 
Run Code Online (Sandbox Code Playgroud)

我希望能够访问我的类型的任何匿名成员,我该怎么做?

Sim*_*ine 6

如何获得自定义数据类型成员?

data Info = Info String Int Int
Run Code Online (Sandbox Code Playgroud)

正如Willem Van Onsem所说,你可以编写一个执行此操作的函数:

getInfoString :: Info -> String
getInfoString (Info x _ _) = x
Run Code Online (Sandbox Code Playgroud)

或者,您可以使用记录语法来命名数据类型字段:

data Info = Info { infoString :: String
                 , infoInt1   :: Int
                 , infoInt2   :: Int
                 }
Run Code Online (Sandbox Code Playgroud)

infoString :: Info -> String用作功能.

最好是为这些领域提出更好的名称.