为什么 C# 是一种函数式编程语言?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/393757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why is C# a functional programmming language?
提问by Morgan Cheng
It has been said that C# can be regarded as a functional programming language, even though it is widely recognized as a OO programming language.
有人说 C# 可以被视为一种函数式编程语言,尽管它被广泛认为是一种面向对象的编程语言。
So, what feature set makes C# a functional programming language?
那么,是什么特性集使 C# 成为一种函数式编程语言?
I can only think of:
我只能想到:
- delegates (even without anonymous methods and lambda expressions)
- closures
- 委托(即使没有匿名方法和 lambda 表达式)
- 关闭
Anything else?
还要别的吗?
采纳答案by Juliet
C# has borrowed a lot of features from ML and Haskell for example:
C# 借鉴了 ML 和 Haskell 的很多特性,例如:
C# 2.0 brought us parametric polymorphism (or "generics"). I've heard that Dom Syme, one of the creators of F#, was largely responsible for implementing generics in the .NET BCL.
C# 2.0 also allows programmers to pass and returns functions as values for higher-order functions, and has limited support for anonymous delegates.
C# 3.0 and 3.5 improved support anonymous functions for true closures.
LINQ can be considered C#'s own flavor of list comprehensions.
Anonymous types look like an approximation of ML records
Type-inference is a given.
I don't know about you, but C# extension methodslook an awful lot like Haskell type classes.
There's been a lot of talk about the "dynamic" keyword in C# 4.0. I'm not 100% sure of its implementation details, but I'm fairly sure its going to use structural typingrather than late binding to retain C#'s compile time safety. Structural typing is roughly equivalent to "duck typing for static languages", its a feature that Haskell and ML hackers have been enjoying for years.
C# 2.0 为我们带来了参数多态性(或“泛型”)。我听说 Dom Syme,F# 的创建者之一,主要负责在 .NET BCL 中实现泛型。
C# 2.0 还允许程序员将函数作为高阶函数的值传递和返回,并且对匿名委托的支持有限。
C# 3.0 和 3.5 改进了对真正闭包的匿名函数的支持。
LINQ 可以被认为是 C# 自己风格的列表推导式。
匿名类型看起来像是 ML 记录的近似值
类型推断是给定的。
在 C# 4.0 中有很多关于“dynamic”关键字的讨论。我不是 100% 确定它的实现细节,但我相当确定它会使用结构类型而不是后期绑定来保留 C# 的编译时安全性。结构类型大致相当于“静态语言的鸭子类型”,这是 Haskell 和 ML 黑客多年来一直享受的功能。
This isn't to say that C# is a functional programming language. Its still missing important features such as pattern matching, tail-call optimization, and list and tuple literals. Additionally, idiomatic C# is fundamentally imperative with a heavy dependence on mutable state.
这并不是说 C# 是一种函数式编程语言。它仍然缺少重要的功能,例如模式匹配、尾调用优化以及列表和元组文字。此外,惯用的 C# 从根本上来说是必不可少的,它严重依赖可变状态。
I wouldn't necessarily consider some of those features mentioned above as exclusive to functional programming languages, but its pretty clear that the C# developers have taken a lot of inspiration from functional programming languages in the past few years.
我不一定认为上面提到的一些特性是函数式编程语言独有的,但很明显,C# 开发人员在过去几年中从函数式编程语言中获得了很多灵感。
回答by Adam Wright
There being no rigourous definition of "OO Language", "Functional Language", "Procedural Language", one can make arguments that any language fits mostly any classification; one can write procedural Java, object oriented C and functional C++. I typically use a classification based around what the main semantic features support, along with common development practice. A good way of looking at this is to examine builtin and popular frameworks, and see what style they use.
“OO语言”、“功能语言”、“过程语言”没有严格的定义,人们可以论证任何语言几乎适合任何分类;可以编写过程式 Java、面向对象的 C 和函数式 C++。我通常使用基于主要语义特征支持的分类以及常见的开发实践。看待这个问题的一个好方法是检查内置和流行的框架,看看它们使用什么风格。
Functional languages are mostly defined as those with first class function primitives, with development styles that use these to reduce complexity with idioms like "map". One other common feature is pattern matching, but I don't see this as exclusively functional. "Pure" functional languages also have no side effects, but that's not mandatory (see how fuzzy these concepts are? :).
函数式语言大多被定义为具有一流函数原语的语言,其开发风格使用这些原语来降低复杂性,如“map”等习语。另一项常见功能是模式匹配,但我不认为这是唯一的功能。“纯”函数式语言也没有副作用,但这不是强制性的(看看这些概念有多模糊?:)。
So, what's C#? Well, it has first class function style primitives, with delegates (and has gained better syntactic support for the style with anonymous delegates and lambdas). Does this make it functional? Perhaps, if one writes in a functional style. Does the Framework use this style? No, not really.
那么,什么是 C#?嗯,它具有一流的函数风格原语,带有委托(并且已经获得了对匿名委托和 lambda 样式的更好的语法支持)。这使它起作用吗?也许,如果一个人以一种功能性的风格写作。框架是否使用这种风格?不,不是真的。
As such, I wouldn't class C# as functional in general discussion - it is, at best, multi-paradigm, with some functional flavour.
因此,我不会在一般讨论中将 C# 归类为函数式——它充其量是多范式,具有一些函数式风格。
回答by Jules
C# has the some functional language features, closures, for example. The .NET libraries aren't written in a functional style, so in practice C# isn't a functional language. Almost everything is accomplished with mutation. The collection types are all mutable.
C# 具有一些函数式语言特性,例如闭包。.NET 库不是以函数式风格编写的,因此实际上 C# 不是函数式语言。几乎所有的事情都是通过突变来完成的。集合类型都是可变的。
回答by Marc Gravell
Well, delegates and closures allow it to operate in a largely functional way... for example:
好吧,委托和闭包允许它以主要功能的方式运行......例如:
var sum = data.Sum(x=>x.SomeProp);
etc
等等
You can write most higher-order functions using lambdas / delegates. The type inference isn't quitethe same as pure functional languages such as F#, but C# generic-type-inference is still pretty good (especially in C# 3.0).
您可以使用 lambdas / 委托编写大多数高阶函数。类型推断与 F# 等纯函数式语言不太一样,但 C# generic-type-inference 仍然相当不错(尤其是在 C# 3.0 中)。
This is especially true in .NET 3.5 and C# 3.0, where LINQ takes a highly-functional approach to many of the problems. But you can still use the functional aspects of C# with .NET 2.0 and C# 2.0. It is just easier with C# 3.0 and lambdas ;-p
在 .NET 3.5 和 C# 3.0 中尤其如此,在这些版本中,LINQ 对许多问题采用了一种功能强大的方法。但是您仍然可以在 .NET 2.0 和 C# 2.0 中使用 C# 的功能方面。使用 C# 3.0 和 lambdas 更容易;-p
Actually, C# is a pragmaticprogramming language. It aims to make it possible to use a number of paradigms, without punishing you hideously if you want to do something different.
实际上,C# 是一种实用的编程语言。它旨在使使用多种范式成为可能,如果你想做一些不同的事情,而不会可怕地惩罚你。
回答by Bill the Lizard
Function pointers is another feature that C# has in the functional category.
函数指针是 C# 在函数类别中的另一个特性。
I don't think C# is very widely regarded as a functional language, however. I do think it's important to point out that you can program in a functional stylein many languages that aren't purely functional.
然而,我不认为 C# 被广泛认为是一种函数式语言。我确实认为需要指出的是,您可以使用许多并非纯函数式的语言以函数式风格进行编程。
From Functional Programming:
从函数式编程:
In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes the application of functions, in contrast to the imperative programming style, which emphasizes changes in state.
在计算机科学中,函数式编程是一种编程范式,它将计算视为对数学函数的评估,并避免了状态和可变数据。它强调函数的应用,而命令式编程风格则强调状态的变化。
Using that definition, you can program in a functional style in almost any procedural language. Purely functionallanguages just enforce it.
使用该定义,您几乎可以使用任何过程语言以函数式风格进行编程。 纯函数式语言只是强制执行它。
回答by Craig Stuntz
I mostly agree with the others here who say that C# is better described as multi-paradigm than functional. But I'd add to the examples of functional features in C# LINQ, a first-class and relatively understandable system for writing monads. While purely functional languages don't require the use of monads, the example of Haskell has shown that they can be extremely useful. Yet they're one of the hardest things go grasp for many people new to Haskell. In C#, on the other hand, many people write LINQ queries these days without even realizing that they're writing monads.
我非常同意这里的其他人的看法,他们说 C# 被描述为多范式而不是功能性更好。但是我会在 C# LINQ 中添加函数特性的示例,这是一个用于编写 monad 的一流且相对易于理解的系统。虽然纯函数式语言不需要使用 monad,但 Haskell 的例子已经表明它们非常有用。然而,对于许多刚接触 Haskell 的人来说,它们是最难掌握的事情之一。另一方面,在 C# 中,如今许多人编写 LINQ 查询,甚至没有意识到他们正在编写 monad。
回答by Tomas Petricek
you can find a great overview regarding language features in the presentation from Andrew Kennedy (from MS Research) called C# is a functional programming language. My article about functional programming in C# and F#gives an overview from a higher level perspecitve (especially towards the end).
您可以在 Andrew Kennedy(来自 MS Research)的演示文稿中找到有关语言特性的精彩概述,该演示文稿称为C# 是一种函数式编程语言。我关于C# 和 F# 中的函数式编程的文章从更高层次的角度进行了概述(尤其是最后)。
T.
T。
回答by Tomas Petricek
These are the main points whow makes c# functional 1-Lamba expresions 2-Extension methods 3-Type inferende 4-Object and collection initializators 5-Closures 6-Anonymous types 7-Linq
这些是使 c# 具有功能性的要点 1-Lamba 表达式 2-扩展方法 3-类型推断 4-对象和集合初始化器 5-闭包 6-匿名类型 7-Linq