简体   繁体   English

F#可以教我很好的函数式编程实践吗?

[英]Can F# teach me good functional programming practices?

I am planning to immerse myself in the functional programming paradigm and make the thought process second nature to me. 我打算让自己沉浸在函数式编程范式中,让思维过程成为我的第二天性。 The .Net/Mono is the only framework/objectmodel I am familiar with and I dont want to learn any other type system. .Net / Mono是我熟悉的唯一框架/对象模型,我不想学习任何其他类型的系统。 That makes F# an obvious consideration since the burden of learning a new type system will not be a distracting factor. 这使得F#显而易见,因为学习新型系统的负担不会是一个令人分心的因素。

But my concern is about the language itself. 但我关心的是语言本身。 Will it be as good as any other classical functional programming language (eg Haskell) in drilling FP into my skull ? 在将FP钻入我的头骨时,它是否会像任何其他经典函数编程语言(例如Haskell)一样好?

Yes, it can. 是的,它可以。 Especially helpful and germane here is Real World Functional Programming which comes at FP from both the C# and especially the F# perspective. 这里特别有用和密切关注的是真实世界的功能编程 ,它来自C#,特别是F#视角的FP。 You'll love it. 你会爱上它的。

I think the benefit of learning functional programming with F# (especially if you already have experience with C# and .NET) is that you'll find it a lot easier to create some fun project. 我认为使用F#学习函数式编程的好处(特别是如果你已经有过C#和.NET的经验)是你会发现创建一些有趣的项目要容易得多。

  • One reason is that you're already familiar with .NET libraries (that are easy to access from F#) 一个原因是你已经熟悉.NET库(易于从F#访问)
  • Second reason is that F# is not pure, so you can start writing code that works and then improve it to make it more functional. 第二个原因是F#不是纯粹的,所以你可以开始编写有效的代码,然后对其进行改进以使其更具功能性。

F# supports multiple paradigms, but it is mainly a functional language (compared to Python or C# that support multiple paradigms too, but aren't primarilly functional). F#支持多种范例,但它主要是一种函数式语言(与支持多种范式的Python或C#相比,但不具备主要功能)。 This makes it easier to force yourself to use the functional style. 这样可以更轻松地强制自己使用功能样式。 It is easier to avoid imperative patterns, because they are more difficult to write in F# (and the code looks worse). 避免命令式模式更容易,因为它们更难以用F#编写(并且代码看起来更糟)。

I think that once you "get" the basics functional programming, it will be easier to learn other functional languages if you'll still be interested. 我认为一旦你“掌握”基础函数编程,如果你仍然感兴趣的话,学习其他函数式语言会更容易。 It is definitely nice to know a little bit about Haskell or Scheme, because they are more radical (in some ways). 对Haskell或Scheme有一点了解是非常好的,因为它们更激进(在某些方面)。

If you're good at understadning concepts without actually using them in some real projects then starting with some classic language using some classic book may be a good option too (eg Haskell School of Expression or Structure and Interpretation of Computer Programs ). 如果你擅长在没有在一些真实项目中实际使用它们的情况下擅长概念,那么从一些经典语言开始使用一些经典书籍也可能是一个不错的选择(例如Haskell School of ExpressionStructure and Interpretation of Computer Programs )。 I don't mean this in any negative sense - I personally quite like reading technical books without trying examples, because I can still take the interesting concepts from just reading (and for practice, there is always Google). 我并不是说任何消极的意义 - 我个人非常喜欢阅读技术书籍而不尝试例子,因为我仍然可以从阅读中获取有趣的概念(对于实践,总有谷歌)。 Of course, you can write some code in Haskell or Scheme, but as a .NET programmer, you'll probably find writing F# code more fun... 当然,您可以在Haskell或Scheme中编写一些代码,但作为.NET程序员,您可能会发现编写F#代码更有趣......

Yes, it will definitely help you learn functional programming. 是的,它肯定会帮助你学习函数式编程。 F# does support object-oriented programming too, so the switch should be easier. F#也支持面向对象的编程,因此切换应该更容易。 If you want to move to a full functional language eventually, you could, but I actually find the combination of OO and FP better than OO or FP alone (I'm a Scala programmer, which is like F#). 如果你想最终转向一个完整的功能语言,你可以,但我实际上发现OO和FP的组合比OO或FP更好(我是Scala程序员,就像F#)。

