IntelliJ中的Haskell调试插件 [英] haskell debug plugin in intellij

查看:94
本文介绍了IntelliJ中的Haskell调试插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

bubblesort2 :: (Ord a, Show a) => [a] -> [a]
bubblesort2 [] = []
bubblesort2 [x] = [x]
bubblesort2 (x:y:rest) =
    bubblesort2 (init bubbled) ++ [last bubbled]
    where
        (first,second) = if x > y then (y,x) else (x,y)
        bubbled = first : bubblesort2(second:rest)

我正在尝试理解上面的haskell代码。我试图调试intellij,但是由于某些原因,它会引发调试执行错误。有没有什么好的方法可以通过ide进行调试。通过gchi进行的正常调试似乎太复杂了。

I'm trying to understand the above haskell code. I tried to debug the code in intellij,jetbrains haskell plugin, but for some reason it throws debug execution error. Is there any nice way to debug through ide. The normal debugging through gchi seems to be too complex.

推荐答案

FWIW,似乎很普遍的经验是,如果您来自面向对象的背景,您会发现您不需要使用功能编程的调试器。我不知道那是你的背景,但这就是我的旅程。在写Haskell代码的几年中,我从未研究过如何调试它。

FWIW, it seems to be a common experience that if you come from an object-oriented background, you'll find that you need a debugger less with functional programming. I don't know if that's your background, but that's been my journey. In the few couple of years I've been writing Haskell code, I've never looked into how to debug it.

有时我必须调试F#代码,但仅当它与.NET的面向对象部分进行交互时。

I sometimes have to debug F# code, but only when it interacts with the object-oriented parts of .NET.

调试器可让您通过计算检查变量各个阶段的内部状态。当代码涉及可变状态时,这很有意义,但是当所有内容都是不可变的且表达式是参照透明的时,重要性就变得不那么重要了。

Debuggers let you inspect the internal state of variables at various stages through a computation. That makes sense when code involves mutable state, but becomes less important when everything is immutable, and when expressions are referentially transparent.

当我不理解时,我通常会做什么Haskell代码的一部分是我开始分解它,并尝试使用GHCi中的各种子表达式。在这个特定的示例中,您可以执行以下操作。

What I normally do when I don't understand a piece of Haskell code is that I start decomposing it and play around with the various sub-expressions in GHCi. In this particular example, you could do something like the following.

首先,希望可以清楚地知道输入为 [] <时会发生什么。 / code>或 [x]

First of all, it's hopefully clear what happens when the input is [] or [x]:

Prelude> bubblesort2 []
[]
Prelude> bubblesort2 [42]
[42]

我假设代码的一部分您想了解的是 bubblesort2(x:y:rest)的情况。然后,我要做的就是从 [] [42] 转到下一个最简单的情况,其中恰好有两个值:

I'll assume that the part of the code you want to understand is the bubblesort2 (x:y:rest) case. What I'd do, then, is move on from [] and [42] to the next-simplest case, where you have exactly two values:

Prelude> bubblesort2 [1337,42]
[42,1337]

这对应于 bubblesort2(x:y:rest)情况,其中:

Prelude> x = 1337
Prelude> y = 42
Prelude> rest = []

请注意,我只是将值绑定到符号 x , y rest 。这使您能够评估函数中 where 块中的第一个表达式:

Notice that I just bound values to the symbols x, y and rest in GHCi. This enables you to evaluate the first expression in the where block in the function:

Prelude> (first,second) = if x > y then (y,x) else (x,y)
Prelude> first
42
Prelude> second
1337

接下来要做的是运行 bubblesort2(second:rest)子表达式:

The next thing you can do is to run the bubblesort2(second:rest) sub-expression:

Prelude> bubblesort2(second:rest)
[1337]

如果您需要提醒为什么是结果,您甚至可以检查休息 second:休息

If you need a reminder on why this is the outcome, you can even inspect second, rest, and second:rest:

Prelude> second
1337
Prelude> rest
[]
Prelude> second:rest
[1337]

迟早,您可能会意识到这是 bubblesort2 [x] 的情况,这就是为什么 bubblesort2(second:rest)返回 [1337] 。现在,您应该对起泡的是个好主意,但是除此之外,您也可以对其进行评估:

Sooner or later, you may come to the realisation that this is the bubblesort2 [x] case, and that's why bubblesort2(second:rest) returns [1337]. You should now have a good idea what bubbled is, but otherwise, you can evaluate that as well:

Prelude> bubbled = first : bubblesort2(second:rest)
Prelude> bubbled
[42,1337]

继续前进,现在您可以开始分解主 bubblesort2 的正文。首先,例如:

Moving on, you can now start to decompose the main body of bubblesort2. First, for instance:

Prelude> [last bubbled]
[1337]

and:

Prelude> init bubbled
[42]

因此,再次, bubblesort2(初始化冒泡)匹配 bubblesort2 [x] 的情况,因此您得到:

So, again, bubblesort2 (init bubbled) matches the bubblesort2 [x] case, so that you get:

Prelude> bubblesort2 (init bubbled)
[42]

,最后:

Prelude> bubblesort2 (init bubbled) ++ [last bubbled]
[42,1337]

通过这些步骤,应该可以使您了解列表中包含两个元素的情况。一旦适合您,您就可以继续将GHCi中的值重新绑定到例如仔细研究案例 [1337,42,12345]

Going through steps like these should enable you to understand the case where the list has exactly two elements. Once that works for you, you can move on to re-bind the values in GHCi to e.g. walk through the case [1337, 42, 12345]:

Prelude> x = 1337
Prelude> y = 42
Prelude> rest = [12345]
Prelude> (x:y:rest)
[1337,42,12345]

我是不会指导您完成本案例,但我希望您能清楚地知道如何以与上述相同的方式来解决它。

I'm not going to walk you through this case, but I hope it's clear how you could work it through in the same way as the above.

我想我知道您的意思会说:

I think I know what you'll say:

每次调试时都必须执行此操作吗?!

Do I have to do this every time I have to debug?!

根据我的经验,当您开始使用Haskell或其他函数式编程语言时,会有很多您不了解的地方,并且您会感到经常需要接触调试器

It's my experience that when you start with Haskell, or another functional programming language, there's a lot that you don't understand, and you'll feel the need to reach for a debugger often.

那只是一个阶段,它将过去。

That's just a phase, and it'll pass.

在REPL中玩代码是一种更惯用的函数式编程方法,一旦习惯了,您将倾向于始终打开REPL。对于Haskell和F#来说,这对我都是正确的,而且我听说其他函数式程序员也是如此。

Playing around with code in a REPL is a more idiomatic approach to functional programming, and once you get used to it, you'll tend to always have a REPL open. This is true for me both when it comes to Haskell and F#, and I've heard other functional programmers say the same. It seems to be the case for Clojure programmers as well.

要明确的是,这些天来,我很少感到有必要在概述的细节级别上逐步通过Haskell代码以上。通常,只有一个或两个表达式很难理解,然后我将其隔离并在GHCi中使用它,直到了解发生了什么。

To be clear, these days, I rarely feel the need to step through Haskell code at the level of detail outlined above. Usually, there's just a single expression or two that I find difficult to understand, and then I just isolate that and play with it in GHCi until I understand what's going on.

我认为,与尝试使调试器正常工作相比,使用GHCi进行工作将使您对Haskell有更好的长期了解。

Working things out in GHCi will, I think, give you a better long-term understanding of Haskell than trying to get a debugger working.

这篇关于IntelliJ中的Haskell调试插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