2011-09-07 67 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