title: Functional Programming, recursing a game state loop
body: <p>I was planning on writing a simple game to test out my understanding of functional programming. The functional way of doing the main loop, would be recursing it, but won't this eat up memory as more and more stack frames are generated?</p>
  
  <p>Thanks</p>
  
  <p>An example from <a href="https://stackoverflow.com/questions/1020653/how-can-you-do-anything-useful-without-mutable-state">How can you do anything useful without mutable state?</a></p>
  
  <pre><code>// imperative version
  pacman = new pacman(0, 0)
  while true
      if key = UP then pacman.y++
      elif key = DOWN then pacman.y--
      elif key = LEFT then pacman.x--
      elif key = UP then pacman.x++
      render(pacman)
  
  // functional version
  let rec loop pacman =
      render(pacman)
      let x, y = switch(key)
          case LEFT: pacman.x - 1, pacman.y
          case RIGHT: pacman.x + 1, pacman.y
          case UP: pacman.x, pacman.y - 1
          case DOWN: pacman.x, pacman.y + 1
      loop(new pacman(x, y))
  </code></pre>