我注意到我写的一个函数现在不起作用,尽管在其他场合成功使用它。没有使用'next'的实例
我的测试文件(做一个只是为了测试这个问题)看起来是这样的:
import System.Random
generator = next . snd
这将导致错误
No instance for (RandomGen g0) arising from a use of ‘next’
The type variable ‘g0’ is ambiguous
Relevant bindings include
generator :: (a, g0) -> (Int, g0) (bound at Test.hs:2:1)
Note: there is a potential instance available:
instance RandomGen StdGen -- Defined in ‘System.Random’
In the first argument of ‘(.)’, namely ‘next’
In the expression: next . snd
In an equation for ‘generator’: generator = next . snd
奇怪的是,如果我打开ghci中和类型:
import System.Random
let generator = next . snd
一切正常。我错过了什么?
编辑:也尝试这样做,它工作得很好:
generator something = next (snd something)
ahhh您找到[Monomorphism Restriction Instance](https://wiki.haskell.org/Monomorphism_restriction);) - 在GHCi中运行':set -XMonomorphismRestriction',您将得到相同的结果 – Carsten