title: zipping lists in R
body: <p>As a guideline I prefer apply functions on elements of a list using lapply or *ply (from plyr) rather than explicitly iterating through them. However, this works well when I have to process one list at a time. When the function takes multiple arguments, I usually do a cycle. </p>
  
  <p>I was wondering if it's possible to have a cleaner construct, still functional in nature. One possible approach could be to define a function similar to Python, zip(x,y), which takes the input lists, and returns a list, whose i-th element is list(x, y), and then apply the function to this list. But my question is whether I am using the cleanest approach or not. I am not worried about performance optimization, but rather clarity/elegance.</p>
  
  <p>Below is the naive example.</p>
  
  <pre><code>        A &lt;- as.list(0:9)
          B &lt;- as.list(0:9)
          f &lt;- function(x, y) x^2+y
  
          OUT &lt;- list()
          for (n in 1:10) OUT[[n]] &lt;- f(A[[n]], B[[n]])
          OUT
          [[1]]
          [1] 0
  
          [[2]]
          [1] 2
  
          ...
  </code></pre>
  
  <p>And here is the zipped example (which could be extended to arbitrary arguments):</p>
  
  <pre><code>zip &lt;- function(x, y){
      stopifnot(length(x)==length(y))
      z &lt;- list()
      for (i in seq_along(x)){
          z[[i]] &lt;- list(x[[i]], y[[i]]) 
      }
      z
  }
  E &lt;- zip(A, B)
  
  lapply(E, function(x) f(x[[1]], x[[2]]))
  
  [[1]]
  [1] 0
  
  [[2]]
  [1] 2
  
   ...
  </code></pre>