ghci 显示执行堆栈
因此,我正在完成 Real World Haskell 的一些初始章节练习,我想知道 GHCi 中是否有一个选项可以使其在每个递归调用上显示带有参数的函数评估。例如,我编写了一个简单版本的“map”,当我应用它时,我希望 GHCi 显示带有实际参数的每个递归调用(希望还有表达式结果)。让我能够追踪幕后发生的事情的东西。
PS 当我写这篇文章时,我有一种感觉,它可能受到 haskell 执行模型的惰性的限制,如果我错了,请纠正我。
So I'm working through some initial chapter exercise of Real World Haskell and I wanted to know if there is an option in GHCi to make it show function evaluation with parameters on each recursive call. So for example I wrote a simple version of 'map', and when I apply it, I would like GHCi to display each recursive call with actual arguments (and hopefully expression results). Something which allows me to follow whats going on behind the scenes.
P.S. As I write this I have a feeling it may be limited by the laziness of haskell's execution model, correct me if I'm wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 hood 来实现此目的:
当您运行它时,它将打印对 map2 的每个调用以及相应的参数和返回的结果。您会看到类似以下内容:
有关更多信息,请查看示例。
You can use hood for this:
When you run it, it will print each call to map2 with the corresponding arguments and the result that was returned. You'll see something like:
For more check the examples.
我通常使用 调试.Trace:
这让我看到了有缺陷的函数是如何工作的:
关键是有一个从不匹配的模式,但在不匹配时打印一些东西。这样它总是会被评估(并因此打印调试信息),并且很容易附加到任何函数。但是,如果您只想查看某些情况,也可以使其匹配,例如:
然后您只能在非基本情况下获得调试输出:
YMMV。
I typically use Debug.Trace:
This lets me see how the buggy function works:
The key is having a pattern that never matches, but prints something while it's not matching. That way it always gets evaluated (and hence prints the debugging information), and it's easy to tack on to any function. But you can also make it match if you only want to see certain cases, like:
Then you only get debugging output at the non-base-case:
YMMV.
我建议查看问题 如何在 Haskell 中获取调用堆栈? 和 Don Stewart 的回答以及链接指南关于如何使用ghci进行调试
I would recommend looking at the question How do I get a callstack in Haskell?, and Don Stewart's answer with linked guides on how to use ghci for debugging