调试/检查函数内的值

时间:2014-02-28 22:53:14

标签: debugging haskell ghc ghci

考虑以下设想的例子:

module Main where
myadd3 first second third = 
  let result1 = first               -- line 3
      result2 = second              -- line 4
      result3 = third               -- line 5
  in result1 + result2 + result3    -- line 6

我想在到达第6行时检查result1result2result3的值。但是当我单步执行代码时,我决不会做任何事情。这些“计算”似乎可供检查:

*Main> :break 3
Breakpoint 0 activated at Main4.hs:3:17-21
*Main> :break 4
Breakpoint 1 activated at Main4.hs:4:17-22
*Main> :break 5
Breakpoint 2 activated at Main4.hs:5:17-21
*Main> :break 6
Breakpoint 3 activated at Main4.hs:6:6-32
*Main> myadd3 2 3 4
Stopped at Main4.hs:6:6-32
_result :: a = _
result1 :: a = _
result2 :: a = _
result3 :: a = _
[Main4.hs:6:6-32] *Main> :step
Stopped at Main4.hs:6:6-22
_result :: a = _
result1 :: a = _
result2 :: a = _
[Main4.hs:6:6-22] *Main> :step
Stopped at Main4.hs:3:17-21
_result :: Integer = _
first :: Integer = 2
[Main4.hs:3:17-21] *Main> :step
Stopped at Main4.hs:4:17-22
_result :: Integer = _
second :: Integer = 3
[Main4.hs:4:17-22] *Main> :step
Stopped at Main4.hs:5:17-21
_result :: Integer = _
third :: Integer = 4
[Main4.hs:5:17-21] *Main> :step
9

即我们有像result1 :: a = _这样的行,但不是像result1 :: a = 2这样的行。这是因为haskell的懒惰性质吗?如果有的话,有一种方法可以检查result1result2result3此时值是否与它们绑定?感谢

1 个答案:

答案 0 :(得分:3)

你是正确的,这种行为是由于懒惰造成的。要强制进行评估,您可以:

[debug.hs:6:6-32] *Main> :print result1
result1 = (_t1::a)
[debug.hs:6:6-32] *Main> :force result1
*** Ignoring breakpoint
result1 = 2
[debug.hs:6:6-32] *Main> :step
Stopped at debug.hs:6:6-22
_result :: Integer = _
result1 :: Integer = 2
result2 :: Integer = _