在Haskell中使用'let'功能纯度 [英] Functional Purity using 'let' in Haskell
本文介绍了在Haskell中使用'let'功能纯度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我正在学习Haskell时,我明白它是一种纯粹的函数式语言。我无法理解为什么让
-statements不违反纯度。
As I am working on learning Haskell, I understand it is a purely functional language. I am having trouble understanding why let
-statements don't violate purity.
例如(以ghci):
For example (in ghci):
Prelude> let e = exp 1
Prelude> e
2.718281828459045
Prelude> let e = 2
Prelude> e
2
不是我的第二个 let
产生副作用的声明?或者是第二个 let
语句新的关闭?
isn't my second let
statement producing a side effect? Or is the second let
statement a new closure?
推荐答案
让为
e
创建一个新的绑定,该绑定会隐藏现有变量。它不会修改 e
。您可以使用以下方式轻松检查:
Your second let
creates a new binding for e
that shadows the existing variable. It does not modify e
. You can easily check this with the following:
Prelude> let e = 1
Prelude> let f () = "e is now " ++ show e
Prelude> f ()
"e is now 1"
Prelude> let e = 2
Prelude> e
2
Prelude> f ()
"e is now 1"
Prelude>
这篇关于在Haskell中使用'let'功能纯度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文