我刚刚从真实世界的 haskell 中输入了 RandomState 示例。它看起来像这样:
import System.Random
import Control.Monad.State
type RandomState a = State StdGen a
getRandom :: Random a => RandomState a
getRandom =
get >>= \gen ->
let (val, gen') = random gen in
put gen' >>
return val
getTwoRandoms :: Random a => RandomState (a, a)
getTwoRandoms = liftM2 (,) getRandom getRandom
它可以工作,但不会显示结果。我收到错误消息:
No instance for (Show (RandomState (Int, Int)))
arising from a use of `print' at <interactive>:1:0-38
Possible fix:
add an instance declaration for (Show (RandomState (Int, Int)))
In a stmt of a 'do' expression: print it
我在为 Show RandomState 添加实例时遇到了一些问题。谁能告诉我这是怎么做到的?
谢谢。