GHCI Haskell不记得在命令行中的绑定 [英] GHCI Haskell not remembering bindings in command line
问题描述
我试图学习Haskell,但它是一个有点困难,因为非我的绑定是从命令行记住;
I am trying to learn Haskell but it is a little hard as non of my bindings are remembered from the command line; output from my terminal below.
> let b = []
> b
[]
> 1:b
[1]
> b
[]
我不知道为什么这样,任何人都可以帮忙。
I have no idea why this is like this can anyone please help.
推荐答案
你期望你的例子做什么?
What did you expect your example to do? From what you've presented, I don't see anything surprising.
当然,这个答案可能是令人惊讶的你,或者你不会问。我会诚实:我可以猜测你的期望。如果我是对的,你认为输出将是:
Of course, that answer is probably surprising to you, or you wouldn't have asked. And I'll be honest: I can guess what you were expecting. If I'm right, you thought the output would be:
> let b = []
> b
[]
> 1:b
[1]
> b
[1]
我是对吗?假设我是,那么问题是:为什么不是?
Am I right? Supposing I am, then the question is: why isn't it?
好吧,短版本是这不是 / code> does。相反,
(:)
从其参数创建一个新列表; x:xs
是一个新列表,其第一个元素是 x
,其余部分与 xs
。但它会创建一个新列表。它就像 +
创建一个新数字,它是其参数的总和:
Well, the short version is "that's not what (:)
does". Instead, (:)
creates a new list out of its arguments; x:xs
is a new list whose first element is x
and the rest of which is identical to xs
. But it creates a new list. It's just like how +
creates a new number that's the sum of its arguments: is the behavior
> let b = 0
> b
0
> 1+b
1
> b
0
(希望不是!)
当然,这打开了下一个问题好, c $ c> b ,那么?。这是Haskell显示其真实颜色的地方:你不。在Haskell中,一旦变量被绑定到一个值,那个值永远不会改变;它就好像所有的变量和所有的数据类型是 const
(在C语言或最新的Javascript标准)或 val
(在Scala中)。
Of course, this opens up the next question of "well, how do I update b
, then?". And this is where Haskell shows its true colors: you don't. In Haskell, once a variable is bound to a value, that value will never change; it's as though all variables and all data types are const
(in C-like languages or the latest Javascript standard) or val
(in Scala).
Haskell的这个功能 - 被称为 Haskell和每一种主流语言之间的最大区别。
This feature of Haskell – it's called being purely functional – is possibly the single biggest difference between Haskell and every single mainstream language out there. You have to think about writing programs in a very different way when you aren't working with mutable state everywhere.
例如,为了更远一点,它是一个非常不同的方式。可能的下一件事你会尝试将是这样:
For example, to go a bit further afield, it's quite possible the next thing you'll try will be something like this:
> let b = []
> b
[]
> let b = 1 : b
在这种情况下,你输入 b
?
In that case, what do you think is going to be printed out when you type b
?
好吧,记住,变量不会改变!所以答案是:
Well, remember, variables don't change! So the answer is:
[1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,…
永远 - 或直到你击中control-C abort。
forever – or until you hit control-C and abort.
这是因为 let b = 1:b
定义了一个新的变量命名为 b
;你可能写了 let c = 1:c
。因此,你说 b
是一个列表,它 1
后跟 b
;因为我们知道 b
是什么,我们可以替换并获得 b
是一个 1
后跟 1
,后跟 b
,等等。或者: b = 1:b
,因此用 b
替换 b = 1 :1:b
,并替换为 b = 1:1:1:1:...
。
This is because let b = 1 : b
defines a new variable named b
; you might as well have written let c = 1 : c
. Thus, you're saying "b
is a list which is 1
followed by b
"; since we know what b
is, we can substitute and get "b
is a list which is 1
followed by 1
followed by b
", and so on forever. Or: b = 1 : b
, so substituting in for b
we get b = 1 : 1 : b
, and substituting in we get b = 1 : 1 : 1 : 1 : …
.
(Haskell产生一个无限列表,而不是进入一个无限循环,这是因为Haskell是非严格的,更普遍地被称为 lazy - 这也是 可能是Haskell和每一种主流语言之间最大的区别。有关详细信息,请在Google或Stack Overflow上搜索延迟评估。)
(The fact that Haskell produces an infinite list, rather than going into an infinite loop, is because Haskell is non-strict, more popularly referred to as lazy – this is also possibly the single biggest difference between Haskell and every single mainstream language out there. For further information, search for "lazy evaluation" on Google or Stack Overflow.)
所以,最后,我希望你能看到为什么我不感到惊讶:Haskell不能更新变量绑定。因为你的定义是 let b = []
,那么最终的结果还是 []
:-)
So, in the end, I hope you can see why I wasn't surprised: Haskell can't possibly update variable bindings. So since your definition was let b = []
, then of course the final result was still []
:-)
这篇关于GHCI Haskell不记得在命令行中的绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!