默认情况下具有不可变变量的语言,例如 Haskell

发布于 2024-11-01 07:19:18 字数 385 浏览 7 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

抽个烟儿 2024-11-08 07:19:18

任何人都可以用类似于 Haskell 的系统指示任何其他语言吗?

有几种语言默认具有不可变变量(即数学意义上的变量):

  • Haskell(显然)、
  • Clean、
  • Erlang、
  • ML。

其他人通过“const”或“val”声明鼓励这种行为(Scala、C)。

在许多函数式语言中,可变值只能通过显式的“ref”或“var”声明来引入。

Can anyone indicate any other language with a system similar to Haskell?

Several languages have immutable variables (i.e. variables in the mathematical sense) by default:

  • Haskell (obviously),
  • Clean,
  • Erlang,
  • ML.

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.

空袭的梦i 2024-11-08 07:19:18

在 Clojure 中,defn 只是 def 的宏。变量是不可变的并且它们保存值。函数只是值,就像任何其他类型的值一样。值是否实际上函数(Fn)并不重要,重要的是该类型的值是否实现函数接口(IFn)。

澄清最后一点,Java 原始数组不是函数。我可能希望将其视为 Clojure 序列,我可以创建一个包装类型,允许我在原始数组上呈现该接口 (ISeq)。我还可以让包装类型实现 IFn,然后原始数组也可以被视为函数。

(def x (wrap-prim-array ...))

(nth x 0) ; idiomatic random access
(x 0)     ; used as a function

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.

(def x (wrap-prim-array ...))

(nth x 0) ; idiomatic random access
(x 0)     ; used as a function
天煞孤星 2024-11-08 07:19:18

不要忘记 JavaScript。

var a = function(x) { return (x + 1) };
var b = a(1);
// b == 2 here.

是完全合法的。

Don't forget Javascript.

var a = function(x) { return (x + 1) };
var b = a(1);
// b == 2 here.

is perfectly legitimate.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文