Haskell 和 F# 之间的主要区别是什么?
I've searched on the Internet for comparisons between F# and Haskell but haven't found anything really definitive. What are the primary differences and why would I want to choose one over the other?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Haskell 是一种“纯”函数式语言,而 F# 具有命令式/OO 和函数式语言的特点。 Haskell 还具有惰性求值,这在函数式语言中相当罕见。
这些事情意味着什么? 纯函数式语言意味着没有副作用(或者在调用函数时共享状态发生变化),这意味着如果您调用 f(x),除了从函数返回值之外不会发生任何其他事情,例如控制台输出、数据库输出、全局或静态变量的更改..虽然 Haskell 可以有非纯函数(通过 monad),但它必须通过声明“显式”暗示。
纯函数式语言和“无副作用”编程最近越来越受欢迎,因为它非常适合多核并发,因为没有共享状态而不是无数的锁和锁,更难出错。 信号量。
惰性求值是指直到绝对必要时才对函数求值。 这意味着许多不必要的操作可以被避免。 在基本的 C# if 子句中考虑这一点,如下所示:
如果
IsSomethingTrue()
为 false,则永远不会计算AnotherThingTrue()
方法。虽然 Haskell 是一种令人惊叹的语言,但 F#(目前)的主要好处是它位于 CLR 之上。 这使其适合多语言编程。 有一天,您可以使用 ASP.net MVC 编写 Web UI,使用 C# 编写业务逻辑,使用 F# 编写核心算法,使用 Ironruby 编写单元测试......所有这些都在 .Net 框架中。
收听 Simon Peyton Jones 的软件工程广播,了解有关 Haskell 的更多信息:第 108 集:Simon Peyton Jones 谈函数式编程和 Haskell
Haskell is a "pure" functional language, where as F# has aspects of both imperative/OO and functional languages. Haskell also has lazy evaluation, which is fairly rare amongst functional languages.
What do these things mean? A pure functional language, means there are no side effects (or changes in shared state, when a function is called) which means that you are guaranteed that if you call f(x), nothing else happens besides returning a value from the function, such as console output, database output, changes to global or static variables.. and although Haskell can have non pure functions (through monads), it must be 'explicitly' implied through declaration.
Pure functional languages and 'No side effect' programming has gained popularity recently as it lends itself well to multi core concurrency, as it is much harder to get wrong with no shared state, rather than myriad locks & semaphores.
Lazy evaluation is where a function is NOT evaluated until it is absolutely necessary required. meaning that many operation can be avoided when not necessary. Think of this in a basic C# if clause such as this:
If
IsSomethingTrue()
is false thenAnotherThingTrue()
method is never evaluated.While Haskell is an amazing language, the major benefit of F# (for the time being), is that it sits on top of the CLR. This lends it self to polyglot programming. One day, you may write your web UI in ASP.net MVC, your business logic in C#, your core algorithms in F# and your unit tests in Ironruby.... All amongst the the .Net framework.
Listen to the Software Engineering radio with Simon Peyton Jones for more info on Haskell: Episode 108: Simon Peyton Jones on Functional Programming and Haskell
大差异:
相似之处比差异更重要。 基本上,如果您已经使用 .NET,则应该使用 F#,否则应该使用 Haskell。 此外,面向对象和惰性意味着 F# 更接近您(可能)已经知道的内容,因此可能更容易学习。
平台:Haskell 有自己的运行时,F# 使用.NET。 我不知道性能差异是什么,尽管我怀疑优化前的平均代码大致相同。 如果您需要 .NET 库,F# 具有优势。
面向对象:F# 具有 OO,并且非常小心地确保 .NET 类易于使用,即使您的代码不是 OO。 Haskell 有类型类,可以让你以一种奇怪的方式做类似 OO 的事情。 它们就像 Ruby mixin 与 Common Lisp 泛型函数的交叉。 它们有点像 Java/C# 接口。
懒惰:Haskell 很懒,而 F# 则不然。 懒惰可以带来一些不错的技巧,并使一些看起来很慢的事情实际上执行得很快。 但我发现很难猜测我的代码的运行速度。 两种语言都允许您使用另一种模型,您只需在代码中明确说明即可。
细微差别:
Big differences:
The similarities are more important than the differences. Basically, you should use F# if you are on .NET already, Haskell otherwise. Also, OO and laziness mean that F# is closer to what you (probably) already know, so it is probably easier to learn.
Platform : Haskell has its own runtime, F# uses .NET. I don't know what the performance difference is, although I suspect the average code is about the same before optimisation. F# has the advantage if you need the .NET libraries.
Object orientation : F# has OO, and is very careful to make sure that .NET classes are easy to use even if your code isn't OO. Haskell has type classes which let you do something like OO, in a weird sort of way. They are like Ruby mixins crossed with Common Lisp generic functions. They're a little like Java/C# interfaces.
Laziness : Haskell is lazy, F# is not. Laziness enables some nice tricks and makes some things that look slow actually execute fast. But I find it a lot harder to guess how fast my code will run. Both languages let you use the other model, you just have to be explicit about it in your code.
Minor differences:
F# 是 ML 语言系列的一部分,与 OCaml 非常接近。 您可能想阅读有关 Haskell 和 OCaml 之间的差异的讨论。
F# is part of the ML family of languages and is very close to OCaml. You may want to read this discussion on the differences between Haskell and OCaml.
一个主要的区别,可能是纯粹性的结果,但我很少看到提及,是单子的普遍使用。 正如经常指出的那样,单子可以用大多数任何语言构建,但是当它们在整个库中普遍使用并且您自己使用它们时,生活会发生很大变化。
Monad 提供了在其他语言中以更有限的方式看到的东西:流控制的抽象。 它们是做各种事情的非常有用和优雅的方式,一年的 Haskell 完全改变了我的编程方式,就像多年前从命令式编程转向面向对象编程改变了它一样,或者,很久以后,使用高阶函数做到了。
不幸的是,在这样的空间中无法提供足够的理解来让您了解其中的差异。 事实上,无论写多少文章都无法做到这一点。 您只需花费足够的时间学习和编写代码即可获得真正的理解。
此外,当您与 .NET 平台/库交互时,F# 有时可能会变得功能稍差或更尴尬(从函数式编程的角度来看),因为这些库显然是从 OO 的角度设计的。
因此,您可能会这样考虑您的决定:您是否希望尝试其中一种语言以获得快速、相对较小的改进,或者您是否愿意投入更多时间并为更大的事情获得较少的直接利益长期来看。 (或者,至少,如果你没有得到更大的东西,那么可以轻松地快速切换到另一个?)如果是前者,F# 是你的选择,如果是后者,则 Haskell。
其他一些不相关的点:
Haskell 的语法稍微好一点,这并不奇怪,因为 Haskell 的设计者非常了解 ML。 然而,F# 的“轻量”语法在改进 ML 语法方面大有帮助,因此两者之间并不存在巨大差距。
就平台而言,F#当然是.NET; 我不知道这在 Mono 上效果如何。 GHC 使用自己的运行时编译为机器代码,在 Windows 和 Unix 下都运行良好,这与 .NET 的情况相同,例如 C++。 在某些情况下,这可能是一个优势,特别是在速度和较低级别的机器访问方面。 (例如,我在 Haskell/GHC 中编写 DDE 服务器没有任何问题;我认为您不能用任何 .NET 语言来做到这一点,而且无论如何,MS 当然不希望您这样做。)
A major difference, which is probably a result ofthe purity but I less see mentioned, is the pervasive use of monads. As is frequently pointed out, monads can be built in most any language, but life changes greatly when they are used pervasively throughout the libraries, and you use them yourself.
Monads provide something seen in a much more limited way in other languages: abstraction of flow control. They're incredibly useful and elegant ways of doing all sorts of things, and a year of Haskell has entirely changed the way I program, in the same way that moving from imperative to OO programming many years ago changed it, or, much later, using higher-order functions did.
Unfortunately, there's no way in a space like this to provide enough understanding to let you see what the difference is. In fact, no amount of writing will do it; you simply have to spend enough time learning and writing code to gain a real understanding.
As well, F# sometimes may become slightly less functional or more awkward (from the functional programming point of view) when you interface with the .NET platform/libraries, as the libraries were obviously designed from an OO point of view.
So you might consider your decision this way: are you looking to try out one of these languages in order to get a quick, relatively small increment of improvement, or are you willing to put in more time and get less immediate benefit for something bigger in the long term. (Or, at least, if you don't get something bigger, the easy ability to switch to the other quickly?) If the former, F# is your choice, if the latter, Haskell.
A couple of other unrelated points:
Haskell has slightly nicer syntax, which is no suprise, since the designers of Haskell knew ML quite well. However, F#'s 'light' syntax goes a long way toward improving ML syntax, so there's not a huge gap there.
In terms of platforms, F# is of course .NET; how well that will work on Mono I don't know. GHC compiles to machine code with its own runtime, working well under both Windows and Unix, which compares to .NET in the same way, that, say, C++ does. This can be an advantage in some circumstances, especially in terms of speed and lower-level machine access. (I had no problem writing a DDE server in Haskell/GHC, for example; I don't think you could do that in any .NET language, and regardless, MS certainly doesn't want you doing that.)
嗯,我想说的一个主要优点是 F# 针对 .NET 平台进行编译,这使得在 Windows 上部署变得很容易。 我见过解释如何使用 F# 与 ASP.NET 结合构建 Web 应用程序的示例;-)
另一方面,Haskell 已经存在了很长时间,所以我认为真正的该语言专家群体是大很多。
对于 F#,到目前为止我只看到了一种真正的实现,即奇点概念证明操作系统。 我见过更多 Haskell 的现实世界实现。
Well, for one I'd say a main advantage is that F# compiles against the .NET platform which makes it easy to deploy on windows. I've seen examples which explained using F# combined with ASP.NET to build web applications ;-)
On the other hand, Haskell has been around for waaaaay longer, so I think the group of people who are real experts on that language is a lot bigger.
For F# I've only seen one real implementation so far, which is the Singularity proof of concept OS. I've seen more real world implementations of Haskell.