简体   繁体   English

使用GHCi中的RecordWildCards扩展时出错

[英]Error when using RecordWildCards extension from GHCi

I have created a function that uses the RecordWildCards syntax for pattern matching on a Haskell record type: 我创建了一个函数,该函数使用RecordWildCards语法对Haskell记录类型进行模式匹配:

Pragmas g语

I have placed the pragmas at the top of the file. 我已将编译指示放在文件的顶部。 I have also tried adding it with :set -XRecordWildCards . 我也尝试过使用:set -XRecordWildCards添加它。

{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE RecordWildCards #-}

Type definitions 类型定义

data ClientR = GovOrgR { clientRName :: String }
                | CompanyR { clientRName :: String,
                            companyId   :: Integer,
                            person      :: PersonR,
                            duty        :: String
                          }
                | IndividualR { person :: PersonR }
                deriving Show

data PersonR = PersonR {
                          firstName :: String,
                          lastName  :: String
                       } deriving Show

Function 功能

greet2 :: ClientR -> String
greet2 IndividualR { person = PersonR { .. } } = "hi" ++ firstName ++ " " ++ lastName + " "
greet2 CompanyR { .. } = "hello " ++ firstName ++ " " ++ lastName ++ "who works as a " ++ duty ++ " " ++ clientRName + " "
greet2 GovOrgR {} = "Welcome"

Error 错误

    • 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.

When I use this function on CompanyR to match the PersonR using as pattern , I get: 当我在CompanyR上使用此功能来匹配as patternPersonR ,我得到:

Function 功能

greet2 c@(CompanyR { .. }) = "hello " ++ (firstName $ person c) ++ " " ++ (lastName $ person c) 

Error 错误

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)’

You do it right in your first case here (although I fixed a ++ where you had + ): 你这样做的权利在这里你的第一种情况(虽然我固定一个++ ,你不得不+ ):

greet2 :: ClientR -> String
greet2 IndividualR { person = PersonR { .. } } = "hi" ++ firstName ++ " " ++ lastName ++ " "

But here firstName etc are not records in CompanyR so CompanyR { .. } does not bring them into scope: 但是这里firstName等不是CompanyR记录,因此CompanyR { .. }不会将它们纳入范围:

greet2 CompanyR { .. } = "hello " ++ firstName ++ " " ++ lastName ++ "who works as a " ++ duty ++ " " ++ clientRName + " "

You have to do something like you did in the first case of greet2 , just above: 您必须像在上面的greet2的第一种情况下那样做:

greet2 CompanyR {person = PersonR { .. }, .. } = "hello " ++ firstName ++ " " ++ lastName ++ "who works as a " ++ duty ++ " " ++ clientRName ++ " "

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM