I did not quite understand the one proposed here. The whole doubt is in combiner, by statement, I understand to be a delegate. You see, they do not need to post codes, I would just like a way forward. Below the statement. The whole part marked with the tag code, is the statement, including the texts. Doing a function that returns the sum of squares I have already done and is working, but I need now the form proposed by this exercise.
function acumular(combiner, nullValue, list)
{
if (list.length == 0) {
return nullValue;
}
var primeiro = list.removePrimeiro();
return combiner(primeiro, acumular (combiner, nullValue, list));
}
Implemente a função somaDeQuadrados que retorna a soma de quadrados de uma lista.
somaDeQuadrados([1,2,3,4,5])
retorna o número 55.
Neste caso a função acumular deve ser utilizada. A variável “combiner” é um “ponteiro para função”. A implementação da função “combiner” faz parte da solução.
What I really need is a path and not a code, because I want to do, as I have done the others.
My big question is in the implementation of Combine. I did not fully understand what the dcastro posted. If someone understood or the dcastro itself, can, thank you with any additional help.
My biggest difficulty lies in implementing the Build function, because it takes an argument of the type of a method (Combiner). My Combiner method is implemented as follows:
private int Combiner(int primeiro, int acc)
{
acc += primeiro * primeiro;
return acc;
}
And the accumulate method I'm catching like this:
public int acumular(int combiner, int? nullValue, List<int> list)
{
if (list.Count == 0)
return 0;
var primeiro = list.First();
list.RemoveAt(0);
return Combiner(primeiro, acumular(Combiner(primeiro,1),null,list));
}
The return 0 is out of the proposed and the parameters in the recursive call to accumulate.
I thought I was wrong, but it worked out the sum of the squares in the way above. I made the call like this:
int[] inteiros = {1,2,3,4,5,6,7};
List<int> lista = new List<int>(inteiros);
lblFat.Text = f.acumular(1,null,lista).ToString();
As the array already exists, I just assign the array to the list, so it does not fill, so it has array and list in the call, but the call does not go to the test.