
On Thu, 23 Oct 2008 17:51:59 +0900, Benjamin L.Russell
[...]
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?
[...]
After researching type classes somewhat, I discovered that in "data Wine = Red Red | White White," the second terms "Red" and "White" were arguments to their respective first terms of the same name. With this information, I then rewrote the type definition and type constructor for Wine as follows: data Wine = Red Red | White White instance Show Wine where show (Red Merlot) = "Merlot" show (Red Syrah) = "Syrah" show (Red Port) = "Port" show (White SauvignonBlanc) = "Sauvignon Blanc" show (White Riesling) = "Riesling" show (White PinotNoir) = "Pinot Noir" Here is my revised program, which seems to work fine this time: 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 show (Red Merlot) = "Merlot" show (Red Syrah) = "Syrah" show (Red Port) = "Port" show (White SauvignonBlanc) = "Sauvignon Blanc" show (White Riesling) = "Riesling" show (White PinotNoir) = "Pinot Noir" 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]] When I run it in GHCi (with my Haskell program file renamed as wine2.hs), these are the results: Prelude> :l wine2.hs [1 of 1] Compiling Wine ( wine2.hs, interpreted ) Ok, modules loaded: Wine. *Wine> options [(Kobe Beef,Buttered Potatoes,Merlot),(Kobe Beef,Brie Cheese,Merlot),(Kobe Beef, Greek Salad,Sauvignon Blanc),(Lemon Chicken,Buttered Potatoes,Syrah),(Lemon Chic ken,Brie Cheese,Riesling),(Lemon Chicken,Greek Salad,Syrah),(Steamed Salmon,Butt ered Potatoes,Pinot Noir),(Steamed Salmon,Brie Cheese,Port),(Steamed Salmon,Gree k Salad,Port)] *Wine> These results seem correct. Thank you for your help! -- Benjamin L. Russell