我建议你保持
Maybe
,因为它允许代码在没有找到匹配项时正常地失败。如果你那样做,你就会离开
getListOfNextStates
返回
Maybe Transition
,然后更改
findNextState
返回
Maybe State
. 现在可以这样定义:
findNextState :: DFA -> State -> Symbol -> Maybe State
findNextState (_,_,_,tList) state symbol = case newstate of
Just s -> Just (getToState s)
Nothing -> Nothing
where newstate = getListOfNextStates tList state symbol
或者更简洁地说,您可以使用
fmap :: (a -> b) -> Maybe a -> Maybe b
(或其中缀版本,
<$>
)像这样:
findNextState :: DFA -> State -> Symbol -> Maybe State
findNextState (_,_,_,tList) state symbol = getToState <$> newstate
where newstate = getListOfNextStates tList state symbol
如果你真的不认为会有一个失败的发现,或者你只是不在乎,你可以使用
fromJust :: Maybe Transition -> Transition
import Data.Maybe
getListOfNextStates :: [Transisition] -> State -> Symbol -> Transisition
getListOfNextStates tList state symbol = fromJust (find (\(sState,sym,eState) -> matchTransition state symbol (sState,sym,eState)) tList)
退货
Nothing
,有效地破坏了程序。我不会在真正的代码中这样做,除非你能绝对确保它永远不会发生。