[Haskell-cafe] Enum to String, and back?

michael rice nowgate at yahoo.com
Wed Apr 15 15:13:12 EDT 2009


Using Show it is possible to establish a relationship between an enum type

data Color
    = Red
    | Blue
    | Green
    | Yellow
    | Orange
    | Brown
    | White
    | Black
    deriving (Show, Eq, Enum, Bounded)

and a String type to display it.

*Main> Red
*Main> [Red,Green,Blue]

which is

Red :: Color -> "Red" :: [Char] 

Can one as easily establish a reverse relationship, i.e., convert a String type like "Red" back to its corresponding Color type?

So that

"Red" :: [Char] -> Red :: Color


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090415/f70b15d0/attachment.htm

More information about the Haskell-Cafe mailing list