與 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>