Recursion 基于ubergraph的最短路径clojure递归函数
我目前在clojure从事路线规划项目。我正在使用ubergraph创建我的数据结构,然后使用ubergraph内置的最短路径算法。我让它处理传入的2个参数,也让它处理传入的3个参数 但是,不是每次我想传入更多参数时都要编写一个新函数,有没有一种方法可以编写一个可以接受任意数量参数的递归版本Recursion 基于ubergraph的最短路径clojure递归函数,recursion,clojure,shortest-path,Recursion,Clojure,Shortest Path,我目前在clojure从事路线规划项目。我正在使用ubergraph创建我的数据结构,然后使用ubergraph内置的最短路径算法。我让它处理传入的2个参数,也让它处理传入的3个参数 但是,不是每次我想传入更多参数时都要编写一个新函数,有没有一种方法可以编写一个可以接受任意数量参数的递归版本 (defn journey [start end] (alg/pprint-path (alg/shortest-path all-edges {:start-node start, :end-node
(defn journey [start end]
(alg/pprint-path (alg/shortest-path all-edges {:start-node start, :end-node end, :cost-attr :weight})))
(journey :main-office :r131)
(defn fullpath [start delivery end]
(journey start delivery)
(journey delivery end))
(fullpath :main-office :r131 :main-office)
(fullpath :main-office :r119 :main-office)
以上是我目前拥有的代码,运行良好
例如,是否可以编写一个函数,该函数可以接受下面的参数,并且仍然可以打印出所采用的路径
(fullpath :main-office :r113 :r115 :main-office)
非常感谢您的帮助。以下几点应该可以
(defn fullpath [& stops]
(map (fn [a b] (journey a b)) stops (rest stops) )
为了什么
(fullpath :a :b :c ..)
收集结果
(journey :a :b)
(journey :b :c)
...
收集成一个集合。由于您的旅程返回值似乎为零,并且您只对打印它的副作用感兴趣,因此您可能希望输入doall,即
(defn fullpath [& stops]
(doall (map (fn [a b] (journey a b)) stops (rest stops) ))
你检查过这个了吗?你试过申请吗?嗨,如果我对申请的理解是正确的,那么在这种情况下就不起作用了?我正在尝试编写一个递归函数,它计划通过在我的例子中被归类为房间的所有变量传递的路径。因此,当传入2个参数时,它计划从a->b开始的路径,但我希望这样,如果传入3个变量,它计划a->b,然后是b->c等。使用[&args]定义函数会将传递给函数的所有参数绑定到参数args。传入的任何参数都会绑定到名为args的变量?这似乎不适用于通过不同数量的房间并在其上形成最短路径?