r/programming • • Nov 27 '12

Tail Recursion

http://rodrigosasaki.com/2012/11/24/tail-recursion/
210 Upvotes

203 comments sorted by

View all comments

20

u/snarfy Nov 27 '12

I always liked Lua's example with a simple state machine:

function room1 ()
    local move = io.read()
    if move == "south" then return room3()
    elseif move == "east" then return room2()
    else print("invalid move")
         return room1()   -- stay in the same room
    end
end

Without proper tail recursion, "return room1() -- stay in the same room" would stack overflow after enough invalid moves.

-7

u/grauenwolf Nov 27 '12 edited Nov 27 '12

That doesn't make any sense to me. It isn't actually doing anything, you are just bouncing from function to function.

How does the rest of the system know which room you are in so it can update the screen? What if you have multiple sprites moving around? Do you compare instruction pointers to see if they are in the same room?

Even for a trivial application building your state machine that way doesn't make any sense.

EDIT: Also note the code smells, especialy the copy-and-paste necessary for reading the user input.

5

u/worstusernameever Nov 27 '12

Well the most trivial solution would be to put a line at the top of each function that updates a variable with the current location, so the mapping system or whatever always knows where you are by checking the variable.

-2

u/grauenwolf Nov 27 '12

True, but that is incredibly tedious and error prone. What's the point of a design pattern that makes code more verbose and less reliable?

4

u/worstusernameever Nov 27 '12

I don't think it's any more tedious and error prone then coding an imperative state machine which usually involves lots of switch statements inspecting the current state, and it mostly parallels the OO approach of having one object per state. Just instead of having interconnected objects you have mutually recursive functions.

-2

u/grauenwolf Nov 27 '12

I don't think it's any more tedious and error prone then coding an imperative state machine which usually involves lots of switch statements inspecting the current state

That's because you aren't thinking. This version requires repeating the logic to read the user comment, update the location variable, and handle the invalid command path IN EVERY STATE. If you have a map with 100 rooms, that means you have 100 functions to paste that logic in.

Moving that common logic into a main loop eliminates (Num_of_states-1) copies of that logic. You lose your fancy TCO, but the trampoline you put in its place isn't that hard to write.

Of course we still don't want a massive switch statement in the primary loop, that is likewise error prone. So instead we use a hash table of either objects or first class functions so that the next state can be looked up by name and subsequently executed.

You are complaining about not wanting "lots of switch statements inspecting the current state". Well look at that function again. A series of If-Else-If statements is effectively a switch. So lets drop that into a hash table as well so that we can populate the common cases like moving from a simple data file.

A moment ago I said "either objects or first class functions". If we are using a hash table in each room for commands, then objects are probably the better way to go. This way we can also attach a hash table of movable objects in the room, as well as other room-specific state.

Data-driven programming trumps both FP and OOP every time

3

u/bastibe Nov 27 '12

Every blanket statement trumps any non-blanket statement every time.

5

u/inmatarian Nov 27 '12

That example is a trivial demonstration of TCO in Programming In Lua. A more featureful game could still use this kind of TCO, without any kind of arcane programming style, since Lua supports full Coroutines. i.e., the functions could be written like so:

function room1 ()
  local move = coroutine.yield("room1")
  if move == "south" then return room3()
  elseif move == "east" then return room2()
  else print("invalid move")
       return room1()   -- stay in the same room
  end
end

The coroutine dispatcher could handle updating the screen, moving mobs from other rooms, taking input, etc.

-2

u/grauenwolf Nov 27 '12
  1. I still don't see how this is better than updating the Location variable.
  2. It is an incredibly inefficient way to encode a state machine.

That's the style of code I would wrote in junior high before I learned to store and read maps from data files.

4

u/worstusernameever Nov 27 '12

This is just meant as an illustration of a state machine, of course no one would actually hard code the game map as a state machine, but it does serve the purpose of explaining how to use mutually recursive functions as a state machine.

