我是 Haskell 新手,仍在尝试练习一些语法/习惯用法。谁能解释一下我在这段代码中做错了什么? main
应该打印出 5 个随机数的列表。这可以在 GHCi 中查找,但不能在文件中查找。我错过了什么?
import System.Random
main = newStdGen >>= print . take 5 . randomRs (1,10)
最佳答案
当你编译这个时,你会得到
Ambiguous type variable `a0' in the constraints ....
这是因为在ghci中,1
和10
的类型默认为Integer,但编译器没有相同的默认规则。
由于您在执行其他操作之前打印它们,因此它无法推断出类型。
例如,您需要做的就是添加显式类型签名
main = newStdGen >>= print . take 5 . randomRs (1::Int,10)
关于haskell - 主要功能错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16309432/