On 2003-02-21T13:37:26-0500, Mike T. Machenry wrote:
> Eh, state is not possible. This is a recursive state space search. I need
> to branch the state of the game and not allow branches to effect others.

I see-- so you don't care about performing array updates in place,
because you will be copying arrays from parent nodes in the state space
to child nodes anyway.  Is that correct?

From what you originally wrote, it seems to me that you are forced
to write awkward code because very often you need to reach into a
data structure and perform some manipulation on a part of it without
affecting anything else.  This seems like a common pattern that deserves
abstraction.

If you have a function from a to a, and you have an Array of a's, you
can apply that function to update a certain element of that Array:

    atIndex :: (Ix i) => i -> (a -> a) -> (Array i a -> Array i a)
    atIndex i f a = a // [(i, f (a!i))]

Similarly, whenever you have a tickets-to-tickets function (by the way, it
seems that you renamed tickets to dTickets or vice versa while composing
your message), you can turn it into a GameState transformer:

    atTickets :: (Array Player (Array Ticket Int) ->
                  Array Player (Array Ticket Int))
              -> (GameState -> GameState)
    atTickets f s = s { tickets = f (tickets s) }

Your removeTicket function can now be written

    removeTicket s d t = (atTickets $ atIndex d $ atIndex t $ pred) s

This technique generalizes from arrays and records to other container
data structures like lists and finite maps.

-- 
Edit this signature at http://www.digitas.harvard.edu/cgi-bin/ken/sig
Is it because of problems at school that you say you hope the
eurythmics'' practice birth control?

Attachment: pgp00000.pgp
Description: PGP signature

Reply via email to