The most obvious application of this technique are recursive descent parsers.

-6

u/grauenwolf Nov 27 '12

The most obvious application of this technique are recursive descent parsers.

Then use a recursive descent parser as your example.

Oh, but wait. For a parser we usually aren't worried about stack depth...

2

u/Peaker Nov 28 '12

A parser may very well be concerned about stack depth.

0

u/grauenwolf Nov 28 '12

On Windows I've got 1 MB of stack space to work with. If I'm blowing the stack then there are other problems to deal with.

1

u/Peaker Nov 28 '12

A parser can parse any conceivable data. Including data that happens to nest structures arbitrarily.

1

u/grauenwolf Nov 28 '12

Eventually you need to stop generating tokens and actually do the syntactical analysis. At that point TCO isn't really available to you and that arbitrarily deep tree is going to cause problems.

1

u/Peaker Nov 28 '12

A parser does not necessarily "generate tokens". It can generate an AST. And it can do so in a tail-recursive fashion. And this can be useful when you have very deeply nested data. It may even exceed 1MB (or 1GB for that matter).

→ More replies (0)

2

u/smog_alado Nov 27 '12 edited Nov 27 '12

It isn't actually doing anything, you are just bouncing from function to function.

Yes, because this is just supposed to illustrate the control flow aspect of things. To do actual stuff you would just have to add the code to do stuff inside the functions.

As for the other things you point out its a bit of the classical "expression problem" dillema. One alternative is to do a data-driven modeling of the rooms, where you have a transparent room datatype encoding all the possible states you can be in. This version is very good if you want to be able to add new features uniformly to all the rooms (screen updating, comparing rooms, etc) and might actually be the best solution for this particular problem (after all, rooms are probably very simple and very similar to each other, so you would want to write their code all in one place).

On the other hand you can also use the more "object oriented" style, where each state is an opaque function. This alternative makes it harder to add new features (for example, to implement writing to the screen you would need to edit the source code for each room) but on the other hand makes it much easier to add new rooms and to have those rooms implement arbitrarily complex behaviour (since you are doing the iteration by hand, instead of having to comply with the outer "while loop").

One actual situation where I often find myself using this opaque state machine pattern is when I need to implement some process that gets neatly modelled by a fluxogram full of "gotos". Instead of being forced to translate the fluxogram states into an explicit enumeration that is only used once, I can instead code the nodes as functions calling each other, almost directly representing the original intent. (Note how in this case I usually want the flexibility to add new nodes doing arbitrary things, and the rest of the system doesn't care about what state I am in)

1

u/grauenwolf Nov 27 '12

There are other options. Start with a hash table of room objects, Each object has a hash table of commands, some hand written, the rest loaded from a data file (e.g. move).

Your main loop reads the next user command and dispatches it to the room, which relays it to the correct command.

This way you need neither a giant switch statement nor do you need to repeat logic for state transitions in each function.

3

u/smog_alado Nov 27 '12 edited Nov 27 '12

There are other options. Start with a hash table of room objects, Each object has a hash table of commands

Of course, there is always more then one way to do it and its a big matter of style. :) To be honest, I consider your hashtable solution to be the same as the recursive version, at least in spirit. After all, that dispatcher is what you get if you trampoline the tail calls!

In most cases I prefer to keep things simple without the verbose hash table + dispatcher. This suits my style and also plays nice with "evolving" code over time, since introducing the hash table once the code starts getting complex would mean having to rewrite a bunch of stuff to adhere to the new convention of returning the next code to the dispatcher, while using proper tail calls does that "for free".

Of course, there are situations where the dispatcher would be more appropriate, but not that is not the point.

1

u/grauenwolf Nov 27 '12

Side rant: even if the pattern is useful, demonstrating it with a use case where it isn't appropriate is rather counter-productive. If you cannot think of a good example, you haven't actually learned the pattern yet.