【发布时间】:2021-07-25 20:38:42
【问题描述】:
程序(Main.hs,在 SO 上找到)看起来像这样
import Control.Monad.Random
main :: IO ()
main = do
gen <- getStdGen
let values = evalRand diceSums gen
print . take 10 $ values
-- Generate sums from rolling two dice
diceSums :: RandomGen g => Rand g [Int]
diceSums = do
xs <- dieRolls
ys <- dieRolls
return $ zipWith (+) xs ys
-- Single die roll function.
dieRolls :: RandomGen g => Rand g [Int]
dieRolls = getRandomRs (1, 6)
然后我跑
stack ghci
Prelude> :l Main.hs
[1 of 1] Compiling Main ( Main.hs, interpreted )
Ok, one module loaded.
*Main> main
<interactive>:2:1: error:
* Variable not in scope: main
* Perhaps you meant `min' (imported from Prelude)
*Main> :t dieRolls
<interactive>:1:1: error: Variable not in scope: dieRolls
如果我尝试编译,我会得到
ghc Main.hs
[1 of 1] Compiling Main ( Main.hs, Main.o )
Main.hs:1:1: error:
The IO action `main' is not defined in module `Main'
我做错了什么?
【问题讨论】:
-
可能是
stack坚持要你在模块顶部添加module声明module Main where? this 似乎相关,但根据它,如果没有声明,main应该仍然可用(尽管dieRolls确实不应该)。或者您可能只是从错误的目录加载了错误的文件? -
感谢@WillNess 添加模块声明以在 ghci 中运行它。
-
好的,我已将其添加为答案。这很奇怪,根据链接,无论如何
main应该仍然可用。
标签: haskell