我希望下面的函数noToState
有效,它会在所有状态之间传播,找到与给定状态号匹配的状态并返回状态。
class State a where
allStates :: [a]
class (State a) => IntState a where
-- starting from zero, consecutive
stateNo :: a -> Integer
noToState :: (IntState a) => Integer -> a
noToState n = case lookup n $ zip (map stateNo allStates) allStates of
Just st -> st
Nothing -> undefined -- this should never happen
但是,它会产生错误:Could not deduce (IntState a0) arising from a use of ‘stateNo’
。
所以在代码中我犯了什么错误?我应该怎么解决它们?感谢。
答案 0 :(得分:4)
将其更改为:
noToState :: (IntState a) => Integer -> a
noToState n = case lookup n $ zip (map stateNo allSts) allSts of
Just st -> st
Nothing -> undefined -- this should never happen
where allSts = allStates
问题是你使用allStates
两次,它可能是不同的东西