我经常发现开发人员将函数式语言和动态语言这两个术语一起使用,并想知道为什么它们总是放在一起。它们之间有什么区别?一种语言可以是动态的和功能性的吗?它们相互补充吗?为什么我们仍然需要它们?我是一名 C# 程序员,但还不了解整个动态/功能性的东西(C# 将在版本 4 中具有一些动态功能。它也可以正常工作吗?这里发生了什么?)。
谢谢,亚伯拉罕
我经常发现开发人员将函数式语言和动态语言这两个术语一起使用,并想知道为什么它们总是放在一起。它们之间有什么区别?一种语言可以是动态的和功能性的吗?它们相互补充吗?为什么我们仍然需要它们?我是一名 C# 程序员,但还不了解整个动态/功能性的东西(C# 将在版本 4 中具有一些动态功能。它也可以正常工作吗?这里发生了什么?)。
谢谢,亚伯拉罕
动态类型是一种类型系统,与编程范式“功能”正交。
动态“语言”实际上是动态类型的。这意味着您没有对变量类型进行编译时检查。
函数式语言提供了大量支持,例如 lambda 演算 - 匿名函数。
执行动态类型并支持匿名函数的语言示例:javascript。Ruby 也有一些函数式风格支持。还有其他人。
把它放在一个简单(但不准确)的答案中
sort(list)
的——适用于字符串列表和整数列表。例如红宝石等。全部然而,界限正在变得混乱,语言获得了世界上最好的……你可以拥有一种语言,两者兼而有之,一种或两种都不是。
例如,主要是静态 C# 在 3.0 中采用 lambda 表达式并在 4.0 中引入动态功能
动态类型和函数式编程是独立的概念。您可以使用一种语言,也可以两者都不使用,也可以两者都使用。
静态类型意味着对象的类型在编译时是已知的。在动态类型中,它们在运行时是已知的。
函数式编程是指通过评估函数同时避免状态变化来完成计算的编程风格。(例如:您使用递归而不是 for 循环,因为循环需要更改计数器变量等)这有助于避免错误并使并发编程更容易。纯语言要求你以函数式编程,其他人只需启用它。
示例语言:
|----------------+---------+---------|
| | Dynamic | Static |
|----------------+---------+---------|
| Functional | LISP | Haskell |
| Not functional | PHP | Java |
|----------------+---------+---------|
另一方面,动态语言是一个更广泛的概念。没有确切的定义,但通常编译器的功能越多,移动到运行时,语言就越动态。这意味着在动态语言中,您通常可以在运行时评估表达式、更改对象结构等。
If you're interested in paradigms, the paper Programming Paradigms for Dummies: What Every Programmer Should Know covers them.
In functional programming, state is implicit - the program executes by calling functions which call other functions. In imperative programming and object oriented programming, state is explicit - you change the value of a variable or object's field.
In a way, functional and imperative systems can be seen as duals - what's fixed in one is a dynamic value in the other.
Closures - which trap some explicit, mutable state in an object which can be called as a function - sit somewhere between, being neither pure functional programming but not quite fully fledged objects; they are more like anonymous objects than functions.
'Dynamic languages' is vague term, usually meaning one of the following:
Dynamically Typed Languages - languages which delay determination of type to runtime, but the set of types is fixed. Examples are Smalltalk, Lisps, current Fortress implementations. Some otherwise statically typed languages also allow some dynamic type checks - Java, C#, C++ and Ada. ( it was a failed dynamic type cast from float to int in Ada that crashed Ariane 5 )
Languages with dynamic types - languages where new types can be created at runtime. The most popular is JavaScript. Because you have to run the program to determine the types, it's harder to make IDEs for these with type aware autocompletion.
Languages which are dynamically compiled - languages where new scripts can be compiled at runtime. This is true of bash, JSP, PHP and ASP at the page scale, and true for at a finer scale for lisps and JavaScript which support an 'eval' function which compiles and runs an expression.
Functional languages which are strongly typed often perform a large amount of type inference, so it's common for their programs to have less explicit typing than poorly implemented static typed languages. This can confuse people who have only seen the lack of explicit typing in dynamic typed languages into believing that type inference is the same as dynamic typing.
xtofl 已经提供了良好的整体情况。我可以说 C# 点。
一段时间以来,C# 已经变得更容易以函数方式使用:
(函数式语言通常还有其他一些东西,例如模式匹配和更令人印象深刻的类型推断,但您可以在 C# 中合理轻松地编写大量函数式代码。)
C# 4 将通过dynamic
类型获得一些动态能力(它本身实际上是一个静态类型,你可以用它做任何事情)。这有点“选择加入” - 如果您从不使用该dynamic
类型,C# 仍将是完全静态的语言。动态响应没有语言支持,但 DLR 对此提供支持 - 例如,如果您实现IDynamicMetaObjectProvider
或派生自DynamicObject
,则可以添加动态行为。
我想说的是,C# 并没有成为一种函数式语言或动态语言,而是一种您可以以函数式风格进行编码并与动态平台互操作的语言。