readSquareTransition :: String -> Maybe [SquareTurn]
readSquareTransition [] = Just []
readSquareTransition (x:xs) = case x of
'L' -> Just (L : readSquareTransition xs)
'R' -> Just (R : readSquareTransition xs)
_ -> Nothing
我想要得到只是[L,L,R,R]。不过貌似我没:(以下是错误信息!Haskell String to Maybe List
src/StudentSources/LangtonsAnt.hs:231:24:
Couldn't match expected type ‘[SquareTurn]’
with actual type ‘Maybe [SquareTurn]’
In the second argument of ‘(:)’, namely ‘readSquareTransition xs’
In the first argument of ‘Just’, namely
‘(L : readSquareTransition xs)’
src/StudentSources/LangtonsAnt.hs:232:24:
Couldn't match expected type ‘[SquareTurn]’
with actual type ‘Maybe [SquareTurn]’
In the second argument of ‘(:)’, namely ‘readSquareTransition xs’
In the first argument of ‘Just’, namely
‘(R : readSquareTransition xs)’
你不得不放弃'Just':'readSquareTransition'已经返回一个Maybe的东西。 – gallais
糟糕,我正在粗心大意,你是对的,我会改变这个...... – jamshidh
你应该在'readSquareTransition xs'或$''前面加括号,或者使用中缀'(<$>)'代替'fmap'。 – gallais