Tail Recursion in gawk

Once you’ve used Lisp—especially Scheme—you come to regard tail recursion as a natural and necessary technique. Perhaps even a God given right. Sadly, leave the Lisp world and you’re pretty much out of luck. Of course, you can always make a tail call but that quickly fills up the stack. What we’re talking about is sometimes called tail call optimization (TCO). With TCO, the function call in the tail position is replaced by a goto and nothing is pushed onto the stack.

Marc Simpson over at 0branch blogs about one of the rare exceptions to the Lisp-only rule about TCO. In gawk—but not in nawk or mawk—there is a limited type of tail call optimization but you have to take steps to enable it and it only works in restricted situations. Still, it’s nice to know it’s available if you really need it. Head over to the post for the details. They’re not earth shattering but it is nice to know.

This entry was posted in General and tagged , . Bookmark the permalink.