0
I did not understand the proposal here. The whole question is in the Commoner, by the statement, I understand to be a delegate. See, they do not need to post codes, I would like only one way to follow. Below the statement. The whole part marked with the code tag is the statement, including the texts. Make a function that returns the sum of the squares I’ve done and it’s 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, not a code, because I want to do it, like I’ve done all the others.
My big doubt is in the implementation of Combine. I didn’t fully understand what dcastro posted. If someone understood or dcastro itself, can, thank you with any additional help.
My biggest difficulty is in the implementation of the Accumulate function, because it receives an argument of the type of a method (Combiner). My Combiner method is thus implemented:
private int Combiner(int primeiro, int acc)
{
acc += primeiro * primeiro;
return acc;
}
And the method accumulate I’m getting it 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));
}
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 of the form 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 there was already the array, I just assign the array to the list, not to be filled in, so it has array and list in the call, but the call does not go to the test.
This isn’t exactly in C#, is it? I think it would be nice to edit the question and leave it all in C#. I see no problem to answer objectively, with code. See also if this answer helps you.
– Leonel Sanches da Silva
@Ciganomorrisonmendez, the question is exactly there, to pass to the C#.
– pnet
@pnet I updated my reply with corrections to your current code.
– dcastro