Send Beginners mailing list submissions to beginners@haskell.org To subscribe or unsubscribe via the World Wide Web, visit http://www.haskell.org/mailman/listinfo/beginners or, via email, send a message with subject or body 'help' to [EMAIL PROTECTED]
You can reach the person managing the list at [EMAIL PROTECTED] When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. Re: what type is 'Val 9' when 'Val Int' a ctor for 'Expr e'? (Daniel Fischer) 2. how to define a user datatype consisting of instances of String? (Benjamin L.Russell) 3. Re: how to define a user datatype consisting of instances of String? (Benjamin L.Russell) 4. Re: how to define a user datatype consisting of instances of String? (Benjamin L.Russell) 5. Re: how to define a user datatype consisting of instances of String? (Daniel Fischer) 6. Re: how to define a user datatype consisting of instances of String? (Benjamin L.Russell) 7. Re: A Pascal-like Language Compiler ( Joel Bj?rnson ) ---------------------------------------------------------------------- Message: 1 Date: Thu, 23 Oct 2008 06:10:21 +0200 From: Daniel Fischer <[EMAIL PROTECTED]> Subject: Re: [Haskell-beginners] what type is 'Val 9' when 'Val Int' a ctor for 'Expr e'? To: Larry Evans <[EMAIL PROTECTED]> Cc: Beginners@haskell.org Message-ID: <[EMAIL PROTECTED]> Content-Type: text/plain; charset="iso-8859-1" Am Donnerstag, 23. Oktober 2008 05:36 schrieb Larry Evans: > Thanks Deaniel. The fog in my head begins to clear. > I took Antoine's suggestion and got: > <---cut here --- > GHCi, version 6.8.2: http://www.haskell.org/ghc/ :? for help > Loading package base ... linking ... done. > Prelude> :load > "/home/evansl/prog_dev/haskell/my-code/uniplate.try.phantom.hs" > [1 of 1] Compiling Main ( > /home/evansl/prog_dev/haskell/my-code/uniplate.try.phantom.hs, interpreted > ) Ok, modules loaded: Main. > *Main> let val_9 = Val 9 > Loading package mtl-1.1.0.0 ... linking ... done. > Loading package array-0.1.0.0 ... linking ... done. > Loading package containers-0.1.0.1 ... linking ... done. > Loading package uniplate-1.2.0.1 ... linking ... done. > *Main> :t val_9 > val_9 :: Expr e > *Main> print val_9 > Val 9 > *Main> > > >---cut here--- > > I guess the phantom type mentioned in Antoine's post is the e in: > > val_9::Expr e > > ? Exactly. That type is never used in any value of type (Expr e), therefore it is called a 'phantom' type. It only serves as a tag to prevent mixing Expr's with different type parameters. Cheers, Daniel ------------------------------ Message: 2 Date: Thu, 23 Oct 2008 15:07:09 +0900 From: Benjamin L.Russell <[EMAIL PROTECTED]> Subject: [Haskell-beginners] how to define a user datatype consisting of instances of String? To: beginners@haskell.org Message-ID: <[EMAIL PROTECTED]> Content-Type: text/plain; charset=us-ascii 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" 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") 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 What would be a good way to overcome this error? -- Benjamin L. Russell ------------------------------ Message: 3 Date: Thu, 23 Oct 2008 15:22:15 +0900 From: Benjamin L.Russell <[EMAIL PROTECTED]> Subject: [Haskell-beginners] Re: how to define a user datatype consisting of instances of String? To: beginners@haskell.org Message-ID: <[EMAIL PROTECTED]> Content-Type: text/plain; charset=us-ascii On Thu, 23 Oct 2008 15:07:09 +0900, Benjamin L.Russell <[EMAIL PROTECTED]> 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 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") Sorry, I forgot to take care of list comprehension. I'm not quite sure how to extract elements of both lists at the same time in a nested guard. Here's a slight revision; would this work? 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 <- sidedieshes == "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 <- sidedieshes == "garlic bread" = ("chicken", "garlic bread", "Merlot") | sidedish <- sidedieshes == "mozzarella sticks" = ("chicken", "mozzarella sticks", "Sauvignon Blanc") | sidedish <- sidedieshes == "caviar"= ("chicken", "caviar", "Merlot") | entree <- entrees == "tuna" = | sidedish <- sidedieshes == "garlic bread" = ("tuna", "garlic bread", "Sauvignon Blanc") | sidedish <- sidedieshes == "mozzarella sticks" = ("tuna", "mozzarella sticks", "Merlot") | sidedish <- sidedieshes == "caviar"= ("tuna", "caviar", "Merlot") -- Benjamin L. Russell ------------------------------ Message: 4 Date: Thu, 23 Oct 2008 15:27:34 +0900 From: Benjamin L.Russell <[EMAIL PROTECTED]> Subject: [Haskell-beginners] Re: how to define a user datatype consisting of instances of String? To: beginners@haskell.org Message-ID: <[EMAIL PROTECTED]> Content-Type: text/plain; charset=us-ascii On Thu, 23 Oct 2008 15:22:17 +0900, Benjamin L. Russell <[EMAIL PROTECTED]> wrote: >Here's a slight revision; would this work? > >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 <- sidedieshes == "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 <- sidedieshes == "garlic bread" = ("chicken", >"garlic bread", >"Merlot") > | sidedish <- sidedieshes == "mozzarella sticks" = ("chicken", >"mozzarella >sticks", "Sauvignon Blanc") > | sidedish <- sidedieshes == "caviar"= ("chicken", "caviar", >"Merlot") > | entree <- entrees == "tuna" = > | sidedish <- sidedieshes == "garlic bread" = ("tuna", "garlic >bread", >"Sauvignon Blanc") > | sidedish <- sidedieshes == "mozzarella sticks" = ("tuna", >"mozzarella >sticks", "Merlot") > | sidedish <- sidedieshes == "caviar"= ("tuna", "caviar", >"Merlot") Sorry; my earlier revision contained some typos where "sidedishes" was misspelled as "sidedieshes." Here is a corrected version: 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") -- Benjamin L. Russell ------------------------------ Message: 5 Date: Thu, 23 Oct 2008 08:47:24 +0200 From: Daniel Fischer <[EMAIL PROTECTED]> Subject: Re: [Haskell-beginners] how to define a user datatype consisting of instances of String? To: Benjamin L.Russell <[EMAIL PROTECTED]>, beginners@haskell.org Message-ID: <[EMAIL PROTECTED]> Content-Type: text/plain; charset="iso-8859-1" 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 :) ------------------------------ Message: 6 Date: Thu, 23 Oct 2008 16:02:49 +0900 From: Benjamin L.Russell <[EMAIL PROTECTED]> Subject: [Haskell-beginners] Re: how to define a user datatype consisting of instances of String? To: beginners@haskell.org Message-ID: <[EMAIL PROTECTED]> Content-Type: text/plain; charset=us-ascii On Thu, 23 Oct 2008 15:27:34 +0900, Benjamin L.Russell <[EMAIL PROTECTED]> 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 ------------------------------ Message: 7 Date: Thu, 23 Oct 2008 10:23:29 +0200 From: " Joel Bj?rnson " <[EMAIL PROTECTED]> Subject: Re: [Haskell-beginners] A Pascal-like Language Compiler To: "Pranesh Srinivasan" <[EMAIL PROTECTED]> Cc: Hasekll - Beginners <beginners@haskell.org> Message-ID: <[EMAIL PROTECTED]> Content-Type: text/plain; charset=ISO-8859-1 On Thu, Oct 23, 2008 at 12:18 AM, Pranesh Srinivasan <[EMAIL PROTECTED]> wrote: > > That seems like a very nice scheme to follow. I had a similar method in > mind. Step 3 is what I am really worried about. How easy/difficult will > it be in a pure func language, to "transform" the sytnax tree. I think the pureness is actually an advantage here, since the transformation is a function from one syntax tree to another. If however, you feel the need of keeping a state and you don't wanna pass around this explicitly, you may consider using a state monad. > I have to take a deeper look at BNFC. But from an initial look, it seems > way too powerful for me to use? At least as powerful as yacc. And that > with Haskell, should give me a very good toolset-advantage? If the project is about writing parsers then using BNFC would probably be considered cheating. But for practical purposes I think it's a great tool that would save you a lot of work. - Joel ------------------------------ _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners End of Beginners Digest, Vol 4, Issue 10 ****************************************