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 beginners-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. Re: Parsec, parsing 'free text' (Franco) 2. Type Tree variables (bahad?r altan) 3. Re: Type Tree variables (Ozgur Akgun) 4. Re: Type Tree variables (Ozgur Akgun) 5. Re: Type Tree variables (Ozgur Akgun) 6. Fw: Type Tree variables (bahad?r altan) ---------------------------------------------------------------------- Message: 1 Date: Sun, 11 Mar 2012 12:45:26 +0100 From: Franco <franc...@gmx.com> Subject: Re: [Haskell-beginners] Parsec, parsing 'free text' To: Stephen Tetley <stephen.tet...@gmail.com> Cc: beginners@haskell.org Message-ID: <20120311124526.6f4aca5b6d74bced689ba...@gmx.com> Content-Type: text/plain; charset=US-ASCII Indeed the second solution is more elegant (and the examples very simple to follow, they should be added to Parsec's documentation!). For the records, before reading this I was using the ductape solution below: 161 162 -- take a string till t, on that runs parser p. 163 -- The last parameters sets wheter t will be consumed or 164 -- not. FILE HANDLING FOR ERRORS? 165 parseTill :: Parser a -> Parser b -> Parser b 166 parseTill ter p = manyTill anyChar ter >>= \sndPar -> 167 case parse p "" sndPar of 168 Left a -> fail "todo: check how error msg are propagted" 169 Right b -> return b Thanks again -F On Sun, 11 Mar 2012 10:53:50 +0000 Stephen Tetley <stephen.tet...@gmail.com> wrote: > Hi Franco > > Actually the simple case of finding formatting tags in free text was > easier in Parsec than my email last night suggested. Perhaps it is > artificially easy because you can identify tag start and ends with a > single character so you can use `satisfy`. > > import Text.Parsec > import Text.Parsec.String > import Control.Applicative hiding ( (<|>), many ) > > > data Text1 = FreeText String | Formatted String > deriving (Eq,Ord,Show) > > type Text = [Text1] > > runText :: String -> Either ParseError Text > runText = runP lexer () "no-input" > > > notLAngle :: Char -> Bool > notLAngle = (/= '<') > > notRAngle :: Char -> Bool > notRAngle = (/= '>') > > lexer :: Parser Text > lexer = many (formatted <|> free) > > formatted :: Parser Text1 > formatted = Formatted <$> > between (char '<') (char '>') (many1 (satisfy notRAngle)) > > free :: Parser Text1 > free = FreeText <$> many1 (satisfy notLAngle) > > > > demo01 = runText "[ someconditions | this is some <red - formatted> text.]" > demo02 = runText "<red - formatted> more text." -- Franco <franc...@gmx.com> ------------------------------ Message: 2 Date: Sun, 11 Mar 2012 13:36:32 +0000 (GMT) From: bahad?r altan <doal...@yahoo.co.uk> Subject: [Haskell-beginners] Type Tree variables To: "beginners@haskell.org" <beginners@haskell.org> Message-ID: <1331472992.51357.yahoomail...@web171602.mail.ir2.yahoo.com> Content-Type: text/plain; charset="iso-8859-1" Hello everyone, I'm trying to write a generic function which works on binary trees. For example my function works on just first three nodes of ?a binary tree, but the tree is bigger, I don't care the other nodes. Like this : function Node a (Node b Subtree Subtree)??(Node c Subtree Subtree)?? The problem is I couldn't find a way to represent "Subtree" structures. A guy here told me to use variables of ?type Tree, but I don't know how to create them. Can you tell me how to solve this problem?? Thanks a lot. -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://www.haskell.org/pipermail/beginners/attachments/20120311/394cb429/attachment-0001.htm> ------------------------------ Message: 3 Date: Sun, 11 Mar 2012 14:05:38 +0000 From: Ozgur Akgun <ozgurak...@gmail.com> Subject: Re: [Haskell-beginners] Type Tree variables To: bahad?r altan <doal...@yahoo.co.uk> Cc: "beginners@haskell.org" <beginners@haskell.org> Message-ID: <calzazpcybzejiqbc4b8cy7af8ugocu0rm-+mmjnqvonxvvo...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" Hi, On 11 March 2012 13:36, bahad?r altan <doal...@yahoo.co.uk> wrote: > ... my function works on just first three nodes of a binary tree, but > the tree is bigger, I don't care the other nodes. Like this : > > function Node a (Node b Subtree Subtree) (Node c Subtree Subtree) > Assuming you use the following declaration for your data type: data Tree a = Leaf | Node a (Tree a) (Tree a) The syntax you are looking for is the following; where x, y and z are pattern variables. f :: Tree a -> (a,a,a) f (Node x (Node y _ _) (Node z _ _)) = (x,y,z) The above function is obviously partial. It would throw an exception if you apply it to anything which doesn't have the 'proper' shape. (e.g. "f Leaf") I don't know your background, but I'd suggest reading some introductory material if you are having trouble understanding the above snippets. LYAH is a pretty fun read: http://learnyouahaskell.com Hope this helps, Ozgur -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://www.haskell.org/pipermail/beginners/attachments/20120311/00068ae4/attachment-0001.htm> ------------------------------ Message: 4 Date: Sun, 11 Mar 2012 14:23:58 +0000 From: Ozgur Akgun <ozgurak...@gmail.com> Subject: Re: [Haskell-beginners] Type Tree variables To: bahad?r altan <doal...@yahoo.co.uk>, Haskell Beginners <beginners@haskell.org> Message-ID: <calzazpao_w0kobnteuan5u9hc6am_sh85mht2zg05986shq...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" (Replying to the list again) On 11 March 2012 14:15, bahad?r altan <doal...@yahoo.co.uk> wrote: > Hi, > Thanks for your reply. I tried your solution but my function becomes > like this and it gives an error on the right side : > function (Node a (Node b _ _) (Node c _ _)) = (Node c (Node a _ _) (Node b > _ _)) > This is not what you originally said though. If you need to refer to the subtrees, you just need to name them. (Instead of using _) [ not a very useful function, but still: ] f :: Tree a -> Tree a f (Node x (Node y y1 y2) (Node z z1 z2)) = Node x (Node y y1 y2) (Node z z1 z2) -- also think about the types: (x, y, z :: a) (y1, y2, z1, z2 :: Tree a) > The thing is I don't need to process the subtrees but I still have to > return them as an output. I still can't do that. And I read a few chapters > in LYAH, it was pretty good, I learned haskell from there. However, I think > the answer of this question isn't in LYAH. > Thanks.. > Bahadir > -- Ozgur Akgun -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://www.haskell.org/pipermail/beginners/attachments/20120311/3816a569/attachment-0001.htm> ------------------------------ Message: 5 Date: Sun, 11 Mar 2012 14:47:59 +0000 From: Ozgur Akgun <ozgurak...@gmail.com> Subject: Re: [Haskell-beginners] Type Tree variables To: bahad?r altan <doal...@yahoo.co.uk>, Haskell Beginners <beginners@haskell.org> Message-ID: <CALzazPBf+a=UMr=marneu8uwlldxxmcon0r+x5crytup4zy...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" Hi, Please reply to the list so others can contribute. On 11 March 2012 14:42, bahad?r altan <doal...@yahoo.co.uk> wrote: > I'm sorry if I mislead you, I think I didn't mention my problem clearly > there. I'm actually stuck in the position of declaring subtrees, I couldn't > find a solution to that. > > My function is like > > f :: Tree -> Tree > > And my tree structure is like > > data Tree = Empty | Branch Integer Tree Tree > > And When I write this which I learned from you : > (c1, c2, d1, d2, e1, e2 :: Tree) > > I get error. If you tell me How should I solve that I'd appreciate. > Hence I was suggesting to think about the types of the pattern variables. f (Branch x (Branch y y1 y2) (Branch z z1 z2)) = (x,y,y1,y2,z,z1,z2) This definition will work, and return a 7-tuple given a Tree. Try to load this into ghci and see what the type of f is (:t f). One hint: y and y1 aren't of the same type. Ozgur -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://www.haskell.org/pipermail/beginners/attachments/20120311/73dfbcad/attachment-0001.htm> ------------------------------ Message: 6 Date: Sun, 11 Mar 2012 14:53:03 +0000 (GMT) From: bahad?r altan <doal...@yahoo.co.uk> Subject: [Haskell-beginners] Fw: Type Tree variables To: "beginners@haskell.org" <beginners@haskell.org> Message-ID: <1331477583.98483.yahoomail...@web171606.mail.ir2.yahoo.com> Content-Type: text/plain; charset="iso-8859-1" ----- Forwarded Message ----- From: bahad?r altan <doal...@yahoo.co.uk> To: Ozgur Akgun <ozgurak...@gmail.com> Sent: Sunday, 11 March 2012, 16:42 Subject: Re: [Haskell-beginners] Type Tree variables I'm sorry if I mislead you, I think I didn't mention my problem clearly there. I'm actually stuck in the position of declaring subtrees, I couldn't find a solution to that. My function is like f :: Tree -> Tree And my tree structure is like? data Tree = Empty | Branch Integer Tree Tree? And When I write this which I learned from you :? (c1, c2, d1, d2, e1, e2 :: Tree) I get error. If you tell me How should I solve that I'd appreciate. Thanks, Bahadir ________________________________ From: Ozgur Akgun <ozgurak...@gmail.com> To: bahad?r altan <doal...@yahoo.co.uk>; Haskell Beginners <beginners@haskell.org> Sent: Sunday, 11 March 2012, 16:23 Subject: Re: [Haskell-beginners] Type Tree variables (Replying to the list again) On 11 March 2012 14:15, bahad?r altan <doal...@yahoo.co.uk> wrote: Hi,? >Thanks for your reply. I tried your solution but my function becomes ?like >this and it gives an error on the right side :?? function (Node a (Node b _ _) (Node c _ _))? =?(Node c (Node a _ _) (Node b _ _))? This is not what you originally said though. If you need to refer to the subtrees, you just need to name them. (Instead of using _) [ not a very useful function, but still: ] f :: Tree a -> Tree a f (Node x (Node y y1 y2) (Node z z1 z2)) =?Node x (Node y y1 y2) (Node z z1 z2) -- also think about the types: (x, y, z :: a) (y1, y2, z1, z2 :: Tree a) ? The thing is I don't need to process the subtrees but I still have to return them as an output. I still can't do that. And I read a few chapters in LYAH, it was pretty good, I learned haskell from there. However, I think the answer of this question isn't in LYAH.? >Thanks.. >Bahadir -- Ozgur Akgun -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://www.haskell.org/pipermail/beginners/attachments/20120311/b073ab81/attachment.htm> ------------------------------ _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners End of Beginners Digest, Vol 45, Issue 14 *****************************************