F#, in many regards, may be better than most "other classical functional programming language[s]". 在许多方面,F#可能比大多数“其他经典函数编程语言[s]”更好。 Since you already know .NET, you'll be able to fully leverage all your .NET knowledge while picking up two useful things from F#: 既然您已经了解.NET,那么您将能够充分利用所有.NET知识,同时从F#中获取两个有用的东西:

  1. The functional programming paradigm. 函数式编程范例。 That is, how to program in the style of FP. 也就是说,如何以FP的风格进行编程。 You can use that in any language. 您可以使用任何语言。 I often learn towards FP in my C# code, because I find it to be a much more maintainable and reusable way to write software. 我经常在C#代码中学习FP,因为我发现它是一种更易于维护和可重用的编写软件的方法。

  2. One language's functional programming syntax. 一种语言的函数式编程语法。 F# is very similar to OCAML, and the ML family makes up one of the 3 or so major dialects of FP languages. F#与OCAML非常相似,ML系列构成了FP语言的3种主要方言之一。 The other two biggies are the Miranda family (of which Haskell is the major modern representative) and the LISP family (where Common Lisp and Scheme are the two big reps). 另外两个大佬是米兰达家族(其中Haskell是主要的现代代表)和LISP家族(Common Lisp和Scheme是两个大代表)。

In F#, it might help you to make the distinction very clearly between F# types and .NET types. 在F#中,它可以帮助您在F#类型和.NET类型之间进行非常清楚的区分。 Records, Tuples, and Discriminated Unions are (pretty much) purely F# things, and they're used in almost all functional languages. 记录,元组和歧视联盟(几乎)纯粹是F#的东西,它们几乎用于所有功能语言。 Use these a lot if you want to learn FP. 如果你想学习FP,可以使用这些。 Classes, structs, and interfaces are also in there, but those are .NET concepts. 类,结构和接口也在那里,但那些是.NET概念。 Use them as needed, but try to avoid them in general, lest you end up writing "C# with a goofy syntax" instead of idiomatic F#. 根据需要使用它们,但一般要尽量避免使用它们,以免你最终写出“用愚蠢的语法编写C#”而不是惯用的F#。

The things you'll really miss out on from Haskell are true purity (eh, not that big of a deal), lazy evaluation by default (a big change), and type classes. 你真正错过Haskell的东西是真正的纯度(呃,不是那么大的交易),默认的懒惰评估(一个很大的改变)和类型类。 You can do lazy eval in F#, but you have to use the explicit Lazy<> classes to get it. 你可以在F#中做懒惰的eval,但你必须使用显式的Lazy <>类来获取它。 Having lazy eval everywhere is one of the really unique and powerful things in Haskell, but it's not a strict FP concept. 懒惰的eval无处不在是Haskell中真正独特而强大的东西之一,但它并不是一个严格的FP概念。 Type Classes are very powerful, but you can get by without them for most real world situations. 类型类非常强大,但是对于大多数现实世界的情况,你可以在没有它们的情况下使用它们。 They're just a higher level of abstraction that cuts down on some boiler plate and repetition. 它们只是一个更高层次的抽象,减少了一些锅炉板和重复。

True purity is usually more academic. 真正的纯度通常更具学术性。 It has certain advantages, but if you're already used to .NET and impure techniques, you'll mostly just find it a pain to work around until you've fully bought into the FP mindset. 它有一定的优势,但是如果你已经习惯了.NET和不纯的技术,那么在你完全融入FP思维模式之前,你大多只会觉得很难解决。

Functional Programming paradigm? 功能编程范例? Haskell springs immediately to mind. 哈斯克尔立刻想到了。 By the way, there's no inherit framework in Haskell. 顺便说一句,Haskell中没有继承框架 As far as I know, your framework is a terminal window and a programming editor. 据我所知,您的框架是一个终端窗口和一个编程编辑器。

If you want a functional programming language that's in your .NET framework, you'll have to stick with F#. 如果你想要一个.NET框架中的函数式编程语言,你将不得不坚持使用F#。

IronPython, but the way, is not a functional programming language. IronPython,但方式,不是一种函数式编程语言。

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

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