19

-Wall使用 GHCi 时,我想知道从提示符(重新)加载时如何使用该选项。

例如在Haskell Programming Tips的第 3.3 节 中,带守卫的例子如下:

-- Bad implementation:
fac :: Integer -> Integer
fac n | n == 0 = 1
      | n /= 0 = n * fac (n-1)

-- Slightly improved implementation:
fac :: Integer -> Integer
fac n | n == 0    = 1
      | otherwise = n * fac (n-1)

它说“第一个问题是编译器几乎不可能检查这样的保护是否详尽无遗,因为保护条件可能非常复杂(如果你使用 -Wall 选项,GHC 会警告你)。”

我知道我可以ghci -Wall some_file.hs从命令行输入,但是一旦在提示符中,我不确定如果我想重新加载如何检查警告。

尝试谷歌后我似乎无法找到答案!

提前致谢!

4

1 回答 1

28

在 ghci 中,输入

:set -Wall

如果你想关闭所有警告,你可以这样做

:set -w

(注意小写w。大写将打开正常警告。)

用户指南中,它说我们可以在命令提示符下使用任何 ghc 命令行选项,只要它们被列为动态,我们可以从标志参考中看到所有警告设置都是动态的。

这是一个示例会话,使用上面的“错误实现”:

Prelude> :l temp.hs
[1 of 1] Compiling Main             ( temp.hs, interpreted )
Ok, modules loaded: Main.
(0.11 secs, 6443184 bytes)

*Main> :set -Wall

*Main> :l temp.hs
[1 of 1] Compiling Main             ( temp.hs, interpreted )

temp.hs:3:1:
    Warning: Pattern match(es) are non-exhaustive
             In an equation for `fac': Patterns not matched: _

Ok, modules loaded: Main.
(0.14 secs, 6442800 bytes)
于 2012-11-08T23:38:47.930 回答