默认情况下具有不可变变量的语言,例如 Haskell
我对 Haskell 感兴趣的一件事是函数和变量是如何相同的。在大多数语言中,变量保存一个值,而函数则执行某些操作,然后最终返回一个值。在 Haskell 中,你看不到这种差异,并且在使用 Haskell 后,回到更“传统”的编程(其中变量与函数或方法不同)会感觉很尴尬。如果我想获取一个值,我不应该真正担心它的来源,无论它是常量值、可变变量还是复杂计算的结果!在 Haskell 中,变量只是 0 元函数。
许多面向对象的语言都有一些让人感觉有点差距的属性。
任何人都可以用类似于 Haskell 的系统来指示任何其他语言吗?我认为由于引用透明性,这在函数式语言中很常见,但我发现情况并非如此。例如,在 Lisp 中,您可以使用 (defun)
来显式声明函数。
one thing that I find fascinating about Haskell is how functions and variables are the same. In most languages a variable holds a value while a function does something and then, eventually, returns a value. In Haskell you don't see this difference and after having used Haskell, falling back to more "traditional" programming where variables are different from functions or methods feels awkward. If I want to get a value, I shouldn't really worry about its origin, whether it is a constant value, a mutable variable or the result of a complex computation! In Haskell, variables are just 0-ary functions.
Many object-oriented languages have properties that feels a bit the gap.
Can anyone indicate any other language with a system similar to Haskell? I thought it was common to functional languages because of referential transparency, but I've seen it's not the case. In Lisp, for example, you have (defun)
to explicitly declare functions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有几种语言默认具有不可变变量(即数学意义上的变量):
其他人通过“const”或“val”声明鼓励这种行为(Scala、C)。
在许多函数式语言中,可变值只能通过显式的“ref”或“var”声明来引入。
Several languages have immutable variables (i.e. variables in the mathematical sense) by default:
Others encourage this behavior via 'const' or 'val' declarations (Scala, C).
In many functional languages mutable values may only be introduced via explicit 'ref' or 'var' declarations.
在 Clojure 中,defn 只是 def 的宏。变量是不可变的并且它们保存值。函数只是值,就像任何其他类型的值一样。值是否实际上是函数(Fn)并不重要,重要的是该类型的值是否实现函数接口(IFn)。
澄清最后一点,Java 原始数组不是函数。我可能希望将其视为 Clojure 序列,我可以创建一个包装类型,允许我在原始数组上呈现该接口 (ISeq)。我还可以让包装类型实现 IFn,然后原始数组也可以被视为函数。
In Clojure defn is just a macro to def. Vars are immutable and they hold values. Functions are just values just like any other kind of value. Whether a value actually is a function (Fn) is not important as whether that type of value implements the function interface (IFn).
To clarify the last point a Java primitive array is not a function. I may wish to treat it as a Clojure Sequence, I could create a wrapper type that allows me to present that interface (ISeq) over the primitive array. I could also have the wrapper type implement IFn and the primitive array could then be treated as a function as well.
不要忘记 JavaScript。
是完全合法的。
Don't forget Javascript.
is perfectly legitimate.