How do I implement graphs and graph algorithms in a functional programming language?

前端 未结 6 1340
暖寄归人
暖寄归人 2020-12-23 02:47

Basically, I know how to create graph data structures and use Dijkstra\'s algorithm in programming languages where side effects are allowed. Typically, graph algorithms use

6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-23 03:11

    I would love to hear about some really clever technique, but I think there are two fundamental approaches:

    1. Modify some global state object. i.e. side-effects
    2. Pass the graph as an argument to your functions with the return value being the modified graph. I assume this is your approach of "passing around large amounts of state"

    That is what's done in functional programming. If the compiler/interpreter is any good, it will help manage memory for you. In particular, you'll want to make sure that you use tail recursion, if you happen to recurse in any of your functions.

提交回复
热议问题