动态语言和函数式语言有什么区别?
我经常发现开发人员同时使用函数式语言和动态语言这两个术语,并且想知道为什么它们总是放在一起。 它们之间有什么区别? 一种语言可以同时是动态的和功能性的吗? 它们相辅相成吗? 为什么我们还需要它们? 我是一名 C# 程序员,还不了解整个动态/功能性的东西(C# 将在版本 4 中具有一些动态功能。它也会是功能性的吗?这里发生了什么?)。
谢谢, 亚伯拉罕
I often find developers use the terms functional language and dynamic language together, and wonder why are they always being put together.
What are the differences between them? Can a language be both dynamic and functional? Do they complement each other? Why do we need them anyway?
I'm a C# programmer and don't yet understand this whole dynamic/functional thing (C# is going to have some dynamic features in ver 4. Will it also be functional? what's going on here?).
Thanks,
Abraham
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
用一个简单(但不准确)的答案来说,
sort(list)
适用于字符串列表和整数列表。 例如鲁比等。 所有然而,随着语言拾取世界上最好的东西,边界正在变得混乱......可以有一种语言,既可以是两者,也可以是一种,也可以不是。
例如,主要是静态的 C# 在 3.0 中采用 lambda 表达式,并在 4.0 中引入动态功能
To put it in a simple (but not accurate) answer
sort(list)
works for a list of strings as well as list of ints. e.g. Ruby et. allHowever the boundaries are being muddied with languages picking up the best of all worlds... You could have a language which is both, one or neither.
e.g. predominantly static C# picking up lambda expressions in 3.0 and bringing in dynamic capabilities with 4.0
动态类型(一种类型系统)与“函数式”(一种编程范式)正交。
动态“语言”实际上是动态类型的。 这意味着您无需在编译时检查变量类型。
函数式语言为 lambda 演算(匿名函数)等提供了大量支持。
执行动态类型、支持匿名函数的语言示例:javascript。 Ruby 也有一些函数式风格支持。 还有其他的。
Dynamic typing, a type system, is orthogonal to 'functional', a programming paradigm.
Dynamic 'languages' are actually dynamically typed. This means that you don't have compile-time checking of your variable types.
Functional languages offer loads of support for e.g. lambda calculus - anonymous functions.
An example of a language that does dynamic typing, and supports anonymous functions: javascript. Ruby has some functional style support, too. And there are others.
动态类型和函数式编程是独立的概念。 您可以在一种语言中选择其中之一,也可以两者都没有,也可以两者兼而有之。
静态类型意味着对象的类型在编译时是已知的。 在动态类型中,它们在运行时是已知的。
函数式编程是指通过评估函数来完成计算同时避免状态更改的编程风格。 (例如:您使用递归而不是 for 循环,因为循环需要更改计数器变量等)这有助于避免错误并使并发编程更容易。 纯语言要求您以函数式风格进行编程,其他语言只是启用它。
示例语言:
另一方面,动态语言是一个更广泛的概念。 没有确切的定义,但通常将编译器的功能移至运行时的越多,语言就越动态。 这意味着在动态语言中,您通常可以在运行时计算表达式、更改对象结构等。
Dynamic typing and functional programming are independent concepts. You can have either, neither or both in a language.
Static typing means that types of objects are known at compilation time. In dynamic typing they are known at runtime.
Functional programming means programming style where computation is done by evaluating functions while avoiding state changes. (example: you use recursion instead of for-loops, because a loop would need changing of a counter variable, etc.) This helps to avoid bugs and makes concurrent programming easier. Pure languages require you to program in functional style, others just enable it.
Example languages:
Dynamic languages on the other hand are a broader concept. There is no exact definition, but usually the more features of the compiler are moved to the runtime, more dynamic the language is. This means that in dynamic languages you can usually evaluate expressions, change object structure etc. at runtime.
如果您对范式感兴趣,请参阅《傻瓜编程范式:每个程序员应该知道的内容》一文 涵盖了它们。
在函数式编程中,状态是隐式的——程序通过调用调用其他函数的函数来执行。 在命令式编程和面向对象编程中,状态是显式的 - 您可以更改变量或对象字段的值。
在某种程度上,函数式系统和命令式系统可以被视为对偶——一个系统中的固定值在另一个系统中是动态值。
闭包——它在一个可以作为函数调用的对象中捕获一些显式的、可变的状态——位于两者之间,既不是纯函数式编程,也不是完全成熟的对象; 它们更像是匿名对象而不是函数。
“动态语言”是一个模糊的术语,通常意味着以下之一:
动态类型语言 - 将类型确定延迟到运行时的语言,但类型集是固定的。 示例包括 Smalltalk、Lisps、当前的 Fortress 实现。 一些静态类型语言也允许一些动态类型检查 - Java、C#、C++ 和 Ada。 (Ada 中从 float 到 int 的动态类型转换失败导致了 Ariane 5 崩溃)
具有动态类型的语言 - 可以在运行时创建新类型的语言。 最流行的是 JavaScript。 因为您必须运行程序来确定类型,所以很难为这些类型创建具有类型感知自动完成功能的 IDE。
动态编译的语言 - 可以在运行时编译新脚本的语言。 对于页面规模的 bash、JSP、PHP 和 ASP 来说是这样,对于支持编译和运行表达式的“eval”函数的更精细规模的 lisps 和 JavaScript 来说也是如此。
强类型的函数语言通常执行大量的类型推断,因此它们的程序通常比实现不佳的静态类型语言具有更少的显式类型。 这可能会让那些只看到动态类型语言中缺乏显式类型的人感到困惑,他们相信类型推断与动态类型相同。
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
类型(它本身实际上是一种静态类型,你可以用它做任何事情)来实现能力。 这在某种程度上是“选择加入”——如果您从不使用动态类型,C# 仍然是完全静态的语言。 没有语言支持动态响应,但 DLR 对此支持 - 例如,如果您实现IDynamicMetaObjectProvider
或从DynamicObject
派生,您可以添加动态行为。我想说,C# 并没有成为一种函数式语言或动态语言,而是一种可以以函数式风格进行编码并与动态平台进行互操作的语言。
xtofl has already offered a good overall picture. I can speak to the C# point.
C# has been becoming easier to work with in a functional way for a while now:
(There are other things functional languages often have, such as pattern matching and more impressive type inference, but you can write a lot of functional-style code reasonably easily in C#.)
C# 4 will gain some dynamic abilities through the
dynamic
type (which itself is effectively a static type you can do anything with). This will be somewhat "opt in" - if you never use thedynamic
type, C# will still be fully static language. There's no language support for responding dynamically, but the DLR has support for this - if you implementIDynamicMetaObjectProvider
or derive fromDynamicObject
, for example, you can add dynamic behaviour.I would say that C# isn't becoming a functional language or a dynamic language, but one in which you can code in a functional style and interoperate with dynamic platforms.