Being Perfect (in code)
© 7 April 2015 Luther Tychonievich
Licensed under Creative Commons: CC BY-NC-ND 3.0
other posts

I find following Christ easier if I think recursively, not iteratively.

 

Earlier this year I interviewed for a faculty position at Brigham Young University. Although they and I agreed the position was not the right one for me, the interview process was very enjoyable. One aspect of that was being asked to teach a room of Christians about recursion. That let me use an example of recursion that I find particularly compelling:

void bePerfect(Day today) {
  try {
    choseTheRight(today);
  } catch(Sin s) {
    repentOf(s);
  }
  Day tomorrow = today + 1;
  bePerfect(tomorrow);
}

That is an example of tail recursion, and tail recursion can be easily rewritten as a loop, in this case an infinite loop since it was infinite recursion:

void bePerfect(Day today) {
  while(true) {
    try {
      choseTheRight(today);
    } catch(Sin s) {
      repentOf(s);
    }
    today += 1;
  }
}

There is something about the loop version of this particular example that seems much less hopeful and achievable than the recursive version. It puts the emphasis in the wrong place: the infinite repetition gets emphasized and the day-by-day nature of life is almost lost. You never leave the loop; nothing changes; it looks so… hopeless. The recursion, on the other hand, emphasizes the one-day perspective and, while semantically identical in practice, somehow makes the fact that each new day is different more visible.

I wish everyone spoke code. It is such a powerful set of languages, filled with nuance and connotation, a family of languages that, by design, express ideas that are difficult or impossible to express in other languages.




Looking for comments…



Loading user comment form…