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. type hierarchies / records (R?zvan Rotaru) 2. Re: type hierarchies / records (Kim-Ee Yeoh) 3. Confused about lazy IO (Jacek Dudek) 4. Re: Confused about lazy IO (Gesh hseG) 5. Re: Confused about lazy IO (Kim-Ee Yeoh) ---------------------------------------------------------------------- Message: 1 Date: Thu, 14 Mar 2013 17:58:52 +0200 From: R?zvan Rotaru <razvan.rot...@gmail.com> Subject: [Haskell-beginners] type hierarchies / records To: beginners@haskell.org Message-ID: <CAP33cnagv24_pQEDDJifo-4phz1BndQA3NVi4aKA7sa=hsx...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" Dear Haskelleers, I suspect my problem has been discussed many times and under many forms, so please don't shoot me for asking. I could not find an acceptable solution yet, and beeing as stubborn as i am, I still hope the solution is eluding me because of my poor haskell knowlegde. So here I am posting my question on this mailing list. Basically I am trying to GUI Widgets using haskell types (Buttons, Textfields, etc.). 1/ Using Records is not acceptable because of the name clashes between attribute names. I could use prefixes and write functions in typeclasses for accessor, but it's ugly and a lot of work. 2/ I decided to keep the attributes in Maps, so basically a textfield would be data Textfield = Textfield (Map.Map TextfieldProps String) The problem is: how to define TextfieldProps? It should be something like: data TextfieldProps = Id | Label | Value But then, buttons also have Id's and labels. So following is not accepted anymore: data ButtonProps = Id | Label | OnClick Ideas and suggestions are much appreciated. Thank You, R?zvan -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://www.haskell.org/pipermail/beginners/attachments/20130314/e23ecbc0/attachment-0001.htm> ------------------------------ Message: 2 Date: Fri, 15 Mar 2013 00:58:59 +0700 From: Kim-Ee Yeoh <k...@atamo.com> Subject: Re: [Haskell-beginners] type hierarchies / records To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Message-ID: <capy+zdqkvlqm4tnjkynsy80arpui6nrdhwkgrjynpj3xqtw...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" On Thu, Mar 14, 2013 at 10:58 PM, R?zvan Rotaru <razvan.rot...@gmail.com>wrote: > 2/ I decided to keep the attributes in Maps, so basically a textfield > would be > > data Textfield = Textfield (Map.Map TextfieldProps String) > Is defining data here even necessary? Even newtype seems superfluous. A plain "shorthand" type synonym surely fits the bill? > The problem is: how to define TextfieldProps? It should be something like: > > data TextfieldProps = Id | Label | Value > > But then, buttons also have Id's and labels. So following is not accepted > anymore: > > data ButtonProps = Id | Label | OnClick > Perhaps data TextfieldProps = TextId | TextLabel | TextValue -- and similarly for ButtonProps? I confess to not fully understanding the GUI modelling attempted here, which isn't to say that you aren't on the right track. To help you and help us help you better, one approach known to be successful is to sketch out the code (better yet, the type signatures!) of what you'd like to write, e.g. how are TextfieldProps and ButtonProps used? What functions act on them? -- Kim-Ee -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://www.haskell.org/pipermail/beginners/attachments/20130315/4a73ac74/attachment-0001.htm> ------------------------------ Message: 3 Date: Thu, 14 Mar 2013 18:52:43 -0400 From: Jacek Dudek <jzdu...@gmail.com> Subject: [Haskell-beginners] Confused about lazy IO To: beginners@haskell.org Message-ID: <cajxg2_fhaecdentzs4gbl7jgk3qozgvpbcaghtl_tfzxff1...@mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1 Q: When trying to compose the following IO actions: readFile fileName >>= \ contents -> writeFile fileName (f contents) I get an exception: openFile: permission denied. However the following works: readFile fileName >>= \ contents -> (mapM_ putStrLn . lines) contents >> writeFile fileName (f contents) I'm assuming it has something to do with lazy IO, and the second action in the second version forces fileName to be read completely and to be closed. Why do I need to do that? I thought lazy IO was implemented in such a way that you were safe to INTERPRET the IO action as having been fully performed. (And so I would have been safe to interpret that, in the first version, in the first action, the file was released.) If that's not the case, then: (1) What is the proper way to reason about lazy IO? (2) Is there some action less arbitrary that what I cooked up in the second version that explicitly instructs to release a file or some other resource? Regards, Jacek. ------------------------------ Message: 4 Date: Fri, 15 Mar 2013 01:16:14 +0200 From: Gesh hseG <g...@gesh.uni.cx> Subject: Re: [Haskell-beginners] Confused about lazy IO To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Message-ID: <CACS5XqMGiO+epR4K31=nqum4a6rtldjaz_wojnzekmrf5s6...@mail.gmail.com> Content-Type: text/plain; charset="utf-8" I was bitten by this a couple of weeks ago. What I was told back then boils down to the following: - The proper way to reason about lazy IO is that nothing that doesn't need to be read is ever read - in much the same way as the only part of an infinite list that gets evaluated is the prefix that you use in your program. - When reading and writing to the same file, it is better in general to make the program transactional by writing to a temp file and then renaming it to the old file. HTH, Gesh On Fri, Mar 15, 2013 at 12:52 AM, Jacek Dudek <jzdu...@gmail.com> wrote: > Q: When trying to compose the following IO actions: > > readFile fileName >>= \ contents -> > writeFile fileName (f contents) > > I get an exception: openFile: permission denied. > However the following works: > > readFile fileName >>= \ contents -> > (mapM_ putStrLn . lines) contents >> > writeFile fileName (f contents) > > I'm assuming it has something to do with lazy IO, and the second > action in the second version forces fileName to be read completely and > to be closed. > > Why do I need to do that? I thought lazy IO was implemented in such a > way that you were safe to INTERPRET the IO action as having been fully > performed. (And so I would have been safe to interpret that, in the > first version, in the first action, the file was released.) > > If that's not the case, then: > (1) What is the proper way to reason about lazy IO? > (2) Is there some action less arbitrary that what I cooked up in the > second version that explicitly instructs to release a file or some > other resource? > > Regards, Jacek. > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://www.haskell.org/pipermail/beginners/attachments/20130315/6ff80854/attachment-0001.htm> ------------------------------ Message: 5 Date: Fri, 15 Mar 2013 06:24:35 +0700 From: Kim-Ee Yeoh <k...@atamo.com> Subject: Re: [Haskell-beginners] Confused about lazy IO To: The Haskell-Beginners Mailing List - Discussion of primarily beginner-level topics related to Haskell <beginners@haskell.org> Message-ID: <CAPY+ZdTsTsXMObZTS5dgVWZuKxEc-jq8QSSS6Bo+2S=f_pq...@mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1 On Fri, Mar 15, 2013 at 5:52 AM, Jacek Dudek <jzdu...@gmail.com> wrote: > readFile fileName >>= \ contents -> > writeFile fileName (f contents) You're reading and writing to the /same/ file back to back. With Lazy I/O. Those two just don't mix. The "permission denied" probably stems from the attempted write clashing with the previous exclusive read handle. > I thought lazy IO was implemented in such a way that you were safe to > INTERPRET the IO action as having been fully performed. Nope. The usual gotcha lying for the unwary is do h <- openFile fname1 ReadMode s <- hGetContents h hClose h writeFile fname2 s In the case fname1==fname2, a different surprise lies in store as you've just witnessed. Importing System.IO.Strict from the strict package should fix all of the above. -- Kim-Ee ------------------------------ _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners End of Beginners Digest, Vol 57, Issue 22 *****************************************