lundi 9 mars 2015

On Haskell, how do you make an AI control a function using IO (or, how do you properly redesign it)?

Suppose you must define a procedure that depends on a specific decision. For example:



sendMonsterToGraveyard :: Game -> IO Game
sendMonsterToGraveyard game = do
let monsters = monstersInPlay game
monster <- chose monsters
sendToGraveyard game monster


This function is an "effect" of an hypothetical game that lets the player chose one monster to kill. The problem with that design is that we are coupling the effect to the IO monad. What if, later on, we decide that we want an AI to use that effect (and, thus, chose a monster to kill)? That would be impossible other than making the AI aware of the terminal, but that doesn't sound robust. What is, thus, a proper way to redesign this pattern so that a game effect can be encoded without coupling it to the IO monad specifically?


Note: I'm asking this upon request as a follow-up of my previous question. One of the answers actually provides a good solution for this issue, but, since it wasn't specifically addressed on the question, we decided it would be better to create a new one.


Aucun commentaire:

Enregistrer un commentaire