Json 不能';t将预期的FullWeather类型与实际的m1 FullWeather类型匹配

Json 不能';t将预期的FullWeather类型与实际的m1 FullWeather类型匹配,json,haskell,haskell-stack,Json,Haskell,Haskell Stack,我正在使用json代码。由于类型中的错误,我无法从中提取值。为了得到这个错误,我使用了一个函数eitherDecode 代码部分: extractValues :: BSL.ByteString -> FullWeather extractValues rawJSON = do let result = eitherDecode rawJSON :: Either String FullWeather case result of Left problem

我正在使用json代码。由于类型中的错误,我无法从中提取值。为了得到这个错误,我使用了一个函数eitherDecode

代码部分:

extractValues :: BSL.ByteString -> FullWeather
extractValues rawJSON = do
    let result  = eitherDecode rawJSON :: Either String FullWeather
    case result of
        Left problem -> return problem
        Right ok     -> return ok
厄洛尔:

  Couldn't match expected type ‘FullWeather’
       with actual type ‘m0 String’    
25        Left problem -> return problem

  Couldn't match expected type ‘FullWeather’
       with actual type ‘m1 FullWeather’
26        Right ok     -> return ok

我可以发现三个问题:

  • do表示法用于一元值,是
    (>>=)
    (>>)
    应用程序的语法糖。不要对不返回一元值的函数使用do符号(如
    IO a
    [a]
    可能是a
    …)。在do符号之外,正确的语法是
    let。。。在…

    我建议遵循Haskell教程,如,它将教您正确使用do符号

  • 函数
    return
    的类型为
    return::Monad m=>a->ma

    返回
    不是关键字

  • 如果函数返回一个
    FullWeather
    ,它也不能返回字符串。这就是
    可能的用途。
    你可以做的另一件事是抛出一个错误

  • 我们可以使用三种基本解决方案:

  • 出现问题时抛出错误

    extractValues :: BSL.ByteString -> FullWeather
    extractValues rawJSON =
        let result  = eitherDecode rawJSON :: Either String FullWeather
        in case result of
            Left problem -> error problem
            Right ok     -> ok
    
  • 返回一个
    可能是FullWeather
    而不是
    FullWeather

    extractValues :: BSL.ByteString -> Maybe FullWeather
    extractValues rawJSON =
        let result  = eitherDecode rawJSON :: Either String FullWeather
        in case result of
            Left problem -> Nothing
            Right ok     -> Just ok
    
  • 返回
    字符串FullWeather
    而不是
    FullWeather

    extractValues :: BSL.ByteString -> Either String FullWeather
    extractValues = eitherDecode
    

  • 我可以发现三个问题:

  • do表示法用于一元值,是
    (>>=)
    (>>)
    应用程序的语法糖。不要对不返回一元值的函数使用do符号(如
    IO a
    [a]
    可能是a
    …)。在do符号之外,正确的语法是
    let。。。在…

    我建议遵循Haskell教程,如,它将教您正确使用do符号

  • 函数
    return
    的类型为
    return::Monad m=>a->ma

    返回
    不是关键字

  • 如果函数返回一个
    FullWeather
    ,它也不能返回字符串。这就是
    可能的用途。
    你可以做的另一件事是抛出一个错误

  • 我们可以使用三种基本解决方案:

  • 出现问题时抛出错误

    extractValues :: BSL.ByteString -> FullWeather
    extractValues rawJSON =
        let result  = eitherDecode rawJSON :: Either String FullWeather
        in case result of
            Left problem -> error problem
            Right ok     -> ok
    
  • 返回一个
    可能是FullWeather
    而不是
    FullWeather

    extractValues :: BSL.ByteString -> Maybe FullWeather
    extractValues rawJSON =
        let result  = eitherDecode rawJSON :: Either String FullWeather
        in case result of
            Left problem -> Nothing
            Right ok     -> Just ok
    
  • 返回
    字符串FullWeather
    而不是
    FullWeather

    extractValues :: BSL.ByteString -> Either String FullWeather
    extractValues = eitherDecode