我在JavaScript中有以下代码,我想在C#中编写相同的代码。我不知道如何进行递归并一起返回一个函数。
function findSequence(goal) {
function find(start, history) {
if (start == goal)
return history;
else if (start > goal)
return null;
else
return find(start + 5, "(" + history + " + 5)") ||
find(start * 3, "(" + history + " * 3)");
}
return find(1, "1");
}
我认为这样可行,但事实并非如此。
public static Func<int, string, string> findSequence(int goal)
{
var find = new Func<int, string, string>(start, history => {
if (start == goal)
return history;
else if (start > goal)
return null;
else
return (find(start + 5, "(" + history + " + 5)") ||
find(start * 3, "(" + history + " * 3)"));
});
return find;
}