编辑纯函数图
假设有一个图表和一些函数集,例如:
create-node :: Graph -> (Graph, Node)
split-node :: Graph -> Node -> (Graph, Node, Node)
我想创建这些函数的版本,这些函数不需要 Graph
作为参数,主要是为了方便(最好没有 monad,所以我不会'不需要将每个图形操作代码段包装在一个 monad 块中)。那么这个呢:
create-node :: (Graph -> (Graph, Node))
split-node :: (Graph -> Node) -> ((Graph -> Node), (Graph -> Node))
或者更一般地说:
fun :: (Graph -> Argument) -> ... -> (Graph -> Result)
然后我就可以使用 (Graph -> ...)
值,就像它们是普通节点一样。最后,要从 (Graph -> ...)
值中获取真实图形,只需将其应用于空图形即可。这是一个合理的做法吗?
Let's say there is a graph and some set of functions like:
create-node :: Graph -> (Graph, Node)
split-node :: Graph -> Node -> (Graph, Node, Node)
I would like to create versions of those functions that don't expect Graph
as an argument, mainly for convenience (preferably without monads so I wouldn't need to wrap every graph manipulating piece of code in a monad block). So what about this:
create-node :: (Graph -> (Graph, Node))
split-node :: (Graph -> Node) -> ((Graph -> Node), (Graph -> Node))
Or more generally:
fun :: (Graph -> Argument) -> ... -> (Graph -> Result)
I would then be able to use the (Graph -> ...)
values as if they were normal nodes. In the end, to get a real graph out of a (Graph -> ...)
value, just apply it to an empty graph. Is this a reasonable approach?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,所以
是状态单子,只是没有花哨的新类型(和翻转的返回值)。所以我看不出这里不使用 State 的好处。毕竟,这让我可以使用状态 Monad 编写相当干净的代码:
然后每当我有一些纯代码可以使用
runState
和朋友运行时弹出它:如果你确实想推进你的函数的想法,您可能需要查看差异列表以了解它是如何使用简单列表完成的。
另外,如果您刚刚要实现一些算法,您可能需要研究现有的函数图数据结构库(例如 fgl),而不是自己滚动。如果您想了解该理论,请查看 Erwig 关于归纳图的论文。
Ok, so
is the state monad, just without the fancy newtype (and a flipped return value). So I don't see the advantage of not using
State
here. After all that lets me write fairly clean code using the state Monad:Then pop out of it whenever I've got some pure code to run using
runState
and friends:If you do want to push forward with your functions idea, you may want to look into difference lists to see how its done with simple lists.
Also, if you've just got some algorithms to implement, you may want to look into existing functional graph data structure libraries (like fgl), rather than rolling your own. If you want to understand the theory, check out Erwig's paper on inductive graphs.