can this be written more easily?

Ken Shan
Sat, 22 Feb 2003 23:32:42 -0500

Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

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?

=46rom 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

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) =3D> i -> (a -> a) -> (Array i a -> Array i a)
    atIndex i f a =3D 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 =3D s { tickets =3D f (tickets s) }

Your removeTicket function can now be written

    removeTicket s d t =3D (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
Is it because of problems at school that you say you hope the
eurythmics'' practice birth control?

Content-Type: application/pgp-signature
Content-Disposition: inline

Version: GnuPG v1.2.1 (GNU/Linux)