title: How do you keep the `roll` operator straight?
body: <p>In postscript, the <code>roll</code> operator is very general and difficult to visualize. How do you make sure you're rolling in the right direction?</p>
  
  <p>I want to get a solid handle on <code>roll</code> because I want to be able to transform functions using variables</p>
  
  <pre><code>/f { % x y z
      /z exch def
      /y exch def
      /x exch def
      x dup mul
      y dup mul
      z dup mul add add % x^2+y^2+z^2
  } def
  </code></pre>
  
  <p>into functions using stack manipulations, more like</p>
  
  <pre><code>/f { % x y z
      3 1 roll dup mul % y z x^2
      3 1 roll dup mul % z x^2 y^2
      3 1 roll dup mul % x^2 y^2 z^2
      add add % x^2+y^2+z^2
  } def
  </code></pre>
  
  <p>or </p>
  
  <pre><code>/f { % x y z
      3 { 3 1 roll dup mul } repeat
      2 { add } repeat      % x^2+y^2+z^2
  } bind def
  </code></pre>
  
  <p>These should both execute faster by having fewer name-lookups (hashtable search).</p>
  
  <p>With <code>roll</code> I always have to test it out; and I usually
  get it wrong on the first try! I'm okay with exch, though</p>