简体   繁体   English

Haskell和LINQ之间有什么联系吗?

[英]Are there any connections between Haskell and LINQ?

I wrote some queries in C# using LINQ. 我使用LINQ在C#中编写了一些查询。 After a while, I started using Haskell a little bit, which is a functional programming language (a not so popular one), and for me it seems that both of them are almost the same thing. 过了一会儿,我开始使用Haskell一点点,这是一种函数式编程语言(一种不那么受欢迎的语言),而对我来说似乎它们几乎都是一样的。 But I am unsure about this. 但我不确定这一点。 Please, if someone has used them more than me, could they tell me if they are almost the same thing regarding principles in programming ? 如果有人比我更多地使用它们,他们能不能告诉我它们在编程原理上是否几乎相同?

Also, could LINQ be considered functional programming ? LINQ可以被认为是函数式编程吗?

Thanks. 谢谢。

for me it seems that both of them are almost the same thing. 对我而言,似乎两者几乎都是一回事。 But I am unsure about this. 但我不确定这一点。 Please, if someone has used them more than me, could they tell me if they are almost the same thing regarding principles in programming ? 如果有人比我更多地使用它们,他们能不能告诉我它们在编程原理上是否几乎相同?

Yes, the design of LINQ query comprehensions was heavily influenced by the design of Haskell. 是的,LINQ查询理解的设计很大程度上受到Haskell设计的影响。 Haskell expert Erik Meijer was on the C# language design committee when we designed LINQ; 当我们设计LINQ时,Haskell专家Erik Meijer加入了C#语言设计委员会; his insights were very valuable. 他的见解非常宝贵。 (I joined the design team at the end of this process, so unfortunately I did not get to participate in all the interesting twists and turns the design went through over the years; it started being much more traditional OO than it ended up!) (我在这个过程结束时加入了设计团队,所以不幸的是我没有参与所有有趣的曲折,这些年来设计经历了多次;它开始变得更加传统OO而不是最终!)

If you've recently done a serious exploration into Haskell then you're probably becoming familiar with the idea of a monad. 如果你最近对Haskell进行了认真的探索,那么你可能已经熟悉了monad的想法。 The LINQ syntax is designed specifically to make operations on the sequence monad feel natural, but in fact the implementation is more general; LINQ语法专门设计用于使序列monad上的操作感觉自然,但实际上实现更通用; what C# calls "SelectMany" is a slightly modified form of the "Bind" operation on an arbitrary monad. C#调用“SelectMany”是对任意monad的“绑定”操作的略微修改形式。 You can actually use query comprehension with any monad, as my colleague Wes describes here , but doing so looks pretty weird and I recommend against it in production code. 你可以使用任何monad实际使用查询理解, 正如我的同事Wes在这里描述的那样 ,但这样做看起来很奇怪,我建议在生产代码中反对它。

Also, could LINQ be considered functional programming ? LINQ可以被认为是函数式编程吗?

Yes, LINQ is heavily influenced by ideas from functional programming. 是的,LINQ深受功能编程思想的影响。 It is designed to treat functions as first-class objects, to emphasize calculation over side effects, and so on. 它旨在将函数视为一等对象,强调对副作用的计算等。

It may be worthwhile to take a look at Erik Meijer Lecture . 看看Erik Meijer Lecture可能是值得的。 Here's the description for it. 这是对它的描述。

