使用 F# 的纯函数式

发布于 2024-09-26 18:05:27 字数 203 浏览 8 评论 0原文

是否可以强制 F# 表现得像 Haskell 这样的纯函数式语言?也许使用一些编译器指令?

PS:由于我来自 C/C++ 背景,所以我想强迫自己学习函数式编程而不学习 Haskell :)

Is it possible to force F# to behave like a pure functional language like Haskell? Maybe using some compiler directives?

PS: since I come from a C/C++ background, I want to force myself to learn functional programming without learning Haskell :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

聊慰 2024-10-03 18:05:27

在 F# 中你不能强迫这种行为,但正如 Brian 所说,纪律是你最好的朋友。例如,不要使用 mutableforwhile 循环、ref 关键字等。还要坚持使用纯粹不可变的数据结构(可区分联合、列表、元组、映射等)。如果您需要在某些时候进行 IO,请架构您的程序,以便将它们与纯功能代码分开。

不要忘记函数式编程就是限制和隔离副作用。

You can't force this behavior in F#, but as Brian said discipline is your best friend. E.g. don't use mutable, for or while loops, ref keywords, etc. Also stick with purely immutable data structures (discriminated union, list, tuple, map, etc). If you need to do IO at some point, architect your program so that they are separated from your purely functional code.

Don't forget functional programming is all about limiting and isolating side-effects.

乖乖公主 2024-10-03 18:05:27

不,抱歉。你只需要运用纪律。

Nope, sorry. You just have to use discipline.

离线来电— 2024-10-03 18:05:27

如果您像我一样,如果您不强迫自己使用 Haskell 而不是 F# 并尽可能地惯用 Haskell,那么您可能会避免使用“好东西”。使用代数数据类型而不是对象、学会热爱懒惰、拥抱 monad 等等更多的是 Haskell 的核心部分,并且可以说是纯函数式编程的一些细节(在我看来)。

F# 在很多方面都没有那么陡峭的学习曲线,但听起来您学习它是为了好玩,所以为什么不挑战自己呢?我可以证明,使用 Haskell 后转向 F# 可以让您更好地了解 F# 通常应该如何使用。

值得深思。

If you're anything like me, you will probably avoid the 'good stuff' if you don't force yourself to use Haskell instead of F# and use Haskell as idiomatically as you can. Using algebraic data types instead of objects, learning to love laziness, embracing the monad, and more are much more of a core part of Haskell, and arguably some of the finer points (in my opinion) of pure functional programming.

F# doesn't have as steep of a learning curve in many ways, but you sound like you are learning it for fun, so why not challenge yourself anyways? I can attest that moving to F# after using Haskell can give you a much better feel for how F# in general ought to be used anyways.

Food for thought.

伴我心暖 2024-10-03 18:05:27

Haskell 的纯函数方面是该语言的基础。你不能只是在语言之间移植它。它导致了重大的设计决策——例如,Haskell 的纯函数性质导致了 IO monad 的发明。 F# 没有这样的用于状态计算的“逃生阀”。

此外,学习使用支持函数式编程但并不绝对强制执行它的语言进行编程实际上可能是有启发性的。许多人对 Haskell 的单独设计决策(例如 IO monad,再次提到这个大例子)和函数式编程的一般工作方式感到困惑。

简而言之:不,你不能这样做。但你可以做的是非常仔细地观察并质疑你所做的一切涉及维护状态和排序操作的事情,以确保你没有遗漏更纯粹的抽象。

The purely functional aspects of Haskell are fundamental to the language. You can't just transplant it between languages. It leads to major design decisions — for instance, Haskell's purely functional nature led to the invention of the IO monad. F# does not have such an "escape-valve" for stateful computation.

Also, learning to program in a language that supports functional programming but doesn't absolutely enforce it might actually be instructive. Many people get confused between Haskell's individual design decisions (e.g. the IO monad, to hit the big example again) and how functional programming works in general.

In short: No, you can't this. But what you can do is watch very carefully and question everything you do that involves maintaining state and sequencing operations to make sure that there isn't a more pure abstraction you're missing.

痴骨ら 2024-10-03 18:05:27

不可以。目前 F# 编译器无法执行此检查。

为什么不开始编写一些 F# 程序来学习函数式编程呢?我认识一些人,他们先学习了 ML/Ocaml/F#,然后才转向 Haskell。

那么你就会对纯洁有更好的理解。 (如果您之前确实没有接触过任何函数式编程语言,那么您对纯函数式的理解可能很肤浅。)

No. Currently F# compiler cannot do this checking.

Why not just start writing some F# program to learn functional programming? I know a few people who learned ML/Ocaml/F# first and then moved to Haskell.

Then you will have a better understanding of purity. (If you really haven't touched any functional programming language before, your understanding of pure functional could be superficial. )

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文