我目前正在做一个clojure的路线规划程序。我使用ubergraph来创建我的数据结构,然后使用最短路径算法中内置的ubergraph。我让它在传入2个参数的情况下工作,也让它在传入3个参数的情况下工作。
但是,有没有一种方法可以编写一个递归版本,可以接受任意数量的参数,而不是每次我想要传递更多参数时都编写一个新函数?
(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)
非常感谢您的帮助。
发布于 2017-03-16 03:00:12
下面的方法应该是可行的
(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) ))
https://stackoverflow.com/questions/42817692
复制相似问题