[Haskell-cafe] Parsers for Text Adventures

Luke Palmer lrpalmer at gmail.com
Sun Jan 17 09:20:06 EST 2010


On Sun, Jan 17, 2010 at 7:02 AM, Stephen Tetley <stephen.tetley at 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 at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20100117/6b1943cc/attachment-0001.html


More information about the Haskell-Cafe mailing list