为了理解fix
函数,从Control.Monad.State
,fix :: (a -> a) -> a
,我在 中有这个小代码modifyValue
,它递增一个整数直到 49,然后函数总是返回 50。
import Control.Monad.State
type StateVal = Int
modifyValue :: StateVal -> StateVal
modifyValue v | v < 50 = v + 1
modifyValue v = 50
simpleStateStep :: State StateVal ()
simpleStateStep = do
s <- get
put $ modifyValue s
main = do
putStrLn $ show $ fix modifyValue
但是,当我启动此代码时,而不是在 50 处找到固定点,序列收敛到 50... 它声称*** Exception: <<loop>>
很明显,我犯了一些错误,因为我没有提供初始状态来启动序列,例如使用 1,这会生成[2,3,4,..,49,50,50,50,50....]
我是否不恰当地使用了修复功能?有什么办法可以找到固定点 50 吗?