I am new to Scheme – I am currently trying to learn the syntax and how to think recursively. I came to a section on vectors and wanted to be able to set values in my vector via some kind of looping (using recursion of course). I have this variable:
(define my-vector (make-vector 5))
which I then want to populate using the vector-set! procedure. Normally in C++ (the only other language I am really familiar with) this would be done in an iterative fashion, eg
//...
std::vector<int> myVector;
for(int i = 0; i < 5; ++i) // populate the vector
myVector.push_back(i);
std::vector<int>::const_iterator outIter;
for(outIter = myVector.begin();
outIter != myVector.end(); ++outIter)
std::cout << *outIter << " ";
std::cout << std::endl;
//...
However, I know that this kind of thing should be done via recursion in Scheme. What would a recursive populate-vector procedure might look like??
You can try it online here.
You can also try using the
DOsyntax, but most find it hard to remember 🙂Learning to use named
LETis very important.Also note, a Scheme vector is just a fixed-sized array.