Haskell:什么是不可变数据? [英] Haskell: What is immutable data?
本文介绍了Haskell:什么是不可变数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在大多数有关Haskell的文章中,您会找到类似"Haskell中的数据是不可变的"这样的语句.我不太明白为什么.例如:
in most articles about Haskell you'll find a statement like "Data in Haskell is immutable". I don't quite understand why. For example:
let a = 123
let a = 456
在主要方法中有效.我只是将a
的数据从123
更改为456
.我想念什么?这可能是我思路中的一个愚蠢错误:/
in the main method works. I just changed the data of a
from 123
to 456
. What am I missing? It's probably a stupid mistake in my train of thought :/
祝你有美好的一天!
推荐答案
实际上,a
并没有改变.在ghci
中尝试此操作以查看:
Actually, a
hasn't changed. Try this in ghci
to see:
> a = 123
> action = print a
> a = 456
> action
123
与具有可变变量的语言进行比较,例如python:
Compare with a language that has mutable variables, e.g. python:
>>> a = 123
>>> def action(): print a
...
>>> a = 456
>>> action()
456
这篇关于Haskell:什么是不可变数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文