我不明白為什么會收到此錯誤訊息:
? Couldn't match type ‘Maybe ([a], a0)’
with ‘String -> Maybe ([a1], String)’
我的代碼:
type Parse a = String -> Maybe(a, String)
parseMany :: Parse a -> Parse [a]
parseMany p str = case p str of
Nothing -> Just ([], str)
Just (z, str') -> case parseMany (z str') of
Just (c, str'') -> Just ((z : c), show (str''))
uj5u.com熱心網友回復:
使用z str'沒有意義,z是一個型別的物件a,而不是一個可以帶String引數的函式。
您可以使用相同的基本決議器p來決議下一項:
parseMany :: Parse a -> Parse [a]
parseMany p str = case p str of
Nothing -> Just ([], str)
Just (z, str') -> case parseMany p str' of
Just (c, str'') -> Just ((z : c), str'')
Nothing -> Nothing
如果我們有一個簡單的決議器,例如:
myParser :: Parse Int
myParser ('a':xs) = Just (1, xs)
myParser _ = Nothing
然后它會將'a's串列決議為:
ghci> parseMany myParser "aaaaaa"
Just ([1,1,1,1,1,1],"")
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/316811.html
