为什么 C# 是一种函数式编程语言? [英] Why is C# a functional programmming language?

查看:36
本文介绍了为什么 C# 是一种函数式编程语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人说 C# 可以被视为一种函数式编程语言,尽管它被广泛认为是一种面向对象的编程语言.

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?

我只能想到:

  1. 委托(即使没有匿名方法和 lambda 表达式)
  2. 关闭

还有什么吗?

推荐答案

C# 借鉴了 ML 和 Haskell 的很多特性,例如:

C# has borrowed a lot of features from ML and Haskell for example:

  • C# 2.0 为我们带来了参数多态性(或泛型").我听说 Dom Syme,F# 的创造者之一,主要负责在 .NET BCL 中实现泛型.

  • 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 还允许程序员将函数作为高阶函数的值传递和返回,并且对匿名委托的支持有限.

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 和 3.5 改进了对真正闭包的匿名函数的支持.

C# 3.0 and 3.5 improved support anonymous functions for true closures.

LINQ 可以被认为是 C# 自己风格的列表推导式.

LINQ can be considered C#'s own flavor of list comprehensions.

匿名类型看起来像是机器学习记录的近似

Anonymous types look like an approximation of ML records

类型推断是给定的.

我不了解你,但 C# 扩展方法 看起来很像 Haskell 类型类.

I don't know about you, but C# extension methods look an awful lot like Haskell type classes.

在 C# 4.0 中有很多关于dynamic"关键字的讨论.我不是 100% 确定它的实现细节,但我相当确定它会使用 结构类型 而不是后期绑定以保留 C# 的编译时安全性.结构类型大致相当于静态语言的鸭子类型",这是 Haskell 和 ML 黑客多年来一直享受的功能.

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 typing rather 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# 是一种函数式编程语言.它仍然缺少重要的功能,例如模式匹配、尾调用优化以及列表和元组文字.此外,惯用的 C# 从根本上来说是必不可少的,它严重依赖可变状态.

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# 开发人员在过去几年中从函数式编程语言中获得了很多灵感.

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# 是一种函数式编程语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