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

Benjamin L.Russell DekuDekuplex at Yahoo.com
Thu Oct 23 03:02:49 EDT 2008


On Thu, 23 Oct 2008 15:27:34 +0900, Benjamin L.Russell
<DekuDekuplex at Yahoo.com> wrote:

>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 entrees sidedishes
>    | entree <- entrees == "pork" = 
>        | sidedish <- sidedishes == "garlic bread" = ("pork", "garlic
>bread",
>"Merlot")
>        | sidedish <- sidedishes == "mozzarella sticks" = ("pork",
>"mozzarella
>sticks", "Merlot")
>        | sidedish <- sidedishes == "caviar" = ("pork", "caviar",
>"Sauvignon Blanc")
>    | entree <- entrees == "chicken" = 
>        | sidedish <- sidedishes == "garlic bread" = ("chicken",
>"garlic bread",
>"Merlot")
>        | sidedish <- sidedishes == "mozzarella sticks" = ("chicken",
>"mozzarella
>sticks", "Sauvignon Blanc")
>        | sidedish <- sidedishes == "caviar"= ("chicken", "caviar",
>"Merlot")
>    | entree <- entrees == "tuna" = 
>        | sidedish <- sidedishes == "garlic bread" = ("tuna", "garlic
>bread",
>"Sauvignon Blanc")
>        | sidedish <- sidedishes == "mozzarella sticks" = ("tuna",
>"mozzarella
>sticks", "Merlot")
>        | sidedish <- sidedishes == "caviar"= ("tuna", "caviar",
>"Merlot")

I just thought of a revision using the guards within an instance of
unlines and map.  This approach eliminates the need for the Wine, Red,
and White datatypes.  However, since two elements of type String need
to be extracted at each step, I am not sure of the syntax.  Would this
approach a solution?

module Wine where 
    
data Entree = "pork" | "chicken" | "tuna"
data SideDish = "garlic bread" | "mozzarella sticks" | "caviar"

wine :: Show a => [Entree] -> [SideDish] -> String
wine entrees sidedishes
    = unlines (map entree entrees) (map sidedish sidedishes) where
(entree Entree) (sidedish SideDish) = 
    | entree == "pork" = 
        | sidedish == "garlic bread" = "(" ++ show entree ++ ", " ++
show sidedish ++ ", Merlot)"
        | sidedish == "mozzarella sticks" = "(" ++ show entree ++ ", "
++ show sidedish ++ ", Merlot)"
        | sidedish == "caviar" = "(" ++ show entree ++ ", " ++ show
sidedish ++ ", Sauvignon Blanc)"
    | entree == "chicken" = 
        | sidedish == "garlic bread" = "(" ++ show entree ++ ", " ++
show sidedish ++ ", Merlot)"
        | sidedish == "mozzarella sticks" = "(" ++ show entree ++ ", "
++ show sidedish ++ ", Sauvignon Blanc)"
        | sidedish == "caviar"= "(" ++ show entree ++ ", " ++ show
sidedish ++ ", Merlot)"
    | entree == "tuna" = 
        | sidedish == "garlic bread" = "(" ++ show entree ++ ", " ++
show sidedish ++ ", Sauvignon Blanc)"
        | sidedish == "mozzarella sticks" = "(" ++ show entree ++ ", "
++ show sidedish ++ ", Merlot)"
        | sidedish == "caviar"= "(" ++ show entree ++ ", " ++ show
sidedish ++ ", Merlot)"

-- Benjamin L. Russell



More information about the Beginners mailing list