[Haskell-beginners] how to define a user datatype consisting of
instances of String?
Daniel Fischer
daniel.is.fischer at web.de
Thu Oct 23 02:47:24 EDT 2008
Am Donnerstag, 23. Oktober 2008 08:07 schrieb Benjamin L.Russell:
> How is it possible to define a user datatype consisting of instances
> of String?
>
> Suppose I am preparing a party for wine connoisseurs, and want to
> define the following data types:
>
> data Wine = Red | White
> data Red = "Merlot"
> data White = "Sauvignon Blanc"
> data Entree = "pork" | "chicken" | "tuna"
> data SideDish = "garlic bread" | "mozzarella sticks" | "caviar"
The syntax for data declarations does not allow such, you must have
constructors applied to types. You could make it
data Wine = Red Red | White White
data Red = Merlot | Syrah
data White = SauvignonBlanc | Riesling | PinotNoir
data Entree = Pork | Chicken | Tuna deriving (Eq, Enum, Bounded)
data SideDish = GarlicBread | MozzarellaSticks | Caviar deriving (Eq, Enum,
Bounded)
instance Show Wine where ...
instance Show Red where ...
...
>
> Then, I want to write a Haskell function that takes two lists; e.g.,
>
> ["pork", "chicken", "tuna"]
>
> and
>
> ["garlic bread", "mozzarella sticks", "caviar"]
>
> and that constructs a three-tuple of the following type:
>
> (Entree, SideDish, Wine)
>
> such that which Wine is chosen depends on the particular combination
> of Entree and SideDish, with respect to the following rules:
>
> ("pork", "garlic bread") -> ("pork", "garlic bread", "Merlot")
> ("pork", "mozzarella sticks") -> ("pork", "mozzarella sticks",
> "Merlot")
> ("pork", "caviar") -> ("pork", "Beluga", "Sauvignon Blanc")
> ("chicken", "garlic bread") -> ("chicken", "garlic bread", "Merlot")
> ("chicken", "mozzarella sticks") -> ("chicken", "mozzarella sticks",
> "Sauvignon Blanc")
> ("chicken", "caviar") -> ("chicken", "caviar", "Merlot")
> ("tuna", "garlic bread") -> ("tuna", "garlic bread", "Sauvignon
> Blanc")
> ("tuna", "mozzarella sticks") -> ("tuna", "mozzarella sticks",
> "Merlot")
> ("tuna", "caviar") -> ("tuna", "caviar", "Merlot")
selectWine :: Entree -> SideDish -> Wine
selectWine Pork sd
= case sd of
Caviar -> White SauvignonBlanc
_ -> Red Merlot
selectWine Chicken sd
= case sd of
MozzarellaSticks -> White SauvignonBlanc
_ -> Red Merlot
selectWine Tuna sd
= case sd of
GarlicBread -> White SauvignonBlanc
_ -> Red Merlot
options :: [(Entree,SideDish,Wine)]
options = [(e,s,selectWine e s) | e <- [minBound .. maxBound], s <- [minBound
.. maxBound]]
>
> So far, I have written the following Haskell code:
>
> module Wine where
>
> data Wine = Red | White
> data Red = "Merlot"
> data White = "Sauvignon Blanc"
> data Entree = "pork" | "chicken" | "tuna"
> data SideDish = "garlic bread" | "mozzarella sticks" | "caviar"
>
> wine :: [Entree] -> [SideDish] -> [(Entree, SideDish, Wine)]
> wine entree sidedish
>
> | entree == "pork" =
> |
> | sidedish == "garlic bread" = ("pork", "garlic bread",
>
> "Merlot")
>
> | sidedish == "mozzarella sticks" = ("pork", "mozzarella
>
> sticks", "Merlot")
>
> | sidedish == "caviar" = ("pork", "caviar", "Sauvignon Blanc")
> |
> | entree == "chicken" =
> |
> | sidedish == "garlic bread" = ("chicken", "garlic bread",
>
> "Merlot")
>
> | sidedish == "mozzarella sticks" = ("chicken", "mozzarella
>
> sticks", "Sauvignon Blanc")
>
> | sidedish == "caviar"= ("chicken", "caviar", "Merlot")
> |
> | entree == "tuna" =
> |
> | sidedish == "garlic bread" = ("tuna", "garlic bread",
>
> "Sauvignon Blanc")
>
> | sidedish == "mozzarella sticks" = ("tuna", "mozzarella
>
> sticks", "Merlot")
>
> | sidedish == "caviar"= ("tuna", "caviar", "Merlot")
>
> However, when I load this code into GHCi, I get the following error
> message:
>
> [1 of 1] Compiling Wine ( wine.hs, interpreted )
>
> wine.hs:18:11: parse error on input `"'
> Failed, modules loaded: none.
> Prelude>
>
> I discovered the following seemingly relevant HaskellWiki page, but am
> not sure about how to apply the advice in it to this problem, because
> the advice does not seem sufficiently specific or detailed:
>
> List instance - HaskellWiki
> http://www.haskell.org/haskellwiki/List_instance
This isn't relevant here, you're not trying to create an instance of some
typeclass for a list-type.
>
> What would be a good way to overcome this error?
>
> -- Benjamin L. Russell
>
See above, but I suggest rethinking your menu :)
More information about the Beginners
mailing list