How do I iterate over a list and pass the funciton result to the next iteration?

I guess you are looking for List.fold. Something like: let inputList = 1; 1; 0; 0; 0; 0; 0; 1; 1; 0; 0; 0; 0; 0; 1; 1; let weights = 0.2; 0.6; 0.5; 0.9; 0.8; 0.4; 0.7; 0.3 let newWeights w values = w //Fake method which returns old weight as it is inputList |> List. Fold (newWeights) weights NOTE: The newWeights function in this case is taking weights and input vector and returns new weights Or may be a List.

Scan in case you also need the intermediate calculated weights let inputList = 1; 1; 0; 0; 0; 0; 0; 1; 1; 0; 0; 0; 0; 0; 1; 1; let weights = 0.2; 0.6; 0.5; 0.9; 0.8; 0.4; 0.7; 0.3 let newWeights w values = w inputList |> List. Scan (newWeights) weights.

Forgive me, I'm trying to figure out how to make my question clearer but it's complicated. I do need the intermediate calculated weights. Those intermediate weights then need to be the input for the next iteration.

– nathan Jul 27 at 19:33 @Nathan, thats what Fold function is for, i. E for each item in the list (in your case each vector in the list) calculate weights and then this calculated weight is used to calculate weight for next vector in the list and so on .. and in the end you will have a final single weight – Ankur Jul 28 at 5:01 yes I understand now – nathan Jul 28 at 13:15.

If you just started learning F#, then it may be useful to try implementing this explicitly using recursion first. As Ankur points out, this particular recursive pattern is captured by List. Fold, but it is quite useful to understand how List.

Fold actually works. So, the explicit version would look like this: // Takes vectors to be processed and an initial list of weights. // The result is an adapted list of weights.

Let rec processVectors weights vectors = match vectors with | -> // If 'vectors' is empty list, we're done and we just return current weights weights | head::tail -> // We got a vector 'head' and remaining vectors 'tail' // Adapt the weights using the current vector... let weights2 = newweights weights head // and then adapt weights using the remaining vectors (recursively) processVectors weights2 tail This is essentially what List. Fold does, but it may be easier to understand it if you see the code written like this (the List. Fold function hides the recursive processing, so the lambda function used as an argument is just the function that calculates new weights).

Aside, I don't quite understand your newMatrix function. Can you give more details about that? Generally, when working with lists you don't need to use indexing and it seems that you're doing something that requires accessing elements at a specific index.

There may be a better way to write that....

The newMatrix function is the heart of the Kohonen algorithm, it essentially calculates a new weight matrix based on the weight with the minimum Euclidian distance. The vector that is the minimum replaces the original vector in the weight matrix, hence the indexing. I'm sure there's a better way, but as I said, I'm still a noob :D – nathan Jul 27 at 19:31 This helped me understand how to put the algorithm in a recursive context.Thanks.

– nathan Jul 28 at 13:17.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions