2011-09-07 68 views
0

据我所知String是在Haskell一个type是不是字符串[Char]?

type String = [Char] 

那我就不明白,为什么下面的代码:

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 

给出关于汇编以下类型的错误:

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

编辑:固定实际上问题是与回归e预计Maybe String,而我返回[Char]和返回Just [Char]确实工作。

+1

替换它你的函数可能会更简单一些,表达为'stringDecode s = fmap(flip drop s)$ elemIndex':'s' –

+1

或者'stringDecode = find((==' :')。头)。在里面 。尾巴' –

+1

如果你回答你自己的问题,请添加一个答案,不要编辑问题的正文。 – fuz

回答

5

因为您的行(Just x) -> snd (splitByColon x)正试图返回String而不是Maybe String。你应该用(Just x) -> Just $ snd (splitByColon x)

+0

谢谢,就在那一刻更新这个问题,因为我已经看到它。 :) – mhitza