与 GHCi 的断点

GHCi 支持使用解释代码的开箱即用式断点(代码是:loaded)。

使用以下程序:

-- mySum.hs
doSum n = do
  putStrLn ("Counting to " ++ (show n))
  let v = sum [1..n]
  putStrLn ("sum to " ++ (show n) ++ " = " ++ (show v))

加载到 GHCi:

Prelude> :load mySum.hs 
[1 of 1] Compiling Main             ( mySum.hs, interpreted )
Ok, modules loaded: Main.
*Main> 

我们现在可以使用行号设置断点:

*Main> :break 2
Breakpoint 0 activated at mySum.hs:2:3-39

当我们运行这个函数时,GHCi 将停在相关的行:

*Main> doSum 12
Stopped at mySum.hs:2:3-39
_result::IO () = _
n::Integer = 12
[mySum.hs:2:3-39] *Main> 

我们在该计划中的位置可能会令人困惑,因此我们可以使用:list 来澄清:

[mySum.hs:2:3-39] *Main> :list
1  doSum n = do
2    putStrLn ("Counting to " ++ (show n))   -- GHCi will emphasise this line, as that's where we've stopped
3    let v = sum [1..n]

我们可以打印变量,并继续执行:

[mySum.hs:2:3-39] *Main> n
12
:continue 
Counting to 12
sum to 12 = 78
*Main>