我程序中的几乎所有函数都有某种异步调用,但它们都依赖于某些先前函数的结果.因此,我将下一个函数调用硬编码到每个函数中:
function getStuff() {
$.ajax({
...
success: function(results) {
// other functions involving results
getMoreStuff(results);
}
});
}
function getMoreStuff(results) {
$.ajax({
...
success: function(moreResults) {
// other functions involving moreResults
doSomethingWithStuff(moreResults);
}
);
}
Run Code Online (Sandbox Code Playgroud)
等等.它是一个大型链,每个函数调用下一个函数.虽然这在程序中有效,但它使每个函数都无法单独使用.
我对如何避免这个问题有点失落.我无法弄清楚如何使用通用回调函数,因为当我进行函数调用时,它会像这样结束(使用上面的函数):
getStuff(function() {
getMoreStuff(results, doSomethingWithStuff);
};
Run Code Online (Sandbox Code Playgroud)
但是,"结果"还没有定义.
解决方案似乎很明显,我只是对它有点密集.抱歉!
我需要编写一个Lisp函数来查找两个节点之间的最长路径,而无需重新访问任何节点.但是,如果起始节点和结束节点相同,则可以重新访问此节点.该函数需要是递归和深度优先搜索.
我一直试图在这里工作几个小时,并且无法提出解决方案.我知道函数的一般大纲,但无法正确编程.在一些代码中,主要是伪代码:
(defun longest-path (start end net &optional (current-path nil))
(cond ((and (eql start end)
(not (null current-path)))
(list start))
(t
(find neighbors of start/node)
(remove any previously traveled neighbors to avoid loop)
(call longest-path on these neighbors)
(check to see which of these correct paths is longest))))
Run Code Online (Sandbox Code Playgroud)
网看起来像'((ab)(bc)),其中第一项是节点,其他一切都是它的邻居(例如,节点a有邻居b,节点b有邻居c).
是的,这是用于家庭作业,所以如果你觉得发布解决方案或其任何部分感觉不舒服,那就不要了.我只是Lisp的新手,想要一些技巧/帮助,以获得一个不错的开始.
谢谢
编辑:嗯,我能得到的最多是:
(defun longest-path (start end net &optional (current-path nil))
(cond ((and (eql start end)
(not (null current-path)))
(list start))
(t
(push start current-path)
(let ((neighbors (cdr (assoc start net)))) …Run Code Online (Sandbox Code Playgroud)