
To the original author: I must notice that this is not the most
convincing use of purity. I personally would prefer to have a
character's spell list be mutable because this corresponds better to
the nature of the problem (in the problem domain it is nonsense to
have two versions of a character and hope that one version will drop
out of the scope).
However, Haskell forces you to be pure, and that pays off a lot,
despite the existence of problems that are not modeled intuitively
this way.
In case you absolutely need mutable state, use the ST monad or
IORef's, but **you have to first become skilled with pure
computations, because that's the only way to not make a mess of your
treatment of the impure ones**.
And if anyone tries to seduce you with unsafePerformIO, resist!
2009/5/3 Belka
Welcome to Haskell! =)
But now I don't know how to dynamically add new spells (new spells can be created in my gameplay). Since I can't assign a new value to the `spells' variable (Data.Map.insert returns a new map), I just don't know where to go.
Do distinguish *pure function* concept and *action with side effects* concept. The default in FP (Funct. Progr.) paradigm usualy is *pure function*, if *action with side effects* is avoidable. Pure functions deal with immutable memory "objects". So in your case, whenever you add a new spell, you get a new character (since your chacter's "essence" is a spell list). That way after calculating *addNewSpell* you will have 2 versions of character: a new one (with spells list bigger), an the original one. Since old one usualy drop out of the scope (while program evaluation progresses) it gets *garbage collected* (note: garbage collection showed up in FP already in 1960s).
addNewSpell :: Character -> Spell -> Character addNewSpell char_old_version spell_to_add = ...
main = let char_new_version = addNewSpell char_old_version new_spell in putStrLn $ show char_new_version where char_old_version = Charcter {...} new_spell = Spell { .... }
Have luck, with the brain rewriting! =) Belka -- View this message in context: http://www.nabble.com/using-haskell-for-a-project-tp23348425p23352598.html Sent from the Haskell - Haskell-Cafe mailing list archive at Nabble.com.
_______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe
-- Eugene Kirpichov Web IR developer, market.yandex.ru