在数学方面,F# 真的比 C# 更好吗? [英] Is F# really better than C# for math?

查看:27
本文介绍了在数学方面,F# 真的比 C# 更好吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尽管是非托管语言,但在实现数学方面,F# 真的比 C# 更好吗?如果是这样,为什么?

Unmanaged languages notwithstanding, is F# really better than C# for implementing math? And if that's the case, why?

推荐答案

我认为大部分重点已经被其他人提到了:

I think most of the important points were already mentioned by someone else:

  1. F# 让您以数学家的思维方式解决问题
  2. 多亏了高阶函数,您可以使用更简单的概念来解决难题
  3. 默认情况下一切都是不可变的,这使程序更易于理解(也更易于并行化)

确实可以在 C# 3.0 中使用某些 F# 概念,但存在限制.您不能使用任何递归计算(因为 C# 没有尾递归),这就是您以函数/数学方式编写原始计算的方式.此外,在 C# 中编写复杂的高阶函数(将其他函数作为参数)很困难,因为您必须显式编写类型(而在 F# 中,类型是推断的,但也会自动泛化,因此您不必显式地使一个泛型函数).

It is definitely true that you can use some of the F# concepts in C# 3.0, but there are limitations. You cannot use any recursive computations (because C# doesn't have tail-recursion) and this is how you write primitive computations in functional/mathematical way. Also, writing complex higher order functions (that take other functions as arguments) in C# is difficult, because you have to write types explicitly (while in F#, types are inferred, but also automatically generalized, so you don't have to explicitly make a function generic).

此外,我认为 Marc Gravell 的以下观点不是有效的反对意见:

Also, I think the following point from Marc Gravell isn't a valid objection:

从维护的角度来看,我认为适当命名的属性等比元组和头/尾列表更易于使用(在整个生命周期内),但这可能只是我.

From a maintenance angle, I'm of the view that suitably named properties etc are easier to use (over full life-cycle) than tuples and head/tail lists, but that might just be me.

这当然是真的.然而,F# 的伟大之处在于您可以开始使用元组编写程序 &头/尾列表,然后在开发过程中将其转换为使用 .NET IEnumerables 和带有属性的类型的程序(这就是我相信典型 F# 程序员的工作方式*).元组等和 F# 交互式开发工具为您提供了一种快速原型解决方案的好方法(并且在进行数学运算时,这是必不可少的,因为当您寻找最佳解决方案时,大多数开发只是试验).获得原型后,您可以使用简单的源代码转换将代码封装在 F# 类型中(也可以从 C# 中用作普通类).F# 还为您提供了很多后期优化代码的方法.

This is of course true. However, the great thing about F# is that you can start writing the program using tuples & head/tail lists and later in the development process turn it into a program that uses .NET IEnumerables and types with properties (and that's how I believe typical F# programmer works*). Tuples etc. and F# interactive development tools give you a great way to quickly prototype solutions (and when doing something mathematical, this is essential because most of the development is just experimenting when you're looking for the best solution). Once you have the prototype, you can use simple source code transformations to wrap the code inisde an F# type (which can also be used from C# as an ordinary class). F# also gives you a lot of ways to optimize the code later in terms of performance.

这为您提供了易于使用的语言(例如 Python)的好处,许多人将其用于原型设计阶段.但是,一旦使用高效的语言(例如 C++ 或 C#)完成原型设计,您就不必稍后重写整个程序,因为 F# 既易于使用"又高效",并且您可以流畅地切换介于这两种风格之间.

This gives you the benefits of easy to use langauges (e.g. Python), which many people use for prototyping phase. However, you don't have to rewrite the whole program later once you're done with prototyping using an efficient language (e.g. C++ or perhaps C#), because F# is both "easy to use" and "efficient" and you can fluently switch between these two styles.

(*) 我也在我的函数式编程书籍中使用了这种风格.

(*) I also use this style in my functional programming book.

这篇关于在数学方面,F# 真的比 C# 更好吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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