动态语言和函数式语言有什么区别?

发布于 2024-07-25 02:04:21 字数 209 浏览 3 评论 0原文

我经常发现开发人员同时使用函数式语言和动态语言这两个术语,并且想知道为什么它们总是放在一起。 它们之间有什么区别? 一种语言可以同时是动态的和功能性的吗? 它们相辅相成吗? 为什么我们还需要它们? 我是一名 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 技术交流群。

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

发布评论

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

评论(5

蝶舞 2024-08-01 02:04:21

用一个简单(但不准确)的答案来说,

  • 动态语言 >与它的宿敌静态类型语言相比,类型(类的名称)并不那么重要。 变量可以在任何给定时间点分配给它不同类型的对象。 方法调用在运行时解析。 这意味着您失去了静态类型的好处(编译器警告),但简单的方法变成了通用方法 - sort(list) 适用于字符串列表和整数列表。 例如鲁比等。 所有
  • 函数式语言都重视不变性。 程序是按照越来越大的函数编写的(通常是自下而上)。 对象状态和可变性的概念不受欢迎。 在这种情况下,函数自给自足(根据维基百科,该术语是纯粹的):产生输出所需的一切都位于它接收的输入中。 它还不会产生副作用(除非明确提及),并为给定的输入返回一致的输出。 这可以产生优雅的代码(参见:流畅的接口),其中输入数据通过 diff 函数进行管道传输以产生最终的输出,例如 LISP 等。

然而,随着语言拾取世界上最好的东西,边界正在变得混乱......可以有一种语言,既可以是两者,也可以是一种,也可以不是。
例如,主要是静态的 C# 在 3.0 中采用 lambda 表达式,并在 4.0 中引入动态功能

To put it in a simple (but not accurate) answer

  • Dynamic languages are ones in which the Type (Name of the Class) is not as important as compared to its nemesis statically typed languages. A variable may have objects of different types assigned to it at any given point of time. Method invocations are resolved at run-time. This means you lose the benefits of static typing (compiler warnings) but simple methods turn generic - sort(list) works for a list of strings as well as list of ints. e.g. Ruby et. all
  • Functional languages value immutability. The programs are written in terms of bigger and bigger functions (usually bottom up). The concept of object state and mutability is frowned upon. A function in this context is self-sufficient (The term is Pure as per Wikipedia): everything it needs to produce output, lies in the input that it receives. It also produces no side-effects (unless it explicitly mentions it) and returns consistent output for a given input. This can lead to elegant code (see: fluent interfaces), where input data is pipelined through diff functions to produce the eventual output e.g. LISP et.all

However 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

黄昏下泛黄的笔记 2024-08-01 02:04:21

动态类型(一种类型系统)与“函数式”(一种编程范式)正交。

动态“语言”实际上是动态类型的。 这意味着您无需在编译时检查变量类型。

函数式语言为 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.

套路撩心 2024-08-01 02:04:21

动态类型和函数式编程是独立的概念。 您可以在一种语言中选择其中之一,也可以两者都没有,也可以两者兼而有之。

静态类型意味着对象的类型在编译时是已知的。 在动态类型中,它们在运行时是已知的。

函数式编程是指通过评估函数来完成计算同时避免状态更改的编程风格。 (例如:您使用递归而不是 for 循环,因为循环需要更改计数器变量等)这有助于避免错误并使并发编程更容易。 纯语言要求您以函数式风格进行编程,其他语言只是启用它。

示例语言:

|----------------+---------+---------|
|                | Dynamic | Static  |
|----------------+---------+---------|
| Functional     | LISP    | Haskell |
| Not functional | PHP     | Java    |
|----------------+---------+---------|

另一方面,动态语言是一个更广泛的概念。 没有确切的定义,但通常将编译器的功能移至运行时的越多,语言就越动态。 这意味着在动态语言中,您通常可以在运行时计算表达式、更改对象结构等。

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 | Static  |
|----------------+---------+---------|
| Functional     | LISP    | Haskell |
| Not functional | PHP     | Java    |
|----------------+---------+---------|

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.

深巷少女 2024-08-01 02:04:21

如果您对范式感兴趣,请参阅《傻瓜编程范式:每个程序员应该知道的内容》一文 涵盖了它们。

在函数式编程中,状态是隐式的——程序通过调用调用其他函数的函数来执行。 在命令式编程和面向对象编程中,状态是显式的 - 您可以更改变量或对象字段的值。

在某种程度上,函数式系统和命令式系统可以被视为对偶——一个系统中的固定值在另一个系统中是动态值。

闭包——它在一个可以作为函数调用的对象中捕获一些显式的、可变的状态——位于两者之间,既不是纯函数式编程,也不是完全成熟的对象; 它们更像是匿名对象而不是函数。

“动态语言”是一个模糊的术语,通常意味着以下之一:

  • 动态类型语言 - 将类型确定延迟到运行时的语言,但类型集是固定的。 示例包括 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.

ι不睡觉的鱼゛ 2024-08-01 02:04:21

xtofl 已经提供了一个很好的整体图景。 我可以谈谈 C# 的观点。

一段时间以来,C# 已经变得更容易以函数方式使用:

  • C# 2 引入了匿名方法,这使得创建使用状态的委托变得更容易,而状态原本是方法的本地
  • C# 3 引入了 lambda 表达式,这些表达式大多类似于匿名方法,但 C# 3 和 .NET 3.5 中更紧凑的
  • LINQ 支持使得以函数方式查询数据变得更容易,将谓词、投影等链接在一起。在我看来,
  • C# 4 功能都没有直接有助于函数式编程,尽管命名参数和可选参数可以使创建/使用不可变类型变得更容易,这是我认为功能图片中缺少的最大功能之一。

(函数式语言还经常具有其他功能,例如模式匹配和更令人印象深刻的类型推断,但是您可以在 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:

  • C# 2 introduced anonymous methods, which made it easier to create delegates which used state which was otherwise local to a method
  • C# 3 introduced lambda expressions which are mostly like anonymous methods but even more compact
  • LINQ support in both C# 3 and .NET 3.5 made it easier to query data in a functional way, chaining together predicates, projections etc
  • None of the C# 4 features directly contributes to functional programming IMO, although named arguments and optional parameters may make it easier to create/use immutable types, which is one of the biggest features missing from the functional picture IMO.

(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 the dynamic 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 implement IDynamicMetaObjectProvider or derive from DynamicObject, 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.

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