为什么:冲刺总是打印“_”? [英] Why :sprint always prints a "_"?
问题描述
Prelude> let a = 3
Prelude> :sprint a
a = _
Prelude> let c = "ab"
Prelude> :sprint c
c = _
为什么总是打印一个 _
?我不太明白:sprint
命令的语义。
Why does it always print a _
? I don't quite get the semantics of the :sprint
command.
推荐答案
Haskell是一种懒惰的语言。
Haskell is a lazy language. It doesn't evaluate results until they are "needed".
现在,只需打印一个值就可以将其全部需要 。换句话说,如果在GHCi中键入一个表达式,它将尝试打印出结果,这导致所有这些都被评估。通常这就是你想要的。
Now, just printing a value causes all of it to be "needed". In other words, if you type an expression in GHCi, it will try to print out the result, which causes it all to be evaluated. Usually that's what you want.
sprint
命令(这是一个GHCi功能,不是Haskell语言的一部分)允许您查看现在有多少值被评估。
The sprint
command (which is a GHCi feature, not part of the Haskell language) allows you to see how much of a value has been evaluated at this point.
例如:
Prelude> let xs = [1..]
Prelude> :sprint xs
xs = _
所以,我们刚刚声明 xs
,目前尚未得到评估。现在我们来打印出第一个元素:
So, we just declared xs
, and it's currently unevaluated. Now let's print out the first element:
Prelude> head xs
1
Prelude> :sprint xs
xs = 1 : _
现在GHCi已经评估了列表的头,但没有更多。
Now GHCi has evaluated the head of the list, but nothing more.
Prelude> take 10 xs
[1,2,3,4,5,6,7,8,9,10]
Prelude> :sprint xs
xs = 1 : 2 : 3 : 4 : 5 : 6 : 7 : 8 : 9 : 10 : _
现在,前10个元素被评估,但仍然存在。 (因为 xs
是一个无限列表,这并不奇怪。)
Now the first 10 elements are evaluated, but more remain. (Since xs
is an infinite list, that's not surprising.)
你可以构建其他表达式,并一次评估一下,看看发生了什么。这实际上是GHCi调试器的一部分,它可以让您一次一步一步地执行代码。特别是如果您的代码陷入无限循环中,则不需要打印
任何内容,因为这可能会锁定GHCi。但是你仍然想看看发生了什么事情,因此 sprint
可以让你看看到目前为止的评估。
You can construct other expressions and evaluate them a bit at a time to see what's going on. This is really part of the GHCi debugger, which lets you step through your code one bit at a time. Especially if your code is getting caught in an infinite loop, you don't want to print
anything, because that might lock up GHCi. But you still want to see what's going on... hence sprint
, which lets you see what's evaluated so far.
这篇关于为什么:冲刺总是打印“_”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!