On Sun, Jan 17, 2010 at 7:02 AM, Stephen Tetley <stephen.tetley@gmail.com> wrote:
I wouldn't use read instead something either a simple function:

> verb :: String -> Maybe Verb
> verb "Go"  = Just Go
> verb "Get" = Just Get
> verb _     = Nothing


Or possible a Map:

> verb2 :: String -> Maybe Verb
> verb2 s = Map.lookup s verb_map

> verb_map :: Map.Map String Verb
> verb_map = Map.fromAscList [ ("Go", Go), ("Get", Get) {- .. -} ]

Oh, yeah, I like these better than relying on the Read instance.  Relying on Read and Show for program logic has been kind of an implicit smell to me, and I can put my finger on why now: you lose alpha conversion on the program scale.  I like the ability to rename with confidence.

Plus, this way it is natural to encode synonyms; eg. "take" and "get".  Though depending on the vernacular of the game you might not want that.  I'm not sure what it means to take out of bed.

Luke
 


You could then do more about say case sensitivity - e.g. add ("get",Get) etc
or always convert to upper before querying the map.


> verb3 :: String -> Maybe Verb
> verb3 s = Map.lookup (map toUpper s) verb_map2

> verb_map2 :: Map.Map String Verb
> verb_map2 = Map.fromAscList [ ("GO", Go), ("GET", Get) {- .. -} ]


Best wishes

Stephen
_______________________________________________
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe