title: Scheme/Lisp nested loops and recursion
body: <p>I'm trying to solve a problem in Scheme which is demanding me to use a nested loop or a nested recursion.</p>
  
  <p>e.g. I have two lists which I have to check a condition on their Cartesian product.</p>
  
  <p>What is the best way to approach these types of problems? Any pointers on how to simplify these types of functions?</p>
  
  <p><hr>
  I'll elaborate a bit, since my intent might not be clear enough.</p>
  
  <p>A regular recursive function might look like this:</p>
  
  <pre><code>(define (factorial n)
    (factorial-impl n 1))
  
  (define (factorial-impl n t)
    (if (eq? n 0)
        t
        (factorial-impl (- n 1) (* t n))))
  </code></pre>
  
  <p>Trying to write a similar function but with nested recursion introduces a new level of complexity to the code, and I was wondering what the basic pattern is for these types of functions, as it can get very ugly, very fast.</p>
  
  <p>As a specific example, I'm looking for the easiest way to visit all the items in a cartesian product of two lists.</p>