title: Writing a cached function in Racket
body: <p>So I'm working on some code (going over a practice exam for a course on Racket), and I have to do the following: </p>
  
  <p>Write a function <code>cached-assoc</code> that takes a list <code>xs</code> and a number <code>n</code> and returns a function that takes one argument <code>v</code> and returns the same thing that (<code>assoc v xs</code>) would return. </p>
  
  <p>You should use an n-element cache of recent results to possibly make this function faster than just calling assoc. The cache should be a vector of length n that is created by the call to cached-assoc and used-and-possibly-mutated each time the function returned by cached-assoc is called. </p>
  
  <p>The cache starts empty (all elements #f). When the function returned by cached-assoc is called, it first checks the cache for the answer. If it is not there, it uses <code>assoc</code> and <code>xs</code> to get the answer and if the result is not <code>#f</code> (i.e., xs has a pair that matches), it adds the pair to the cache before returning
  (using vector-set!). </p>
  
  <p>The cache slots are used in a round-robin fashion: the first time a pair is added to the cache it is put in position <code>0</code>, the next pair is put in position 1, etc. up to position <code>n - 1</code> and then back to position <code>0</code> (replacing the pair already there), then position <code>1</code>, etc.</p>
  
  <p>I have no idea how to do this. </p>