设计大型功能程序的方法

发布于 2024-12-02 17:12:27 字数 69 浏览 1 评论 0原文

我已经阅读了大量有关函数式语言的文章,并且可能想尝试用 F# 重写我的应用程序的某些部分。从外向内设计好还是从内向外设计好?

I've been reading lots on functional languages and would like to maybe play with re-writing some parts of my application in F#. Is it better to design from the outside in or the inside out?

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

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

发布评论

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

评论(5

羞稚 2024-12-09 17:12:27

关于 FP 主题的有影响力的著作之一为什么函数式编程很重要 可以被视为直接答案:

我们的分解能力
将问题分解成各个部分直接取决于我们粘合解决方案的能力
一起。为了支持模块化编程,语言必须提供良好的
胶水。函数式编程语言提供了两种新的粘合剂——
高阶函数和惰性求值。使用这些胶水可以进行模块化
以新的、有用的方式编写程序,我们已经展示了几个这样的例子。
更小、更通用的模块可以更广泛地重用,从而简化后续工作
编程。这解释了为什么函数式程序要小得多并且
比传统的更容易编写。它还提供了功能目标
程序员的目标。如果程序的任何部分混乱或复杂,
程序员应该尝试将其模块化并通用化各个部分。他或
她应该期望使用高阶函数和惰性求值作为工具
为了做到这一点。

我还建议您查看 InfoQ 上 John Hughes 的当代访谈

One of the influental works on the subject of FP Why Functional Programming Matters by John Hughes can be considered as a direct answer:

Our ability to decompose
a problem into parts depends directly on our ability to glue solutions
together. To support modular programming, a language must provide good
glue. Functional programming languages provide two new kinds of glue —
higher-order functions and lazy evaluation. Using these glues one can modularize
programs in new and useful ways, and we’ve shown several examples of this.
Smaller and more general modules can be reused more widely, easing subsequent
programming. This explains why functional programs are so much smaller and
easier to write than conventional ones. It also provides a target for functional
programmers to aim at. If any part of a program is messy or complicated, the
programmer should attempt to modularize it and to generalize the parts. He or
she should expect to use higher-order functions and lazy evaluation as the tools
for doing this.

I would also recommend looking at contemporary interview with John Hughes in InfoQ.

万人眼中万个我 2024-12-09 17:12:27

真正帮助我了解函数式编程的一篇文章是:
http://prog21.dadgum.com/23.html
这是有关如何使用函数式编程技术创建吃豆人克隆的教程。我什至不认识他使用的语言(可能是 Erlang),但是这个解释帮助我理解了我在Scheme 中重写一个程序时所使用的正确思维方式。

One article that really helped me come to terms with functional programming is this one:
http://prog21.dadgum.com/23.html
It is a tutorial for how to create a Pac-Man clone using functional-programming techniques. I don't even recognize the language he used (it might have been Erlang,) but the explanation helped me understand the correct mindset which I used when rewriting one of my programs in Scheme.

山川志 2024-12-09 17:12:27

我根本不是这个主题的专家,但这里有一个关于这个主题的文章的链接,作者是“我们”自己的 布莱恩

函数式编程如何影响代码的结构?

以及 SO & 的一些问题程序员(顺便说一句,我怀疑这个问题更适合)

I'm not an expert at all on this subject, but here is a link to an article on this subject, by 'our' very own Brian:

How does functional programming affect the structure of your code?

And some questions from SO & Programmers (where - incidentally - I suspect this question would be a better fit)

身边 2024-12-09 17:12:27

不确定我是否理解“由内而外”/“由外而内”的含义,但 FP 在“小方面”(在功能级别)最有用。面向对象可能仍然是组织大型项目的最佳方式。处理与您的业务领域相关的对象是最简单的。

Not sure I understand what is meant by "inside out" / "outside in", but FP is most useful "in the small" -- at the function level. OO is probably still the best way to organize projects in the large. It's easiest to deal with objects that correlate with your business domain.

鹿! 2024-12-09 17:12:27

我猜你所说的从外到内是指从上到下的方法,而从内到外是指从下到上的方法。如果这就是你的意思,那么 FP 更适合自下而上的方法,即由内而外。

在设计 FP 程序时,请考虑使用以下方法解决问题:

  • 数据结构:您的应用程序需要处理的所有数据以及
    哪种数据结构最适合表示该数据。
  • 函数:函数做一件事(并且正确地做)。
  • 组合:创建新函数,通过使用高阶函数和组合等技术组合其他函数来实现其目标。
  • 其他更先进和抽象的 FP 技术,如 Monad 等。

所以基本上你从底层开始,为每个原子问题编写一个函数,然后当你在设计中向上移动时,你使用这些原子函数通过组合来创建更高级别的函数。

请记住,始终考虑使用抽象和这些抽象的组合来解决问题,而不是像在命令式编程中那样按顺序思考。

I guess by outside in you mean top to bottom approach and by inside out you mean from bottom to top. If this is what you mean than FP is more suited towards bottom up approach i.e inside out.

While designing FP programs think about solving the problem using:

  • Data Structure : What all data your application needs to handle and
    which data structure is most suitable to represent that data.
  • Function : A function does one thing (and do it correctly).
  • Composition : Create new functions which achieve their goals by composing other functions using technique like higher order functions and compositions.
  • Other more advance and abstract FP techniques like Monads etc.

So basically you start from bottom, for each atomic problem you write a function and then as you move upward in the design you use these atomic function to create functions at higher level using composition.

Remember always think of solving problem using abstractions and composition of those abstraction rather than thinking sequentially as you would do in a imperative programming.

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