[Haskell-beginners] Re: how to define a user datatype consisting of instances of String?

Benjamin L.Russell DekuDekuplex at Yahoo.com
Thu Oct 23 04:51:59 EDT 2008


On Thu, 23 Oct 2008 08:47:24 +0200, Daniel Fischer
<daniel.is.fischer at web.de> wrote:

>[...]
>
>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 ...
>...
>
>[...]
>
>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]]
>
>[...]
>
>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 :)

After taking your advice into account, I have rewritten the wine.hs
program as follows, but am unable to figure out what to write for the
type constructor for Wine.  In particular, I do not understand why the
user type definition for Wine is "Red Red | White White" instead of
just "Red | White."

Should I fill out "instance Show Wine where ..." as something similar
to

instance Show Wine where
    show Red = ...
    show White = ...

and then supply a conditional after "show Red = ..." and "show White =
...," or do I need to supply something else there?

Here's my revised code so far (the "instance Show Wine where ..." part
is unfinished):

module Wine where 
    
data Wine = Red Red | White White
data Red = Merlot | Syrah | Port
data White = SauvignonBlanc | Riesling | PinotNoir
data Entree = KobeBeef | LemonChicken | SteamedSalmon deriving (Eq,
Enum, Bounded)
data SideDish = ButteredPotatoes | BrieCheese | GreekSalad deriving
(Eq, Enum, Bounded)

instance Show Wine where ...
 
instance Show Red where
    show Merlot = "Merlot"
    show Syrah = "Syrah"
    show Port = "Port"

instance Show White where
    show SauvignonBlanc = "Sauvignon Blanc"
    show Riesling = "Riesling"
    show PinotNoir = "Pinot Noir"

instance Show Entree where 
    show KobeBeef = "Kobe Beef"
    show LemonChicken = "Lemon Chicken"
    show SteamedSalmon = "Steamed Salmon"

instance Show SideDish where
    show ButteredPotatoes = "Buttered Potatoes"
    show BrieCheese = "Brie Cheese"
    show GreekSalad = "Greek Salad"

selectWine :: Entree -> SideDish -> Wine
selectWine KobeBeef sd 
    = case sd of
	GreekSalad -> White SauvignonBlanc
	_ -> Red Merlot
selectWine LemonChicken sd
    = case sd of
	BrieCheese -> White Riesling
	_ -> Red Syrah
selectWine SteamedSalmon sd
    = case sd of
	ButteredPotatoes -> White PinotNoir
	_ -> Red Port

options :: [(Entree,SideDish,Wine)]
options = [(e,s,selectWine e s) | e <- [minBound .. maxBound], s <-
[minBound .. maxBound]]

-- Benjamin L. Russell



More information about the Beginners mailing list