开发者

Isn't String a [Char]?

As far as I know String is a type in Haskell:

type String = [Char]

Then I don't understand why the following code:

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

Gives the following type error on compilation:

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

EDIT: Fixe开发者_如何学Cd actually the problem was with the return expected of Maybe String whereas I returned [Char] and returning Just [Char] did work.


Because your line (Just x) -> snd (splitByColon x) is attempting to return a String instead of a Maybe String. You should replace that with (Just x) -> Just $ snd (splitByColon x)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