can this be written more easily?

Ken Shan ken@digitas.harvard.edu
Sat, 22 Feb 2003 23:32:42 -0500


--PNTmBPCT7hxwcZjr
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
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) =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.

--=20
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?

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

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.1 (GNU/Linux)

iD8DBQE+WE7qzjAc4f+uuBURAgbXAJ0YoOUm8VpdW5CpbWaYFxqOo1J0BACbBmhw
t1YurlyDeHrcVnWUVxyG6WY=
=bJOw
-----END PGP SIGNATURE-----

--PNTmBPCT7hxwcZjr--