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]
確實工作。
替換它你的函數可能會更簡單一些,表達爲'stringDecode s = fmap(flip drop s)$ elemIndex':'s' –
或者'stringDecode = find((==' :')。頭)。在裏面 。尾巴' –
如果你回答你自己的問題,請添加一個答案,不要編輯問題的正文。 – fuz