如果你能够以格西实施该守则,那么 de子可以做你想要的一切。 这里的方案提出例外情况
foo s i
| i == 57 = read s
| otherwise = i
main = mapM_ (print . foo "") [1..100]
Now load it into ghci and use the debugger, as documented here:
http://www.haskell.org/ghc/docs/latest/html/users_guide/ghci-debugger.html#ghci-debugger-exceptions
> ghci test.hs
*Main> :set -fbreak-on-error
*Main> :trace main
1
2
... snipped 3 through 55 ...
56
Stopped at <exception thrown>
_exception :: e = _
[<exception thrown>] *Main> :back
Logged breakpoint at test.hs:2:15-20
_result :: a
s :: String
[-1: test.hs:2:15-20] *Main> :list
1 foo s i
2 | i == 57 = **read s**
3 | otherwise = i
[-1: test.hs:2:15-20] *Main> s
""
[-1: test.hs:2:15-20] *Main>
它让你在评价史上走过一步,强调提出例外(bold,而不是在终点站挨饿)的实际表述,并允许你检查当地变量。
Another option is to recompile with profiling and some flags to tag appropriate cost centers, and run with the -xc profiling option which prints the cost center stack on uncaught exceptions
http://www.haskell.org/ghc/docs/latest/html/users_guide/prof-time-options.html
> ghc -prof -auto-all test.hs
> ./test +RTS -cs
1
2
... snipped 3 through 55 ...
56
*** Exception (reporting due to +RTS -xc): (THUNK_2_0), stack trace:
Main.foo,
called from Main.main,
called from Main.CAF
--> evaluated by: Main.main,
called from Main.CAF
test: Prelude.read: no parse
The reason this is a bit difficult is described a bit earlier on the debugger page
http://www.haskell.org/ghc/docs/latest/html/users_guide/ghci-debugger.html#tracing
Basically, efficient Haskell execution doesn t use anything resembling a normal call stack, so to get that kind of information on an exception you have to be running in some special mode (debugging or profiling) which does keep that sort of information.