We kick off C9 Lectures with a journey into the world of Functional Programming with functional language purist and high priest of the lambda calculus, Dr. Erik Meijer (you can thank Erik for many of the functional constructs that have shown up in languages like C# and VB.NET. When you use LINQ, thank Erik in addition to Anders). 我们带着功能性语言纯粹主义者和lambda演算的大祭司Erik Meijer博士进入C9讲座,进入功能编程世界(你可以感谢Erik的许多功能结构,这些结构已经出现在像C#这样的语言中VB.NET。当你使用LINQ时,感谢Erik除了Anders之外。

It is true that C# has gained some aspects of functional programming. 确实,C#已经获得了函数式编程的某些方面。 First and foremost is the lambda statement, implemented as an anonymous delegate. 首先是lambda语句,作为匿名委托实现。 You now no longer need to define a method as belonging to an object, and you can define a method in a similar fashion as any other variable. 您现在不再需要将方法定义为属于对象,并且您可以以与任何其他变量类似的方式定义方法。 This allows for many functional-type constructs: 这允许许多功能类型的构造:

var mult = (a,b)=>a*b;
var square = (a)=>Math.Pow(a,2);
var multandsquare = (a,b)=>square(mult(a,b));

//None of the above give a lick about what a and b really are, until...
multandsquare(5,3); //== 225

The basic paradigm of functional programming - that basically anything you can tell a computer to do can be told in terms of higher-order functions - can be applied to C# programs, especially now that C# actually has higher-order functions. 函数式编程的基本范例 - 基本上可以告诉计算机做的任何事情都可以用高阶函数来表示 - 可以应用于C#程序,特别是现在C#实际上具有更高阶函数。 The compiler will force you to have at least one class with at least one public main method (that's just how OO works), but from that point you can define pretty much everything only in terms of functions (def: a subclass of "methods" that take N parameters and produce 1 output without "side effects") instantiated as lambdas. 编译器将强制你至少有一个类至少有一个公共main方法(这就是OO的工作方式),但从那时起你只能根据函数来定义几乎所有东西(def:“方法”的子类)采用N个参数并产生1个输出而没有“副作用”)实例化为lambdas。

Linq does have some functional structure. Linq确实有一些功能结构。 Basically its method-chain paradigm is an example of monadic processing, which is how sequences of operations are structured through operation encapsulation in functional languages (and now in C#). 基本上它的方法链范例是monadic处理的一个例子,它是通过操作封装在函数语言中(现在在C#中)来构造操作序列的。 Calling a Linq method on an IEnumerable returns another IEnumerable, which is really a different concrete class which contains a handle to the source Enumerable and some lambda to perform. 在IEnumerable上调用Linq方法会返回另一个IEnumerable,它实际上是一个不同的具体类,它包含源Enumerable的句柄和一些要执行的lambda。 You can replicate it in a functional language very simply; 您可以非常简单地用函数式语言复制它; it's a tuple of the source (itself a tuple of the current element and everything else) and a function to perform that transforms the source tuple into the result, one element at a time (which in Linq, as it would be implemented in a functional language, is a nesting of some operation into a defined "rollup" function). 它是源的元组(本身是当前元素和其他所有元素的元组)和一个执行的函数,它将源元组转换为结果,一次一个元素(在Linq中,因为它将在函数中实现) language,是将某些操作嵌套到定义的“汇总”函数中)。 Call a method that must produce an actual answer (a concrete typed result), and all these functions are evaluated sufficiently to produce the desired result. 调用必须产生实际答案的方法(具体的类型结果),并充分评估所有这些函数以产生所需的结果。

Also could LINQ be considered functional programming? LINQ也可以被认为是函数式编程吗?

LINQ implies a functional style in that it resembles, for instance, a SQL select, which is read-only in principle. LINQ意味着一种功能风格,因为它类似于SQL select,原则上它是只读的。 However, because the LINQ clauses in .NET are implemented by plain-old CLR routines, there is nothing to prevent LINQ expressions from modifying state. 但是,因为.NET中的LINQ子句是由普通的CLR例程实现的,所以没有什么可以阻止LINQ表达式修改状态。

Haskell, on the other hand, is a "pure" functional language, so expressions cannot modify global state. 另一方面,Haskell是一种“纯粹的”函数式语言,因此表达式无法修改全局状态。 Although it is possible to perform IO and graphics operations, these are carried out using a very different idiom from anything in .NET — including F#, which lets you drop into a procedural style more or less transparently. 尽管可以执行IO和图形操作,但这些操作使用与.NET中任何东西完全不同的惯用语来执行 - 包括F#,它允许您或多或少透明地使用程序样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM