是否存在变量实际上是函数的编程语言?
例如,我会写:
x = 2
y = x + 4
print(y)
x = 5
print(y)
它会输出:
6 (=2+4)
9 (=5+4)
另外,是否有任何情况下这实际上有用?
澄清:是的,lambda 等解决了这个问题(它们是我得出这个想法的方式);我想知道是否有特定语言的默认设置:不需要函数或 lambda 关键字。
For example, I would write:
x = 2
y = x + 4
print(y)
x = 5
print(y)
And it would output:
6 (=2+4)
9 (=5+4)
Also, are there any cases where this could actually be useful?
Clarification: Yes, lambdas etc. solve this problem (they were how I arrived at this idea); I was wondering if there were specific languages where this was the default: no function or lambda keywords required or needed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
Haskell 会向你妥协,因为本质上一切都是函数,但变量只能绑定一次(这意味着你不能在同一范围内重新分配
x
)。很容易将 y = x + 4 视为变量赋值,但是当您查看 y = map (+4) [1..](这意味着将 4 添加到无限列表中从 1 开始的每个数字),现在
y
是多少?它是一个无限列表,还是一个返回无限列表的函数? (提示:这是第二个。) 在这种情况下,在利用 懒惰。实际上,在 Haskell 中,
y
的定义是一个不接受参数并返回x+4
的函数,其中x
也是一个函数,它接受无参数,但返回值 2。在任何具有一阶函数的语言中,将匿名函数分配给变量都很简单,但对于大多数语言,您必须添加括号来指示函数调用。
Lua 代码示例:
或者 Python 中的相同内容(坚持使用一阶函数,但我们可以只使用普通整数表示
x
):Haskell will meet you halfway, because essentially everything is a function, but variables are only bound once (meaning you cannot reassign
x
in the same scope).It's easy to consider
y = x + 4
a variable assignment, but when you look aty = map (+4) [1..]
(which means add 4 to every number in the infinite list from 1 upwards), what isy
now? Is it an infinite list, or is it a function that returns an infinite list? (Hint: it's the second one.) In this case, treating variables as functions can be extremely beneficial, if not an absolute necessity, when taking advantage of laziness.Really, in Haskell, your definition of
y
is a function accepting no arguments and returningx+4
, wherex
is also a function that takes no arguments, but returns the value 2.In any language with first order functions, it's trivial to assign anonymous functions to variables, but for most languages you'll have to add the parentheses to indicate a function call.
Example Lua code:
Or the same thing in Python (sticking with first-order functions, but we could have just used plain integers for
x
):您可以在 C# 中使用 func 表达式
...或者...
...如果您确实想以函数式风格进行编程,第一个选项可能是最好的。
you can use func expressions in C#
... or ...
... if you are really wanting to program in a functional style the first option would probably be best.
查看各种函数式语言,例如 F#、Haskell 和 Scala。 Scala 将函数视为具有 apply() 方法的对象,您可以将它们存储在变量中并像传递任何其他类型的对象一样传递它们。我不知道你可以将 Scala 函数的定义打印为代码。
更新:我似乎记得至少有一些 Lisp 允许你将函数漂亮地打印为代码(例如,Scheme 的漂亮打印函数)。
Check out various functional languages like F#, Haskell, and Scala. Scala treats functions as objects that have an apply() method, and you can store them in variables and pass them around like you can any other kind of object. I don't know that you can print out the definition of a Scala function as code though.
Update: I seem to recall that at least some Lisps allow you to pretty-print a function as code (eg, Scheme's pretty-print function).
这就是电子表格的工作方式。
它还与用于评估函数参数的按名称调用语义相关。 Algol 60 有这个功能,但没有流行起来,因为实现起来太复杂了。
This is the way spreadsheets work.
It is also related to call by name semantics for evaluating function arguments. Algol 60 had that, but it didn't catch on, too complicated to implement.
编程语言 Lucid 会执行此操作,尽管它调用
x 和
y
“流”而不是函数。程序会这样写:
然后你输入:
当然,Lucid(像大多数有趣的编程语言一样)相当晦涩,所以我对没有其他人发现它并不感到惊讶。 (或者寻找它)
The programming language Lucid does this, although it calls
x
andy
"streams" rather than functions.The program would be written:
And then you'd input:
Of course, Lucid (like most interesting programming languages) is fairly obscure, so I'm not surprised that nobody else found it. (or looked for it)
尝试在此处和关于函数式编程语言的维基百科。
我自己还没有研究过这些类型的语言,因为我一直专注于 OOP,但一旦 F# 发布,我很快就会深入研究。
希望这有帮助!
Try checking out F# here and on Wikipedia about Functional programming languages.
I myself have not yet worked on these types of languages since I've been concentrated on OOP, but will be delving soon once F# is out.
Hope this helps!
我见过的最接近的是图表组件中技术分析系统的一部分。 (Tradestation、metastock 等),但主要侧重于返回多组元数据(例如买入/卖出信号),然后将这些元数据输入到接受元数据或财务数据或直接绘制的其他函数中。
我的2c:
我想说,至少可以说,您建议的语言会非常令人困惑。函数通常是 r 值是有充分理由的。此代码 (javascript) 显示了如何将函数强制作为 r 值提高可读性(从而提高维护性)n 倍:
The closest I've seen of these have been part of Technical Analysis systems in charting components. (Tradestation, metastock, etc), but mainly they focus on returning multiple sets of metadata (eg buy/sell signals) which can be then fed into other functions that accept either meta data, or financial data, or plotted directly.
My 2c:
I'd say a language as you suggest would be highly confusing to say the least. Functions are generally r-values for good reason. This code (javascript) shows how enforcing functions as r-values increases readability (and therefore maintenance) n-fold:
Self 不区分字段和方法,两者都是槽,可以在完全相同的方式。槽可以包含一个值或一个函数(因此这两个仍然是单独的实体),但区别对于槽的用户来说并不重要。
Self makes no distinction between fields and methods, both are slots and can be accessed in exactly the same way. A slot can contain a value or a function (so those two are still separate entities), but the distinction doesn't matter to the user of the slot.
在 Scala 中,函数中有惰性值和按名称调用参数。
在某种程度上,这可以达到您想要的效果。
In Scala, you have lazy values and call-by-name arguments in functions.
In some way, this can have the effect you looked for.
我相信面向数学的语言,例如 Octave、R 和 Maxima 就是这样做的。我可能是错的,但没有人提到过它们,所以我想我会的。
I believe the mathematically oriented languages like Octave, R and Maxima do that. I could be wrong, but no one else has mentioned them, so I thought I would.