字符串不是[Char]吗?

mhi*_*tza 0 haskell

据我所知String,type在Haskell:

type String = [Char]
Run Code Online (Sandbox Code Playgroud)

然后我不明白为什么以下代码:

stringDecode :: String -> Maybe String
stringDecode s =
  let
    splitByColon x' = splitAt x' s
  in case findIndex (\b -> b == ':') s of
     (Just x)  -> snd (splitByColon x) 
     (Nothing) -> Nothing
Run Code Online (Sandbox Code Playgroud)

编译时出现以下类型错误:

Couldn't match expected type `Maybe String'
            with actual type `[Char]'
Expected type: ([Char], Maybe String)
  Actual type: ([Char], [Char])
In the return type of a call of `splitByColon'
In the first argument of `snd', namely `(splitByColon x)'
Run Code Online (Sandbox Code Playgroud)

编辑:修复实际上问题是返回预期,Maybe String而我返回 [Char]并返回Just [Char]确实工作.

Lil*_*ard 5

因为你的线路(Just x) -> snd (splitByColon x)试图返回String而不是a Maybe String.你应该替换它(Just x) -> Just $ snd (splitByColon x)