Tail recursive vector sum
This question already has an answer here:
Your solution has two main problems:
The are several ways to implement this recursion, I suggest the following approach:
function result = vectorSum(v)
%takes a vector v and the returns the sum of all elements in the vector
if length(v) == 0
result = 0;
else
result = v(end) + vectorSum(v(1:end-1));
end
end
链接地址: http://www.djcxy.com/p/14166.html
上一篇: python 3.x
下一篇: 尾递归矢量和