流程图函数式编程语言
流程图。这种古老的做法已经沿用了一千多年,强加给我们这些贫穷的学生,没有任何用处(或者我认为是这样)。它可能适用于命令式、顺序运行的语言,但是我心爱的函数式编程呢?
遗憾的是,我被迫为我的程序(用 Haskell 编写)创建流程图。
我想像这样的事情很容易:
main :: IO ()
main = do
someInput <- getLine
let upped = map toUpper someInput
putStrLn upped
这只是 3 个顺序步骤,获取数据,将其大写,输出它。
这次事情看起来更糟:
main :: IO ()
main = do
someInput <- fmap toUpper getLine
putStrLn someInput
或者像这样:
main :: IO ()
main = interact (map toUpper)
好吧,那就是 IO,你可以像命令式语言一样处理它。纯函数呢?
一个实际的例子:
onlyMatching :: String -> [FilePath] -> [FilePath]
onlyMatching ext = filter f
where f name = lower ('.' : ext) == (lower . takeExtension $ name)
lower = map toLower
你会如何绘制最后一个流程图?
Flowcharting. This ancient old practice that's been in use for over 1000 years now, being forced upon us poor students, without any usefulness (or so do I think). It might work well with imperative, sequentially running languages, but what about my beloved functional programming?
Sadly, I'm forced to create a flow chart for my programm (that is written in Haskell).
I imagine it being easy for something like this:
main :: IO ()
main = do
someInput <- getLine
let upped = map toUpper someInput
putStrLn upped
Which is just 3 sequenced steps, fetching data, uppercasing it, outputting it.
Things look worse this time:
main :: IO ()
main = do
someInput <- fmap toUpper getLine
putStrLn someInput
Or like this:
main :: IO ()
main = interact (map toUpper)
Okay, that was IO, you can handle that like an imperative language. What about pure functions?
An actual example:
onlyMatching :: String -> [FilePath] -> [FilePath]
onlyMatching ext = filter f
where f name = lower ('.' : ext) == (lower . takeExtension $ name)
lower = map toLower
How would you flowchart that last one?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
流程图和 FP 的线索是您开始在功能流中思考。
正如您现在可能知道的那样,FP 建立在调用函数来完成工作的函数之上。
如果您没有很好地了解谁将使用什么信息给谁打电话,那么您最终仍然会创建意大利面条式代码或创建大量执行相同操作的函数,从而使您的代码很难维护
。计划使用流程图来构建,描述事情发生的顺序,您最终将得到一个可维护且更易于测试的程序。
对于那些不熟悉结构图的人来说,这是一种使用发送值和返回值对从调用者到接收者的函数调用进行建模的方法。如果您已经具有从 ie 配置文件检索数据并在系统中的任何位置重用它的功能,那么有了它您就可以轻松航行
The clue with flow charts and FP is that you start thinking in Functional flows.
As you may know by now FP builds on functions calling functions to get the job done.
If you don't have a good image of who is going to call who with what information you still will end up creating spaghetti code or creating loads of functions doing the same thing making your code very hard to maintain
Creating a Structured Chart of what you plan to build with a flow chart describing in what order things have to happen you will end up with a program that is maintainable and easier to test.
For those unfamiliar with Structure Charts, this is a way to model function calls from the caller to the receiver with the send and return values. With it you can easy sea if you already have a function the retrieve data from a ie config file and reuse it anywhere in your system
嗯...您可以手动将代码编译为基于超级组合器的表示形式,然后绘制该超级组合器应用程序的类似流程图的图形。在某些情况下,这样做甚至很有用,可以提供流程的一些合理的视觉表示。只需考虑数据流而不是执行流。
Hm... You could manually compile your code into a supercombinators-based representation, and then draw a flowchart-like graph of that supercombinators application. In some cases it is even useful to do so, gives some reasonable visual representation of the flow. Just think of a data flow instead of an execution flow.
我认为代表流程(因此状态改变)的流程图不适合 FP,因为它大多是无状态的。
但我认为你可以展示一个电路图(?)。
你最好咨询一下导师。
I don't think flowchart, which represent processes (therefore change of states), is suitable for FP, which is mostly stateless.
But I think you can show a circuit diagram (?).
You'd better consult the instructor.
实际上,流程图在软件中的使用历史只有大约 60 年。 (实际上,正如我们所知,编程的历史可以追溯到 65 年!)当时,在非常繁琐且容易出错的“编码”阶段之前,它们作为规划和开发算法的工具非常重要。
如今,我们的编程语言已经达到了表达能力的水平,算法的意图可以通过代码本身更清楚地表达。 (在 VisualBasic 中可能没有那么多,但在 Haskell 中肯定如此。)因此,没有现代编程商店使用流程图。
然而,可视化编程语言是存在的,并且在某些领域取得了巨大的成功。这些环境与流程图相关。也许您的讲师确实准备做一些比较编程语言的工作,并引导大家思考这些方法。
最后,针对您手头的具体问题,请这样考虑:传统流程图主要演示了通过程序的控制流程,因为这就是人们当时编写的代码类型。然而,总会有一些数据流被说明。对于功能性程序,您主要是演示数据流。
不过,诀窍是弄清楚如何将(部分应用的)函数流说明为数据。想想流程图必须做什么来支持可以在两个地方调用的子例程的概念......现在也许您可以创建类似的图形构造来表示“由 Ⓐ 标识的函数作为
的第二个参数流入”过滤器
”我想象一个标记为fmap
的小弓,侧面有一个钥匙孔,用于将Ⓐ与箭头连接起来。如果没有别的事,请将其视为探索程序的替代表示形式的作业 - 如果您有扩展流程图(从来不需要处理通用函数),并明确这一点,您的老师应该给您额外的分数!
Actually, flowcharts for use in software date back only about 60 years. (And really, programming, as we know it, dates back only 65!) At the time they were incredibly important as a tool for planning and developing algorithms prior to the very tedious and error prone stage of "coding".
These days, our programming languags have reached a level of expressiveness where the intent of the algorithm is more clearly expressed by the code itself. (Perhaps not as much in VisualBasic, but certainly in Haskell.) Hence, no modern programming shop uses flowcharts.
However, visual programming languages exist, and have great success in some fields. These environments related to flowcharting. Perhaps your instructor is really preparing to do some amount of comparative programming language work, and is leading you all to thinking about these approaches.
Finally, to your specific problem at hand, think about it this way: Traditional flowcharts primarily demonstrated the flow of control through a program, since that is the kind of code people were writing at the time. However, there was always some amount of data flow illustrated as well. For a functional program, you'd be primarily demonstrating data flow.
The trick though, is figuring out how to illustrate flow of (partially applied) functions as data. Think of what flowcharting has to do to support the concept of a subroutine that can be called in two places... Now perhaps you can create similar graphical construct to mean "the function identified by Ⓐ flows in as the second argument of
filter
" I'm imagining a small bow labeledfmap
with a key-hole cut in the side for Ⓐ to be connected with an arrow to.If nothing else, think of this as an assignment in exploring alternate representations of your program - and if you have extend flowcharting (which never had to deal with generic functions), and make that clear, your instructor should be giving you extra marks!