Haskell 使用GHCi中的记录通配符扩展时出错
我创建了一个函数,该函数使用RecordWildCards语法对Haskell记录类型进行模式匹配: 布拉格马 我已将pragmas放在文件的顶部。我还尝试添加:set-xrecord通配符 类型定义 作用 错误 当我在CompanyR上使用此函数来匹配使用as模式的PersonR时,我得到: 作用 错误Haskell 使用GHCi中的记录通配符扩展时出错,haskell,record,ghci,Haskell,Record,Ghci,我创建了一个函数,该函数使用RecordWildCards语法对Haskell记录类型进行模式匹配: 布拉格马 我已将pragmas放在文件的顶部。我还尝试添加:set-xrecord通配符 类型定义 作用 错误 当我在CompanyR上使用此函数来匹配使用as模式的PersonR时,我得到: 作用 错误 在这里的第一个案例中,您做得很好,尽管我在您有++的地方修复了一个+: 但此处firstName等不是CompanyR中的记录,因此CompanyR{..}不将其纳入范围: greet2 Co
在这里的第一个案例中,您做得很好,尽管我在您有++的地方修复了一个+: 但此处firstName等不是CompanyR中的记录,因此CompanyR{..}不将其纳入范围:
greet2 CompanyR { .. } = "hello " ++ firstName ++ " " ++ lastName ++ "who works as a " ++ duty ++ " " ++ clientRName + " "
您必须像上面第一个greet2案例中那样:
greet2 CompanyR {person = PersonR { .. }, .. } = "hello " ++ firstName ++ " " ++ lastName ++ "who works as a " ++ duty ++ " " ++ clientRName ++ " "
我正在努力c@CompanyR{..}=hello++firstName$person c+++lastName$person c使用记录通配符将person从CompayR类型中删除。
greet2 :: ClientR -> String
greet2 IndividualR { person = PersonR { .. } } = "hi" ++ firstName ++ " " ++ lastName + " "
greet2 CompanyR { .. } = "hello " ++ firstName ++ " " ++ lastName ++ "who works as a " ++ duty ++ " " ++ clientRName + " "
greet2 GovOrgR {} = "Welcome"
• Couldn't match expected type ‘[Char]’
with actual type ‘PersonR -> String’
• Probable cause: ‘lastName’ is applied to too few arguments
In the first argument of ‘(++)’, namely ‘lastName’
In the second argument of ‘(++)’, namely
‘lastName ++ "who works as a " ++ duty ++ " " ++ clientRName + " "’
In the second argument of ‘(++)’, namely
‘" "
++
lastName ++ "who works as a " ++ duty ++ " " ++ clientRName + " "’
Failed, modules loaded: none.
greet2 c@(CompanyR { .. }) = "hello " ++ (firstName $ person c) ++ " " ++ (lastName $ person c)
Couldn't match expected type ‘ClientR -> PersonR’
with actual type ‘PersonR’
• The function ‘person’ is applied to one argument,
but its type ‘PersonR’ has none
In the second argument of ‘($)’, namely ‘person c’
In the first argument of ‘(++)’, namely ‘(firstName $ person c)’
• Couldn't match expected type ‘ClientR -> PersonR’
with actual type ‘PersonR’
• The function ‘person’ is applied to one argument,
but its type ‘PersonR’ has none
In the second argument of ‘($)’, namely ‘person c’
In the second argument of ‘(++)’, namely ‘(lastName $ person c)’
greet2 :: ClientR -> String
greet2 IndividualR { person = PersonR { .. } } = "hi" ++ firstName ++ " " ++ lastName ++ " "
greet2 CompanyR { .. } = "hello " ++ firstName ++ " " ++ lastName ++ "who works as a " ++ duty ++ " " ++ clientRName + " "
greet2 CompanyR {person = PersonR { .. }, .. } = "hello " ++ firstName ++ " " ++ lastName ++ "who works as a " ++ duty ++ " " ++ clientRName ++ " "