From hyangfji at gmail.com Thu Oct 1 18:13:38 2009 From: hyangfji at gmail.com (Hong Yang) Date: Thu Oct 1 17:51:30 2009 Subject: [Haskell-beginners] Help with Data.Time Message-ID: I want to calculate the length of time between time ta and time tb. Time ta is a String looking like "N:NNAM" or "N:NNPM." I need tb to be 9:30AM on the current date. I think the flow should be 1) parse ta to utcTimeA :: UTCTime 2) get current date and form tb to utcTimeB :: UTCTime 3) call "diffUTCTime utcTimeA utcTimeB" So my program looks like as follows in a "do" expression: let utcTimeA = fromJust (parseTime defaultTimeLocale "%R%P" ta :: Maybe UTCTime) currentTime <- getCurrentTime let day = utctDay currentTime let utcTimeB = UTCTime day "09:30:00.0000000 UTC" let timeElapsed = diffUTCTime utcTimeA utcTimeB There are two difficulties I could not get around: 1) It seems that parseTime returns "Nothing". Why? 2) I knew "09:30:00.0000000 UTC" in the statement of "let utcTimeB ..." is incorrect, because it is a String instead of expected DiffTime type. After consulting Data.Time module (it just say "Data DiffTime"), I still cannot figure out how to get utcTimeB. Can someone help me? Thanks, Hong -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20091001/44b4fa9e/attachment.html From daniel.is.fischer at web.de Thu Oct 1 21:33:34 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Thu Oct 1 21:12:39 2009 Subject: [Haskell-beginners] Help with Data.Time In-Reply-To: References: Message-ID: <200910020333.35081.daniel.is.fischer@web.de> Am Freitag 02 Oktober 2009 00:13:38 schrieb Hong Yang: > I want to calculate the length of time between time ta and time tb. > Time ta is a String looking like "N:NNAM" or "N:NNPM." > I need tb to be 9:30AM on the current date. > > I think the flow should be > 1) parse ta to utcTimeA :: UTCTime > 2) get current date and form tb to utcTimeB :: UTCTime > 3) call "diffUTCTime utcTimeA utcTimeB" > > So my program looks like as follows in a "do" expression: > There are two difficulties I could not get around: > 1) It seems that parseTime returns "Nothing". Why? parseTime expects two digits for the hours: Prelude Data.Time System.Locale> parseTime defaultTimeLocale "%R%P" "09:30AM" :: MaybeUTCTime Just 1970-01-01 09:30:00 UTC > 2) I knew "09:30:00.0000000 UTC" in the statement of "let utcTimeB ..." is > incorrect, because it is a String instead of expected DiffTime type. After > consulting Data.Time module (it just say "Data DiffTime"), I still cannot > figure out how to get utcTimeB. > > let utcTimeA = fromJust (parseTime defaultTimeLocale "%R%P" ta :: Maybe > UTCTime) Note that that will be the time of day on the first of January 1970, probably not what you want. If you want it to have the current day, change the day like below, otherwise you'd have to give the date in the string to parse, too > currentTime <- getCurrentTime let Just temp = parseTime defaultTimeLocale "%RP" "09:30AM" utcTimeB = currentTime{ utctDayTime = utctDayTime temp } -- utcTimeB = temp{ utctDay = utctDay currentTime } is another option timeElapsed = diffUTCTime utcTimeA utcTimeB > let day = utctDay currentTime > let utcTimeB = UTCTime day "09:30:00.0000000 UTC" > let timeElapsed = diffUTCTime utcTimeA utcTimeB > > > Can someone help me? > > Thanks, > > Hong From timothyea at comcast.net Fri Oct 2 00:36:03 2009 From: timothyea at comcast.net (Tim Attwood) Date: Fri Oct 2 00:14:30 2009 Subject: [Haskell-beginners] Re: Help with Data.Time In-Reply-To: <200910020333.35081.daniel.is.fischer@web.de> References: <200910020333.35081.daniel.is.fischer@web.de> Message-ID: > Prelude Data.Time System.Locale> parseTime defaultTimeLocale "%R%P" > "09:30AM" :: > MaybeUTCTime > Just 1970-01-01 09:30:00 UTC Prelude Data.Time System.Locale> parseTime defaultTimeLocale "%R%P" "09:30AM" :: Maybe UTCTime Nothing I suspect the parser malfunction is an issue with POSIX and Windows. 1970-01-01 00:00 UTC is the POSIX zero time... From mklklk at hotmail.com Sat Oct 3 09:19:18 2009 From: mklklk at hotmail.com (Kui Ma) Date: Sat Oct 3 08:57:05 2009 Subject: [Haskell-beginners] examples of Crypto In-Reply-To: <29bf512f0909281036q554c0b51h48bbae7df2c9a2fd@mail.gmail.com> References: <305228b20909272008g3304b53sb890b95424623bf5@mail.gmail.com> Message-ID: This is exactly what I need. It helped me learn how to use the symmetry encryption algorithm. Thank you very much! Regards, Kui ----------------------------------------------------------------------------------------------------------------- I use it in clientsession: http://hackage.haskell.org/package/clientsession. Let me know if I can be of some assistance. Michael 2009/9/28 Kui Ma Hello, I want some examples to learn the package of Crypto, anyone knows where I can find them? Thank you in advance! Regards, kui Invite your mail contacts to join your friends list with Windows Live Spaces. It's easy! Try it! _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners _________________________________________________________________ Windows Live Hotmail: Your friends can get your Facebook updates, right from Hotmail?. http://www.microsoft.com/middleeast/windows/windowslive/see-it-in-action/social-network-basics.aspx?ocid=PID23461::T:WLMTAGL:ON:WL:en-xm:SI_SB_4:092009 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20091003/99a2aa9d/attachment.html From michael at snoyman.com Sat Oct 3 14:40:46 2009 From: michael at snoyman.com (Michael Snoyman) Date: Sat Oct 3 14:18:32 2009 Subject: [Haskell-beginners] examples of Crypto In-Reply-To: References: <305228b20909272008g3304b53sb890b95424623bf5@mail.gmail.com> <29bf512f0909281036q554c0b51h48bbae7df2c9a2fd@mail.gmail.com> Message-ID: <29bf512f0910031140v25afe9c8w2e39f328ac26728@mail.gmail.com> No problem, any time ;) Michael On Sat, Oct 3, 2009 at 3:19 PM, Kui Ma wrote: > > This is exactly what I need. It helped me learn how to use the symmetry > encryption algorithm. Thank you very much! > > Regards, > Kui > > ----------------------------------------------------------------------------------------------------------------- > > > I use it in clientsession: > http://hackage.haskell.org/package/clientsession. Let me know if I can be > of some assistance. > > Michael > > 2009/9/28 Kui Ma > > > Hello, > > I want some examples to learn the package of Crypto, anyone knows where I > can find them? Thank you in advance! > > Regards, > kui > > ------------------------------ > Invite your mail contacts to join your friends list with Windows Live > Spaces. It's easy! Try it! > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > > > ------------------------------ > Windows Live Hotmail: Your friends can get your Facebook updates, right > from Hotmail?. > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20091003/70beb1d3/attachment.html From john.moore54 at gmail.com Sat Oct 3 15:07:50 2009 From: john.moore54 at gmail.com (John Moore) Date: Sat Oct 3 14:45:37 2009 Subject: [Haskell-beginners] main Message-ID: <6656E51F7BD24292A0296C70F2868EDE@johnpmoore1> Hi, I must be doing something wrong when I run a program it comes up on the screen as OK module loaded Main *Main> But it will not do what I program to do. What do I enter after *main> I tried putting in the arguments but ot just returns the arguments. For example: signum x | x < 0 = -1 | x == 0 = 0 | x > 0 = 1 Enter 5 it returns 5 What have I missed? Regards John -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20091003/914ab9c2/attachment.html From keithshep at gmail.com Sat Oct 3 15:36:32 2009 From: keithshep at gmail.com (Keith Sheppard) Date: Sat Oct 3 15:14:18 2009 Subject: [Haskell-beginners] main In-Reply-To: <6656E51F7BD24292A0296C70F2868EDE@johnpmoore1> References: <6656E51F7BD24292A0296C70F2868EDE@johnpmoore1> Message-ID: <92e42b740910031236n6ddb80cfha853c1a38799f6b@mail.gmail.com> Hello, if you enter ":h" at the prompt it gives all of the available ghci commands. I think this is the one you're looking for :main [ ...] run the main function with the given arguments -Keith On Sat, Oct 3, 2009 at 3:07 PM, John Moore wrote: > Hi, > ??? I must be doing something wrong when I run a program it comes up on the > screen as OK module loaded Main > *Main> > But it will not do what I program to do. What do I enter after *main> I > tried putting in the arguments but ot just returns the arguments. > > For example: > > signum x | x < 0 = -1 > ??????? ? | x == 0 = 0 > ????????????? | x > 0 = 1 > > Enter 5 it returns 5 > > What have I missed? > > Regards > > John > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -- keithsheppard.name From byorgey at seas.upenn.edu Sat Oct 3 16:27:13 2009 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Sat Oct 3 16:04:57 2009 Subject: [Haskell-beginners] main In-Reply-To: <6656E51F7BD24292A0296C70F2868EDE@johnpmoore1> References: <6656E51F7BD24292A0296C70F2868EDE@johnpmoore1> Message-ID: <20091003202713.GA15110@seas.upenn.edu> On Sat, Oct 03, 2009 at 08:07:50PM +0100, John Moore wrote: > Hi, > I must be doing something wrong when I run a program it comes up on the > screen as OK module loaded Main > *Main> > But it will not do what I program to do. What do I enter after *main> I > tried putting in the arguments but ot just returns the arguments. > For example: > > signum x | x < 0 = -1 > | x == 0 = 0 > | x > 0 = 1 If you have the above in a file and load it into ghci, it just means that now the function 'signum' is available for use. You type expressions at the ghci prompt, so if you type 5 by itself, that is just the expression 5, which of course has the value... 5. =) If you want to evaluate signum with an input you must type, for example, signum 5 However, I should warn you that the Haskell Prelude already defines a function called 'signum', so if you load the above and type 'signum 5' it will give you an error about signum being ambiguous (it does not know which one you want, the one from the Prelude or the one you defined in the file you loaded). There are various ways around this, but the easiest for now is probably just to use a different name for your function. -Brent From hyangfji at gmail.com Sat Oct 3 17:43:25 2009 From: hyangfji at gmail.com (Hong Yang) Date: Sat Oct 3 17:21:10 2009 Subject: [Haskell-beginners] redirect stdout to /dev/null Message-ID: On UNIX, we can easily redirect stdout to /dev/null in a Shell. How is this realized in Haskell on both UNIX and Windows? I used a library which gave me unwanted stdout display. I tried to find any function to hide the stdout output, but could not. Can someone help me out? Otherwise I have to modify the code in the library to comment out some print commands. Thanks, Hong -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20091003/789b18fb/attachment.html From magnus at therning.org Sun Oct 4 01:56:55 2009 From: magnus at therning.org (Magnus Therning) Date: Sun Oct 4 01:34:42 2009 Subject: [Haskell-beginners] redirect stdout to /dev/null In-Reply-To: References: Message-ID: <20091004055655.GA19710@tatooine> On Sat, Oct 03, 2009 at 04:43:25PM -0500, Hong Yang wrote: > On UNIX, we can easily redirect stdout to /dev/null in a Shell. How is this > realized in Haskell on both UNIX and Windows? I'm not sure about Windows, but in Unix you'd close stdout, open /dev/null and then call dup2() (System.Posix.IO.dupTo) to duplicate the newly opened Fd to stdout (1). > I used a library which gave me unwanted stdout display. I tried to find any > function to hide the stdout output, but could not. Can someone help me out? > > Otherwise I have to modify the code in the library to comment out some print > commands. I would strongly encourage you to modify the library. It can be argued that it's rather poor library design to not let the user control where output goes. /M -- Magnus Therning (OpenPGP: 0xAB4DFBA4) magnus?therning?org Jabber: magnus?therning?org http://therning.org/magnus identi.ca|twitter: magthe -------------- next part -------------- A non-text attachment was scrubbed... Name: not available Type: application/pgp-signature Size: 198 bytes Desc: not available Url : http://www.haskell.org/pipermail/beginners/attachments/20091004/d01b6f26/attachment.bin From john.moore54 at gmail.com Sun Oct 4 09:43:35 2009 From: john.moore54 at gmail.com (John Moore) Date: Sun Oct 4 09:21:17 2009 Subject: [Haskell-beginners] parse error Message-ID: <4f7ad1ad0910040643w2a852017ge612411a59ff945c@mail.gmail.com> Hi, This is a function that returns true if divisible by 2. But it keeps telling me parse error on input | . What is wrong here. is_even n | mod n 2 == 0 | True Not sure if I need another statement for false John -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20091004/a11ce3e0/attachment.html From informationen at gmx.de Sun Oct 4 10:11:35 2009 From: informationen at gmx.de (informationen) Date: Sun Oct 4 09:48:52 2009 Subject: [Haskell-beginners] parse error In-Reply-To: <4f7ad1ad0910040643w2a852017ge612411a59ff945c@mail.gmail.com> References: <4f7ad1ad0910040643w2a852017ge612411a59ff945c@mail.gmail.com> Message-ID: <20091004141135.GB3696@debian> Hi, the "| True" part is wrong. It should read: is_even n | n `mod` 2 == 0 = True | otherwise = False. Kind regards Chris >Hi, >???? This is a function?that returns true if divisible by 2. But it keeps >telling me parse error on input | . What is wrong here. >is_even n? ?| mod n 2 == 0 >??????????????? ?| True >? >Not sure if I need another statement for false >? >John >_______________________________________________ >Beginners mailing list >Beginners@haskell.org >http://www.haskell.org/mailman/listinfo/beginners From felipe.lessa at gmail.com Sun Oct 4 10:35:55 2009 From: felipe.lessa at gmail.com (Felipe Lessa) Date: Sun Oct 4 10:14:12 2009 Subject: [Haskell-beginners] parse error In-Reply-To: <20091004141135.GB3696@debian> References: <4f7ad1ad0910040643w2a852017ge612411a59ff945c@mail.gmail.com> <20091004141135.GB3696@debian> Message-ID: <20091004143555.GA2691@kira.casa> On Sun, Oct 04, 2009 at 04:11:35PM +0200, informationen wrote: > the "| True" part is wrong. It should read: > > is_even n | n `mod` 2 == 0 = True > | otherwise = False. Which means that is_even may be expressed as is_even n = n `mod` 2 == 0 Equivalently, in C the first code would be int is_even(int n) { if (n % 2 == 0) return 1; else return 0; } while the code I presented above would be int is_even(int n) { return (n % 2 == 0); } HTH, -- Felipe. From john.moore54 at gmail.com Sun Oct 4 12:05:33 2009 From: john.moore54 at gmail.com (John Moore) Date: Sun Oct 4 11:43:16 2009 Subject: [Haskell-beginners] string instead of char Message-ID: <4f7ad1ad0910040905p64be6483o53e53a4f8b0052c4@mail.gmail.com> Hi, I am now writing a function that replaces vowels with the letter x. eg.put in a string "help" and out comes hxlp. I tried this: f x = case x of {'a' -> 'x';'e' -> 'x';'i' -> 'x';'o' -> 'x';'u' -> 'x';_ -> x} but this wont work on strings, only on the individual letters. Any direction would be very welcome. Yours john -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20091004/a727e5c8/attachment.html From colin at colina.demon.co.uk Sun Oct 4 12:10:39 2009 From: colin at colina.demon.co.uk (Colin Paul Adams) Date: Sun Oct 4 11:48:23 2009 Subject: [Haskell-beginners] string instead of char In-Reply-To: <4f7ad1ad0910040905p64be6483o53e53a4f8b0052c4@mail.gmail.com> (John Moore's message of "Sun\, 4 Oct 2009 17\:05\:33 +0100") References: <4f7ad1ad0910040905p64be6483o53e53a4f8b0052c4@mail.gmail.com> Message-ID: >>>>> "John" == John Moore writes: John> Hi, I am now writing a function that replaces vowels with John> the letter x. eg.put in a string "help" and out comes hxlp. John> I tried this: John> f x = case x of John> {'a' -> 'x';'e' -> 'x';'i' -> 'x';'o' -> 'x';'u' -> John> 'x';_ -> x} John> but this wont work on strings, only on the individual John> letters. Any direction would be very welcome. You need to call map f "whatever" where f is your function for replacing a character (as above). -- Colin Adams Preston Lancashire From rziemba at gmail.com Sun Oct 4 14:55:05 2009 From: rziemba at gmail.com (Robert Ziemba) Date: Sun Oct 4 14:32:48 2009 Subject: [Haskell-beginners] string instead of char In-Reply-To: References: <4f7ad1ad0910040905p64be6483o53e53a4f8b0052c4@mail.gmail.com> Message-ID: <65135790910041155ma0ec28em551e0d15c7c07028@mail.gmail.com> You can also do something like this instead of the case statement (this is using GHCI). Prelude> let exclude = ['a', 'e', 'i', 'o', 'u'] Prelude> let f excl letter | letter `elem` excl = 'x' | otherwise = letter Prelude> map (f exclude) "hello" "hxllx" Rob Ziemba On Sun, Oct 4, 2009 at 9:10 AM, Colin Paul Adams wrote: > >>>>> "John" == John Moore writes: > > John> Hi, I am now writing a function that replaces vowels with > John> the letter x. eg.put in a string "help" and out comes hxlp. > John> I tried this: > > John> f x = case x of > > John> {'a' -> 'x';'e' -> 'x';'i' -> 'x';'o' -> 'x';'u' -> > John> 'x';_ -> x} > > John> but this wont work on strings, only on the individual > John> letters. Any direction would be very welcome. > > You need to call > > map f "whatever" > > where f is your function for replacing a character (as above). > -- > Colin Adams > Preston Lancashire > _______________________________________________ > 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/20091004/68b6b7a0/attachment-0001.html From michael at snoyman.com Sun Oct 4 15:14:05 2009 From: michael at snoyman.com (Michael Snoyman) Date: Sun Oct 4 14:51:46 2009 Subject: [Haskell-beginners] string instead of char In-Reply-To: <65135790910041155ma0ec28em551e0d15c7c07028@mail.gmail.com> References: <4f7ad1ad0910040905p64be6483o53e53a4f8b0052c4@mail.gmail.com> <65135790910041155ma0ec28em551e0d15c7c07028@mail.gmail.com> Message-ID: <29bf512f0910041214n3fe2786bqf5b9c9666c22526d@mail.gmail.com> Or instead of ['a', 'e', 'i', 'o', 'u'] use "aeiou". However, I think the case statement, while my verbose, is also clearer. But that's purely a style choice. Michael On Sun, Oct 4, 2009 at 8:55 PM, Robert Ziemba wrote: > You can also do something like this instead of the case statement (this is > using GHCI). > Prelude> let exclude = ['a', 'e', 'i', 'o', 'u'] > Prelude> let f excl letter | letter `elem` excl = 'x' | otherwise = letter > Prelude> map (f exclude) "hello" > "hxllx" > > Rob Ziemba > > On Sun, Oct 4, 2009 at 9:10 AM, Colin Paul Adams > wrote: > >> >>>>> "John" == John Moore writes: >> >> John> Hi, I am now writing a function that replaces vowels with >> John> the letter x. eg.put in a string "help" and out comes hxlp. >> John> I tried this: >> >> John> f x = case x of >> >> John> {'a' -> 'x';'e' -> 'x';'i' -> 'x';'o' -> 'x';'u' -> >> John> 'x';_ -> x} >> >> John> but this wont work on strings, only on the individual >> John> letters. Any direction would be very welcome. >> >> You need to call >> >> map f "whatever" >> >> where f is your function for replacing a character (as above). >> -- >> Colin Adams >> Preston Lancashire >> _______________________________________________ >> Beginners mailing list >> Beginners@haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> > > > _______________________________________________ > 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/20091004/8bdcbff0/attachment.html From velman at cox.net Sun Oct 4 17:53:44 2009 From: velman at cox.net (John Velman) Date: Sun Oct 4 17:31:30 2009 Subject: [Haskell-beginners] Advantage to installing Hugs with GHC? Message-ID: <20091004215344.GA441@cox.net> My machine is an iMac running Leopard, 10.5.8. I've installed GHC from the haskell-platform-..(Leopard/x86) disk image. Ghci works fine as far as I can tell. Is there any particular advantage to also installing Hugs? Thanks, John Velman From byorgey at seas.upenn.edu Sun Oct 4 21:51:22 2009 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Sun Oct 4 21:29:04 2009 Subject: [Haskell-beginners] Advantage to installing Hugs with GHC? In-Reply-To: <20091004215344.GA441@cox.net> References: <20091004215344.GA441@cox.net> Message-ID: <20091005015122.GA11877@seas.upenn.edu> On Sun, Oct 04, 2009 at 02:53:44PM -0700, John Velman wrote: > > My machine is an iMac running Leopard, 10.5.8. I've installed GHC from > the haskell-platform-..(Leopard/x86) disk image. Ghci works fine as far as > I can tell. > > Is there any particular advantage to also installing Hugs? Not really, unless (say) you are following some tutorial that specifically uses it. -Brent From apfelmus at quantentunnel.de Mon Oct 5 05:40:13 2009 From: apfelmus at quantentunnel.de (Heinrich Apfelmus) Date: Mon Oct 5 05:18:24 2009 Subject: [Haskell-beginners] Re: Advantage to installing Hugs with GHC? In-Reply-To: <20091005015122.GA11877@seas.upenn.edu> References: <20091004215344.GA441@cox.net> <20091005015122.GA11877@seas.upenn.edu> Message-ID: Brent Yorgey wrote: > On Sun, Oct 04, 2009 at 02:53:44PM -0700, John Velman wrote: >> My machine is an iMac running Leopard, 10.5.8. I've installed GHC from >> the haskell-platform-..(Leopard/x86) disk image. Ghci works fine as far as >> I can tell. >> >> Is there any particular advantage to also installing Hugs? > > Not really, unless (say) you are following some tutorial that specifically uses it. Well, Hugs does start much faster than ghci on my machine, but that's about the only advantage. :) Regards, apfelmus -- http://apfelmus.nfshost.com From velman at cox.net Mon Oct 5 11:15:16 2009 From: velman at cox.net (John Velman) Date: Mon Oct 5 10:52:55 2009 Subject: [Haskell-beginners] Re: Advantage to installing Hugs with GHC? In-Reply-To: References: <20091004215344.GA441@cox.net> <20091005015122.GA11877@seas.upenn.edu> Message-ID: <20091005151516.GA282@cox.net> On Mon, Oct 05, 2009 at 11:40:13AM +0200, Heinrich Apfelmus wrote: > Brent Yorgey wrote: > > On Sun, Oct 04, 2009 at 02:53:44PM -0700, John Velman wrote: > >> My machine is an iMac running Leopard, 10.5.8. I've installed GHC from > >> the haskell-platform-..(Leopard/x86) disk image. Ghci works fine as far as > >> I can tell. > >> > >> Is there any particular advantage to also installing Hugs? > > > > Not really, unless (say) you are following some tutorial that specifically uses it. > > Well, Hugs does start much faster than ghci on my machine, but that's > about the only advantage. :) > > > Regards, > apfelmus > Thanks -- I can't notice any delay when starting ghci (I didn't always have a fast machine!). Also, I guess if something in a tutorial really depends on Hugs, it would be something that I really don't need to know (unless I plan on using Hugs,that is). I'll skip installing Hugs. Thanks to both. John Velman > -- > http://apfelmus.nfshost.com > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners From joe at fixieconsulting.com Mon Oct 5 14:24:43 2009 From: joe at fixieconsulting.com (Joe Van Dyk) Date: Mon Oct 5 14:02:42 2009 Subject: [Haskell-beginners] string instead of char In-Reply-To: <29bf512f0910041214n3fe2786bqf5b9c9666c22526d@mail.gmail.com> References: <4f7ad1ad0910040905p64be6483o53e53a4f8b0052c4@mail.gmail.com> <65135790910041155ma0ec28em551e0d15c7c07028@mail.gmail.com> <29bf512f0910041214n3fe2786bqf5b9c9666c22526d@mail.gmail.com> Message-ID: How about instead: devowelize input | elem input "abcde" = 'x' devowelize input = input Not sure if pattern matching is appropriate here. On Sun, Oct 4, 2009 at 12:14 PM, Michael Snoyman wrote: > Or instead of ['a', 'e', 'i', 'o', 'u'] use "aeiou". However, I think the > case statement, while my verbose, is also clearer. But that's purely a style > choice. > > Michael > > On Sun, Oct 4, 2009 at 8:55 PM, Robert Ziemba wrote: >> >> You can also do something like this instead of the case statement (this is >> using GHCI). >> Prelude> let exclude = ['a', 'e', 'i', 'o', 'u'] >> Prelude> let f excl letter | letter `elem` excl = 'x' | otherwise = letter >> Prelude> map (f exclude) "hello" >> "hxllx" >> Rob Ziemba >> On Sun, Oct 4, 2009 at 9:10 AM, Colin Paul Adams >> wrote: >>> >>> >>>>> "John" == John Moore writes: >>> >>> ? ?John> Hi, I am now writing a function that replaces vowels with >>> ? ?John> the letter x. ?eg.put in a string "help" and out comes hxlp. >>> ? ?John> I tried this: >>> >>> ? ?John> f x = case x of >>> >>> ? ?John> ? ? ? {'a' -> 'x';'e' -> 'x';'i' -> 'x';'o' -> 'x';'u' -> >>> ? ?John> 'x';_ -> x} >>> >>> ? ?John> but this wont work on strings, only on the individual >>> ? ?John> letters. Any direction would be very welcome. >>> >>> You need to call >>> >>> map f "whatever" >>> >>> where f is your function for replacing a character (as above). >>> -- >>> Colin Adams >>> Preston Lancashire >>> _______________________________________________ >>> Beginners mailing list >>> Beginners@haskell.org >>> http://www.haskell.org/mailman/listinfo/beginners >> >> >> _______________________________________________ >> Beginners mailing list >> Beginners@haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> > > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > -- Joe Van Dyk http://fixieconsulting.com From velman at cox.net Mon Oct 5 18:41:38 2009 From: velman at cox.net (John Velman) Date: Mon Oct 5 18:19:17 2009 Subject: [Haskell-beginners] Compiling C that calls Haskell with GCC Message-ID: <20091005224138.GB282@cox.net> More specifically, I want to link a haskell .o file into a project I create on OS X using Xcode, but the answer to the gcc question should do it. I've followed the simple "calling haskell from C" example from HaskellWiki, and it works as advertised when compiled as advertised with gcc. However, I'm looking toward writing OS X programs with Xcode and the standard Mac interface, calling some functions written in Haskell. Consequently, I want to compile and link using the mac Xcode IDE, which uses gcc. Seems like the thing that GHC puts in must be include .h files and libraries. But which ones? I think I ran across the answer to this in my browsing, but can't find it again. Can someone point me in the right direction? Thanks, John Velman From martin.hofmann at uni-bamberg.de Fri Oct 9 03:45:27 2009 From: martin.hofmann at uni-bamberg.de (Martin Hofmann) Date: Fri Oct 9 03:22:37 2009 Subject: [Haskell-beginners] (ab)use compiler optimization Message-ID: <1255074327.6111.25.camel@ios.cogsys.wiai.uni-bamberg.de> Hello, To analyse, test, and debug my system I use a WriterT for logging. The log file created can be quite large and a lot of 'Log' is created in memory, too. I thought, that maybe I can (ab)use the compiler to switch logging on and off as needed before compiling. I tried to either always return an empty Log when running the WriterT, or just always ignore logging depending on a constant and hoped GHC to optimize the logging away. _NOLOGGING = True runWT :: (Monad m) => (WriterT Log m a) -> m (a, Log) runWT m | _NOLOGGING = runWriterT m empty >>= \(a,_) -> return (a,emptyLog ) | otherwise = runWriterT m emptyLog logging :: Message -> WriterT Log m () logging msg = if _NOLOGGING then return () else tell msg However, according to the heap profile there is still a lot of 'Log' in the memory. So, is this possible this way at all and if not, is there another (apart from removing all logging by hand). Thanks From apfelmus at quantentunnel.de Fri Oct 9 07:10:28 2009 From: apfelmus at quantentunnel.de (Heinrich Apfelmus) Date: Fri Oct 9 06:48:25 2009 Subject: [Haskell-beginners] Re: (ab)use compiler optimization In-Reply-To: <1255074327.6111.25.camel@ios.cogsys.wiai.uni-bamberg.de> References: <1255074327.6111.25.camel@ios.cogsys.wiai.uni-bamberg.de> Message-ID: Martin Hofmann wrote: > I thought, that maybe I can (ab)use the compiler to switch logging on > and off as needed before compiling. I tried to either always return an > empty Log when running the WriterT, or just always ignore logging > depending on a constant and hoped GHC to optimize the logging away. > > > _NOLOGGING = True > > runWT :: (Monad m) => (WriterT Log m a) -> m (a, Log) > runWT m > | _NOLOGGING = runWriterT m empty >>= \(a,_) -> return (a,emptyLog ) > | otherwise = runWriterT m emptyLog > > logging :: Message -> WriterT Log m () > logging msg = if _NOLOGGING then return () else tell msg > > > However, according to the heap profile there is still a lot of 'Log' in > the memory. So, is this possible this way at all and if not, is there > another (apart from removing all logging by hand). Most likely, the WriterT monad is still constructing a log, except that it now looks like emptyLog `mappend` emptyLog `mappend` ... etc. Of course, this is equal to emptyLog but it's never evaluated and these huge expressions are eating up memory. I think that importing WriterT from Control.Monad.Writer.Strict should solve the problem. With that, you can revert to runWT = runWriterT as well, only logging needs to be implemented as you did. Another method, which is guaranteed to work, is to implement a minuscule custom logging monad like this: module Logger where type Logger m a = WriterT Log m a runLogger = runWriterT logging = tell and replace it with module MockLogger where type Logger m a = m a runLogger = fmap $ \a -> (a,emptyLog) logging = const $ return () when you don't want to log anything. Regards, apfelmus -- http://apfelmus.nfshost.com From martin.hofmann at uni-bamberg.de Fri Oct 9 09:52:52 2009 From: martin.hofmann at uni-bamberg.de (Martin Hofmann) Date: Fri Oct 9 09:30:02 2009 Subject: [Haskell-beginners] Re: (ab)use compiler optimization In-Reply-To: <20091009104826.A1AC8324A4E@www.haskell.org> References: <20091009104826.A1AC8324A4E@www.haskell.org> Message-ID: <1255096372.6111.75.camel@ios.cogsys.wiai.uni-bamberg.de> Thanks apfelmus this worked fine. From mpm at alumni.caltech.edu Sat Oct 10 03:59:15 2009 From: mpm at alumni.caltech.edu (Michael Mossey) Date: Sat Oct 10 03:36:46 2009 Subject: [Haskell-beginners] questions about randomness Message-ID: <1228.75.50.154.24.1255161555.squirrel@mail.alumni.caltech.edu> I would like to know if this is a good way to do this job, both from the point of view of elegance and efficiency. Maybe it needs to be more strict? I may create lists of 100,000 elements and don't want to run into space/time problems. I want to create an infinite list of Floats (for purposes of lazily zipping or writing later), with the following form: a random number of zeros, followed by a single random non-zero value, repeat the pattern. I call it a "break series" for reasons I won't go into here, but in practice, I want to choose the number of zeros in each section from a range of integers, and I want the non-zero values to have a particular frequency distribution. Here's some code I wrote: import System.Random import Control.Monad import Control.Monad.Random breakSeries :: Rand StdGen [Float] breakSeries = do x <- getRandomR (0.0::Float, 1.0) -- Hacking a simple frequency distribution of values of d. d <- if x < 0.6 then getRandomR (5.0::Float, 8.0) else if x < 0.8 then getRandomR (12.0::Float , 24.0) else getRandomR (60.0::Float , 90.0) n <- getRandomR (4::Int, 6) let bits = replicate n 0.0 ++ [d] liftM (bits++) breakSeries test = print . take 20 . evalRand breakSeries =<< newStdGen From chaddai.fouche at gmail.com Sat Oct 10 05:47:01 2009 From: chaddai.fouche at gmail.com (=?UTF-8?B?Q2hhZGRhw68gRm91Y2jDqQ==?=) Date: Sat Oct 10 05:24:26 2009 Subject: [Haskell-beginners] questions about randomness In-Reply-To: <1228.75.50.154.24.1255161555.squirrel@mail.alumni.caltech.edu> References: <1228.75.50.154.24.1255161555.squirrel@mail.alumni.caltech.edu> Message-ID: On Sat, Oct 10, 2009 at 9:59 AM, Michael Mossey wrote: > I would like to know if this is a good way to do this job, both from the > point of view of elegance and efficiency. Maybe it needs to be more > strict? I may create lists of 100,000 elements and don't want to run into > space/time problems. I think the principal advice we can give you is not to use System.Random : while the interface is pleasant the performances are excruciatingly bad... Try one of the alternative like the recently released statistics package or the mersenne-random package by dons. A recent blog post that shows the problem : http://www.serpentine.com/blog/2009/09/19/a-new-pseudo-random-number-generator-for-haskell/ -- Jeda? From patrick.leboutillier at gmail.com Sun Oct 11 08:10:23 2009 From: patrick.leboutillier at gmail.com (Patrick LeBoutillier) Date: Sun Oct 11 07:47:49 2009 Subject: [Haskell-beginners] Re: [Haskell-cafe] Haskell Weekly News: Issue 134 - October 10, 2009 In-Reply-To: <4ad03c0a.9753f10a.238d.3a88@mx.google.com> References: <4ad03c0a.9753f10a.238d.3a88@mx.google.com> Message-ID: Hi, Could/should the Haskell Weekly News be posted to the beginners list as well? I normally don't follow haskell-cafe (too much traffic and generally above my level I must admit...), but I like to follow what's going on in the Haskell community. Patrick On Sat, Oct 10, 2009 at 3:47 AM, wrote: > > --------------------------------------------------------------------------- > Haskell Weekly News > http://sequence.complete.org/hwn/20091010 > Issue 134 - October 10, 2009 > --------------------------------------------------------------------------- > Welcome to issue 134 of HWN, a newsletter covering developments in the > [1]Haskell community. > > What with Don Stewart's [2]call to [3]arms to lead Haskell to conquest > over (E)DSL-land, I've once again tried to highlight discussion of > EDSL's this week. Fortunately, it was actually more difficult choosing > what _not_ to include this week, since there was so much discussion > about DSLs and Syntax extensions (a related notion, in my opinion). > Also, this week Bryan O'Sullivan put his Criterion Library to good use > on the `text` package, leading to [4]code which is more than ten times > faster than before! With all this fantastic news, I won't hold you up > any longer, Haskellers, the Haskell Weekly News! > > Announcements > > CfPart: FMICS 2009, 2-3 November 2009, Final Call. FMICS 2009 workshop > chair [5]announced the final call for particpaction for FMICS 2009 > > ICFP videos now available. Wouter Swierstra [6]announced the > availablity of videos from the International Conference on Functional > Programming (ICFP) > > GPipe-1.0.0: A functional graphics API for programmable GPUs. Tobias > Bexelius [7]announced the first release of GPie, a functional graphics > API for programmable GPUs. > > text 0.5, a major revision of the Unicode text library. Bryan > O'Sullivan [8]announced a new, major version of the text package. New > API features, and huge improvments in speed, as Bryan says, 'Get it > while it's fresh on Hackage, folks!' > > vty-ui 0.2. Jonathan Daugherty [9]announced a new version of the vty-ui > package, with fewer bugs, more widgets, and cleaner code due to new > more powerful abstractions. > > htzaar-0.0.1. Tom Hawkins [10]announced HTZAAR, a Haskell > implementation of TZAAR > > Graphalyze-0.8.0.0 and SourceGraph-0.5.5.0. Ivan Lazar Miljenovic > [11]announced To keep this editor happy, Ivan released two new packaged > in one announcement. This time, he's added Legend support to > Graphalyze, but also many new changes to SourceGraph, including a > legend so you can see what all the symbols mean, Better color support, > and much more. > > TxtSushi 0.4.0. Keith Sheppard [12]announced a new version of TxtSushi, > a set of command line utilities for processing CSV and TSV files. > > Discussion > > Applicative do? Philippa Cowderoy [13]asked about a `do` like syntax > for Applicative functors. > > How to add use custom preprocessor in cabal. Bernd Brassel [14]asked > how to add a custom preprocessor to the build chain of a cabal file. > > On DSLs - one last time. Gunther Schmidt [15]summarized his impressions > on al the recent discussion of DSLs > > What is a DSL? Oleg [16]offered some insight into different > [17]properties that can be part of a single tagless framework. He also > pointed to some slides and other materials such as a website [18]here > and slides [19]here about DSL implementations and definitions. > > What is a DSL? Gunther Schmidt [20]posed the question, 'What is a DSL', > and with some further questions added by yours truly, a lively > discussion about the definition of a DSL ensued. > > Finally tagless - stuck with implementation of 'lam'. Gunther Schmidt > [21]asked another question about Finally Tagless DSLs and resolving an > issue with the implementation of 'lam' > > Blog noise > > [22]Haskell news from the [23]blogosphere. Blog posts from people new > to the Haskell community are marked with >>>, be sure to welcome them! > * Darcs: [24]darcs weekly news #43. > * JP Moresmau: [25]What client for an Haskell Multi Player Game?. > * Mikael Vejdemo Johansson (Syzygy-): [26][MATH198] Third lecture is > up. > * Bryan O'Sullivan: [27]Announcing a major revision of the Haskell > text library. > * Eric Kow (kowey): [28]darcs hashed-storage work merged (woo!). > * David Amos: [29]Symmetries of PG(n,Fq). > * The GHC Team: [30]Parallelism /= Concurrency. > * >>> Nefigah: [31]Fake World Haskell. Nefigah, a recent addition to > the community, has been working through RWH, and is providing some > excellent examples. Though, This editor prefers the title 'Real > Life Haskell' as opposed to his choice. > * Tom Schrijvers: [32]Release 0.6 of Monadic Constraint Programming. > * Neil Brown: [33]Concurrency Can Be Deterministic (But The Type > System Doesn't Know It). > * Clint Moore: [34]Curiously Parallel. > * Galois, Inc: [35]Tech Talk: Constructing A Universal Domain for > Reasoning About Haskell Datatypes. > * Neil Brown: [36]Terminal Concurrency: The Printing Process. > * Sean Leather: [37]'Upwards and downwards accumulations on trees' > translated into Haskell. > * Mikael Vejdemo Johansson (Syzygy-): [38][MATH 198] Second lecture. > * Chris Smith: [39]View Patterns as Pattern Matching for Records. > * Chris Smith: [40]Playing With Records. > * FP Lunch: [41]Left Kan extensions of containers. > > Quotes of the Week > > * Baughn: Blum Blum Shub, a PRNG derived from poking around R'Lyeh. > * ksf: * lambdabot locks up ksf in a Monad mmmmh it's warm and > fuzzy in here. > * monochrom: Don't wrap your head around Haskell. Immerse! Wrap > Haskell around your head. > * chak: ... In other words, FP is inevitable. > * robreim: I'm in your base hacking all your lambdas > * gwern: RAM is overrated, swap is where it's at ;) > * idnar: [to gwern] swap to a ramdisk! ;P > > About the Haskell Weekly News > > New editions are posted to [42]the Haskell mailing list as well as to > [43]the Haskell Sequence and [44]Planet Haskell. [45]RSS is also > available, and headlines appear on [46]haskell.org. > > To help create new editions of this newsletter, please see the > information on [47]how to contribute. Send stories to jfredett . at . > gmail . dot . com. The darcs repository is available at darcs get > [48]http://patch-tag.com/r/jfredett/HWN2/pullrepo HWN2 . > > References > > 1. http://haskell.org/ > 2. > http://www.haskell.org/pipermail/haskell-cafe/2009-October/067203.html > 3. > http://www.reddit.com/r/haskell/comments/9qk54/haskell_should_own_the_edsl_space_a_call_to_arms/ > 4. > http://www.serpentine.com/blog/2009/10/09/announcing-a-major-revision-of-the-haskell-text-library/ > 5. http://article.gmane.org/gmane.comp.lang.haskell.general/17555 > 6. http://article.gmane.org/gmane.comp.lang.haskell.general/17552 > 7. http://article.gmane.org/gmane.comp.lang.haskell.general/17546 > 8. http://article.gmane.org/gmane.comp.lang.haskell.cafe/64591 > 9. http://article.gmane.org/gmane.comp.lang.haskell.cafe/64529 > 10. http://article.gmane.org/gmane.comp.lang.haskell.cafe/64516 > 11. http://article.gmane.org/gmane.comp.lang.haskell.cafe/64399 > 12. http://article.gmane.org/gmane.comp.lang.haskell.cafe/64301 > 13. http://thread.gmane.org/gmane.comp.lang.haskell.cafe/64616 > 14. http://thread.gmane.org/gmane.comp.lang.haskell.cafe/64558 > 15. http://thread.gmane.org/gmane.comp.lang.haskell.cafe/64552 > 16. http://thread.gmane.org/gmane.comp.lang.haskell.cafe/64533 > 17. http://thread.gmane.org/gmane.comp.lang.haskell.cafe/ > 18. http://dsl09.blogspot.com/ > 19. http://dsl09.blogspot.com/2009/07/panel.html > 20. http://thread.gmane.org/gmane.comp.lang.haskell.cafe/64474 > 21. http://thread.gmane.org/gmane.comp.lang.haskell.cafe/64343 > 22. http://planet.haskell.org/ > 23. http://haskell.org/haskellwiki/Blog_articles > 24. http://blog.darcs.net/2009/10/darcs-weekly-news-43.html > 25. > http://jpmoresmau.blogspot.com/2009/10/what-client-for-haskell-multi-player.html > 26. > http://blog.mikael.johanssons.org/archive/2009/10/math198-third-lecture-is-up/ > 27. > http://www.serpentine.com/blog/2009/10/09/announcing-a-major-revision-of-the-haskell-text-library/ > 28. > http://koweycode.blogspot.com/2009/10/darcs-hashed-storage-work-merged-woo.html > 29. http://haskellformaths.blogspot.com/2009/10/symmetries-of-pgnfq.html > 30. > http://ghcmutterings.wordpress.com/2009/10/06/parallelism-concurrency/ > 31. http://my.life-is-virtual.com/2009/10/07/fake-world-haskell-part-1/ > 32. > http://tomschrijvers.blogspot.com/2009/10/release-06-of-monadic-constraint.html > 33. > http://chplib.wordpress.com/2009/10/07/concurrency-can-be-deterministic/ > 34. http://www.l2mlogistics.com/2009/10/curiously-parallel.html > 35. http://www.galois.com/blog/2009/10/06/huffman-universal/ > 36. http://chplib.wordpress.com/2009/10/06/the-printing-process/ > 37. > http://feedproxy.google.com/~r/splonderzoek/~3/4E8TZPDZ-aM/upwards-and-downwards-accumulations-on.html > 38. > http://blog.mikael.johanssons.org/archive/2009/10/math-198-second-lecture/ > 39. > http://cdsmith.wordpress.com/2009/10/04/view-patterns-as-pattern-matching-for-records/ > 40. http://cdsmith.wordpress.com/2009/10/03/playing-with-records/ > 41. http://sneezy.cs.nott.ac.uk/fplunch/weblog/?p=237 > 42. http://www.haskell.org/mailman/listinfo/haskell > 43. http://sequence.complete.org/ > 44. http://planet.haskell.org/ > 45. http://sequence.complete.org/node/feed > 46. http://haskell.org/ > 47. http://haskell.org/haskellwiki/HWN > 48. http://patch-tag.com/r/jfredett/HWN2/pullrepo%20HWN2 > _______________________________________________ > Haskell-Cafe mailing list > Haskell-Cafe@haskell.org > http://www.haskell.org/mailman/listinfo/haskell-cafe > -- ===================== Patrick LeBoutillier Rosem?re, Qu?bec, Canada -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20091011/334c0df2/attachment-0001.html From zsol.zso.l at gmail.com Sun Oct 11 08:19:58 2009 From: zsol.zso.l at gmail.com (Zsolt Dollenstein) Date: Sun Oct 11 07:57:40 2009 Subject: [Haskell-beginners] Re: [Haskell-cafe] Haskell Weekly News: Issue 134 - October 10, 2009 In-Reply-To: References: <4ad03c0a.9753f10a.238d.3a88@mx.google.com> Message-ID: <419feb5f0910110519y65fe6a6fo5f1fcf6be58671c7@mail.gmail.com> Hi Patrick, On Sun, Oct 11, 2009 at 1:10 PM, Patrick LeBoutillier < patrick.leboutillier@gmail.com> wrote: > Hi, > > Could/should the Haskell Weekly News be posted to the beginners list as > well? > > I normally don't follow haskell-cafe (too much traffic and generally above > my level I must admit...), but I like to follow what's going on in the > Haskell community. > It is also posted to haskell@ as well. This is a low-traffic group which usually hosts announcements and news. Try signing up for this one. Zsolt > > > Patrick > > On Sat, Oct 10, 2009 at 3:47 AM, wrote: > >> >> >> --------------------------------------------------------------------------- >> Haskell Weekly News >> http://sequence.complete.org/hwn/20091010 >> Issue 134 - October 10, 2009 >> >> --------------------------------------------------------------------------- >> Welcome to issue 134 of HWN, a newsletter covering developments in the >> [1]Haskell community. >> >> What with Don Stewart's [2]call to [3]arms to lead Haskell to conquest >> over (E)DSL-land, I've once again tried to highlight discussion of >> EDSL's this week. Fortunately, it was actually more difficult choosing >> what _not_ to include this week, since there was so much discussion >> about DSLs and Syntax extensions (a related notion, in my opinion). >> Also, this week Bryan O'Sullivan put his Criterion Library to good use >> on the `text` package, leading to [4]code which is more than ten times >> faster than before! With all this fantastic news, I won't hold you up >> any longer, Haskellers, the Haskell Weekly News! >> >> Announcements >> >> CfPart: FMICS 2009, 2-3 November 2009, Final Call. FMICS 2009 workshop >> chair [5]announced the final call for particpaction for FMICS 2009 >> >> ICFP videos now available. Wouter Swierstra [6]announced the >> availablity of videos from the International Conference on Functional >> Programming (ICFP) >> >> GPipe-1.0.0: A functional graphics API for programmable GPUs. Tobias >> Bexelius [7]announced the first release of GPie, a functional graphics >> API for programmable GPUs. >> >> text 0.5, a major revision of the Unicode text library. Bryan >> O'Sullivan [8]announced a new, major version of the text package. New >> API features, and huge improvments in speed, as Bryan says, 'Get it >> while it's fresh on Hackage, folks!' >> >> vty-ui 0.2. Jonathan Daugherty [9]announced a new version of the vty-ui >> package, with fewer bugs, more widgets, and cleaner code due to new >> more powerful abstractions. >> >> htzaar-0.0.1. Tom Hawkins [10]announced HTZAAR, a Haskell >> implementation of TZAAR >> >> Graphalyze-0.8.0.0 and SourceGraph-0.5.5.0. Ivan Lazar Miljenovic >> [11]announced To keep this editor happy, Ivan released two new packaged >> in one announcement. This time, he's added Legend support to >> Graphalyze, but also many new changes to SourceGraph, including a >> legend so you can see what all the symbols mean, Better color support, >> and much more. >> >> TxtSushi 0.4.0. Keith Sheppard [12]announced a new version of TxtSushi, >> a set of command line utilities for processing CSV and TSV files. >> >> Discussion >> >> Applicative do? Philippa Cowderoy [13]asked about a `do` like syntax >> for Applicative functors. >> >> How to add use custom preprocessor in cabal. Bernd Brassel [14]asked >> how to add a custom preprocessor to the build chain of a cabal file. >> >> On DSLs - one last time. Gunther Schmidt [15]summarized his impressions >> on al the recent discussion of DSLs >> >> What is a DSL? Oleg [16]offered some insight into different >> [17]properties that can be part of a single tagless framework. He also >> pointed to some slides and other materials such as a website [18]here >> and slides [19]here about DSL implementations and definitions. >> >> What is a DSL? Gunther Schmidt [20]posed the question, 'What is a DSL', >> and with some further questions added by yours truly, a lively >> discussion about the definition of a DSL ensued. >> >> Finally tagless - stuck with implementation of 'lam'. Gunther Schmidt >> [21]asked another question about Finally Tagless DSLs and resolving an >> issue with the implementation of 'lam' >> >> Blog noise >> >> [22]Haskell news from the [23]blogosphere. Blog posts from people new >> to the Haskell community are marked with >>>, be sure to welcome them! >> * Darcs: [24]darcs weekly news #43. >> * JP Moresmau: [25]What client for an Haskell Multi Player Game?. >> * Mikael Vejdemo Johansson (Syzygy-): [26][MATH198] Third lecture is >> up. >> * Bryan O'Sullivan: [27]Announcing a major revision of the Haskell >> text library. >> * Eric Kow (kowey): [28]darcs hashed-storage work merged (woo!). >> * David Amos: [29]Symmetries of PG(n,Fq). >> * The GHC Team: [30]Parallelism /= Concurrency. >> * >>> Nefigah: [31]Fake World Haskell. Nefigah, a recent addition to >> the community, has been working through RWH, and is providing some >> excellent examples. Though, This editor prefers the title 'Real >> Life Haskell' as opposed to his choice. >> * Tom Schrijvers: [32]Release 0.6 of Monadic Constraint Programming. >> * Neil Brown: [33]Concurrency Can Be Deterministic (But The Type >> System Doesn't Know It). >> * Clint Moore: [34]Curiously Parallel. >> * Galois, Inc: [35]Tech Talk: Constructing A Universal Domain for >> Reasoning About Haskell Datatypes. >> * Neil Brown: [36]Terminal Concurrency: The Printing Process. >> * Sean Leather: [37]'Upwards and downwards accumulations on trees' >> translated into Haskell. >> * Mikael Vejdemo Johansson (Syzygy-): [38][MATH 198] Second lecture. >> * Chris Smith: [39]View Patterns as Pattern Matching for Records. >> * Chris Smith: [40]Playing With Records. >> * FP Lunch: [41]Left Kan extensions of containers. >> >> Quotes of the Week >> >> * Baughn: Blum Blum Shub, a PRNG derived from poking around R'Lyeh. >> * ksf: * lambdabot locks up ksf in a Monad mmmmh it's warm and >> fuzzy in here. >> * monochrom: Don't wrap your head around Haskell. Immerse! Wrap >> Haskell around your head. >> * chak: ... In other words, FP is inevitable. >> * robreim: I'm in your base hacking all your lambdas >> * gwern: RAM is overrated, swap is where it's at ;) >> * idnar: [to gwern] swap to a ramdisk! ;P >> >> About the Haskell Weekly News >> >> New editions are posted to [42]the Haskell mailing list as well as to >> [43]the Haskell Sequence and [44]Planet Haskell. [45]RSS is also >> available, and headlines appear on [46]haskell.org. >> >> To help create new editions of this newsletter, please see the >> information on [47]how to contribute. Send stories to jfredett . at . >> gmail . dot . com. The darcs repository is available at darcs get >> [48]http://patch-tag.com/r/jfredett/HWN2/pullrepo HWN2 . >> >> References >> >> 1. http://haskell.org/ >> 2. >> http://www.haskell.org/pipermail/haskell-cafe/2009-October/067203.html >> 3. >> http://www.reddit.com/r/haskell/comments/9qk54/haskell_should_own_the_edsl_space_a_call_to_arms/ >> 4. >> http://www.serpentine.com/blog/2009/10/09/announcing-a-major-revision-of-the-haskell-text-library/ >> 5. http://article.gmane.org/gmane.comp.lang.haskell.general/17555 >> 6. http://article.gmane.org/gmane.comp.lang.haskell.general/17552 >> 7. http://article.gmane.org/gmane.comp.lang.haskell.general/17546 >> 8. http://article.gmane.org/gmane.comp.lang.haskell.cafe/64591 >> 9. http://article.gmane.org/gmane.comp.lang.haskell.cafe/64529 >> 10. http://article.gmane.org/gmane.comp.lang.haskell.cafe/64516 >> 11. http://article.gmane.org/gmane.comp.lang.haskell.cafe/64399 >> 12. http://article.gmane.org/gmane.comp.lang.haskell.cafe/64301 >> 13. http://thread.gmane.org/gmane.comp.lang.haskell.cafe/64616 >> 14. http://thread.gmane.org/gmane.comp.lang.haskell.cafe/64558 >> 15. http://thread.gmane.org/gmane.comp.lang.haskell.cafe/64552 >> 16. http://thread.gmane.org/gmane.comp.lang.haskell.cafe/64533 >> 17. http://thread.gmane.org/gmane.comp.lang.haskell.cafe/ >> 18. http://dsl09.blogspot.com/ >> 19. http://dsl09.blogspot.com/2009/07/panel.html >> 20. http://thread.gmane.org/gmane.comp.lang.haskell.cafe/64474 >> 21. http://thread.gmane.org/gmane.comp.lang.haskell.cafe/64343 >> 22. http://planet.haskell.org/ >> 23. http://haskell.org/haskellwiki/Blog_articles >> 24. http://blog.darcs.net/2009/10/darcs-weekly-news-43.html >> 25. >> http://jpmoresmau.blogspot.com/2009/10/what-client-for-haskell-multi-player.html >> 26. >> http://blog.mikael.johanssons.org/archive/2009/10/math198-third-lecture-is-up/ >> 27. >> http://www.serpentine.com/blog/2009/10/09/announcing-a-major-revision-of-the-haskell-text-library/ >> 28. >> http://koweycode.blogspot.com/2009/10/darcs-hashed-storage-work-merged-woo.html >> 29. http://haskellformaths.blogspot.com/2009/10/symmetries-of-pgnfq.html >> 30. >> http://ghcmutterings.wordpress.com/2009/10/06/parallelism-concurrency/ >> 31. http://my.life-is-virtual.com/2009/10/07/fake-world-haskell-part-1/ >> 32. >> http://tomschrijvers.blogspot.com/2009/10/release-06-of-monadic-constraint.html >> 33. >> http://chplib.wordpress.com/2009/10/07/concurrency-can-be-deterministic/ >> 34. http://www.l2mlogistics.com/2009/10/curiously-parallel.html >> 35. http://www.galois.com/blog/2009/10/06/huffman-universal/ >> 36. http://chplib.wordpress.com/2009/10/06/the-printing-process/ >> 37. >> http://feedproxy.google.com/~r/splonderzoek/~3/4E8TZPDZ-aM/upwards-and-downwards-accumulations-on.html >> 38. >> http://blog.mikael.johanssons.org/archive/2009/10/math-198-second-lecture/ >> 39. >> http://cdsmith.wordpress.com/2009/10/04/view-patterns-as-pattern-matching-for-records/ >> 40. http://cdsmith.wordpress.com/2009/10/03/playing-with-records/ >> 41. http://sneezy.cs.nott.ac.uk/fplunch/weblog/?p=237 >> 42. http://www.haskell.org/mailman/listinfo/haskell >> 43. http://sequence.complete.org/ >> 44. http://planet.haskell.org/ >> 45. http://sequence.complete.org/node/feed >> 46. http://haskell.org/ >> 47. http://haskell.org/haskellwiki/HWN >> 48. http://patch-tag.com/r/jfredett/HWN2/pullrepo%20HWN2 >> _______________________________________________ >> Haskell-Cafe mailing list >> Haskell-Cafe@haskell.org >> http://www.haskell.org/mailman/listinfo/haskell-cafe >> > > > > -- > ===================== > Patrick LeBoutillier > Rosem?re, Qu?bec, Canada > > > _______________________________________________ > 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/20091011/c0051cb6/attachment-0001.html From jfredett at gmail.com Sun Oct 11 11:19:35 2009 From: jfredett at gmail.com (Joe Fredette) Date: Sun Oct 11 10:56:59 2009 Subject: [Haskell-beginners] Re: [Haskell-cafe] Haskell Weekly News: Issue 134 - October 10, 2009 In-Reply-To: References: <4ad03c0a.9753f10a.238d.3a88@mx.google.com> Message-ID: <4055ED6E-9503-425B-8DAA-53C9B188FFB4@gmail.com> I'm happy to tack it on to the sendout, but as others have mentioned, subscription to haskell-general (to use GManes nomenclature) is probably the better option. -beginners, iirc, is principally for questions, not community content. Is this the consensus over there? I'll do whatever you folks decide on... /Joe On Oct 11, 2009, at 8:10 AM, Patrick LeBoutillier wrote: > Hi, > > Could/should the Haskell Weekly News be posted to the beginners list > as well? > > I normally don't follow haskell-cafe (too much traffic and generally > above my level I must admit...), but I like to follow what's going > on in the Haskell community. > > > Patrick > > On Sat, Oct 10, 2009 at 3:47 AM, wrote: > > --------------------------------------------------------------------------- > Haskell Weekly News > http://sequence.complete.org/hwn/20091010 > Issue 134 - October 10, 2009 > --------------------------------------------------------------------------- > Welcome to issue 134 of HWN, a newsletter covering developments in > the > [1]Haskell community. > > What with Don Stewart's [2]call to [3]arms to lead Haskell to > conquest > over (E)DSL-land, I've once again tried to highlight discussion of > EDSL's this week. Fortunately, it was actually more difficult > choosing > what _not_ to include this week, since there was so much discussion > about DSLs and Syntax extensions (a related notion, in my opinion). > Also, this week Bryan O'Sullivan put his Criterion Library to good > use > on the `text` package, leading to [4]code which is more than ten > times > faster than before! With all this fantastic news, I won't hold you > up > any longer, Haskellers, the Haskell Weekly News! > > Announcements > > CfPart: FMICS 2009, 2-3 November 2009, Final Call. FMICS 2009 > workshop > chair [5]announced the final call for particpaction for FMICS 2009 > > ICFP videos now available. Wouter Swierstra [6]announced the > availablity of videos from the International Conference on > Functional > Programming (ICFP) > > GPipe-1.0.0: A functional graphics API for programmable GPUs. Tobias > Bexelius [7]announced the first release of GPie, a functional > graphics > API for programmable GPUs. > > text 0.5, a major revision of the Unicode text library. Bryan > O'Sullivan [8]announced a new, major version of the text package. > New > API features, and huge improvments in speed, as Bryan says, 'Get it > while it's fresh on Hackage, folks!' > > vty-ui 0.2. Jonathan Daugherty [9]announced a new version of the > vty-ui > package, with fewer bugs, more widgets, and cleaner code due to new > more powerful abstractions. > > htzaar-0.0.1. Tom Hawkins [10]announced HTZAAR, a Haskell > implementation of TZAAR > > Graphalyze-0.8.0.0 and SourceGraph-0.5.5.0. Ivan Lazar Miljenovic > [11]announced To keep this editor happy, Ivan released two new > packaged > in one announcement. This time, he's added Legend support to > Graphalyze, but also many new changes to SourceGraph, including a > legend so you can see what all the symbols mean, Better color > support, > and much more. > > TxtSushi 0.4.0. Keith Sheppard [12]announced a new version of > TxtSushi, > a set of command line utilities for processing CSV and TSV files. > > Discussion > > Applicative do? Philippa Cowderoy [13]asked about a `do` like syntax > for Applicative functors. > > How to add use custom preprocessor in cabal. Bernd Brassel [14]asked > how to add a custom preprocessor to the build chain of a cabal file. > > On DSLs - one last time. Gunther Schmidt [15]summarized his > impressions > on al the recent discussion of DSLs > > What is a DSL? Oleg [16]offered some insight into different > [17]properties that can be part of a single tagless framework. He > also > pointed to some slides and other materials such as a website > [18]here > and slides [19]here about DSL implementations and definitions. > > What is a DSL? Gunther Schmidt [20]posed the question, 'What is a > DSL', > and with some further questions added by yours truly, a lively > discussion about the definition of a DSL ensued. > > Finally tagless - stuck with implementation of 'lam'. Gunther > Schmidt > [21]asked another question about Finally Tagless DSLs and > resolving an > issue with the implementation of 'lam' > > Blog noise > > [22]Haskell news from the [23]blogosphere. Blog posts from people > new > to the Haskell community are marked with >>>, be sure to welcome > them! > * Darcs: [24]darcs weekly news #43. > * JP Moresmau: [25]What client for an Haskell Multi Player Game?. > * Mikael Vejdemo Johansson (Syzygy-): [26][MATH198] Third > lecture is > up. > * Bryan O'Sullivan: [27]Announcing a major revision of the Haskell > text library. > * Eric Kow (kowey): [28]darcs hashed-storage work merged (woo!). > * David Amos: [29]Symmetries of PG(n,Fq). > * The GHC Team: [30]Parallelism /= Concurrency. > * >>> Nefigah: [31]Fake World Haskell. Nefigah, a recent > addition to > the community, has been working through RWH, and is providing > some > excellent examples. Though, This editor prefers the title 'Real > Life Haskell' as opposed to his choice. > * Tom Schrijvers: [32]Release 0.6 of Monadic Constraint > Programming. > * Neil Brown: [33]Concurrency Can Be Deterministic (But The Type > System Doesn't Know It). > * Clint Moore: [34]Curiously Parallel. > * Galois, Inc: [35]Tech Talk: Constructing A Universal Domain for > Reasoning About Haskell Datatypes. > * Neil Brown: [36]Terminal Concurrency: The Printing Process. > * Sean Leather: [37]'Upwards and downwards accumulations on trees' > translated into Haskell. > * Mikael Vejdemo Johansson (Syzygy-): [38][MATH 198] Second > lecture. > * Chris Smith: [39]View Patterns as Pattern Matching for Records. > * Chris Smith: [40]Playing With Records. > * FP Lunch: [41]Left Kan extensions of containers. > > Quotes of the Week > > * Baughn: Blum Blum Shub, a PRNG derived from poking around > R'Lyeh. > * ksf: * lambdabot locks up ksf in a Monad mmmmh it's warm > and > fuzzy in here. > * monochrom: Don't wrap your head around Haskell. Immerse! Wrap > Haskell around your head. > * chak: ... In other words, FP is inevitable. > * robreim: I'm in your base hacking all your lambdas > * gwern: RAM is overrated, swap is where it's at ;) > * idnar: [to gwern] swap to a ramdisk! ;P > > About the Haskell Weekly News > > New editions are posted to [42]the Haskell mailing list as well as > to > [43]the Haskell Sequence and [44]Planet Haskell. [45]RSS is also > available, and headlines appear on [46]haskell.org. > > To help create new editions of this newsletter, please see the > information on [47]how to contribute. Send stories to jfredett . > at . > gmail . dot . com. The darcs repository is available at darcs get > [48]http://patch-tag.com/r/jfredett/HWN2/pullrepo HWN2 . > > References > > 1. http://haskell.org/ > 2. http://www.haskell.org/pipermail/haskell-cafe/2009-October/067203.html > 3. http://www.reddit.com/r/haskell/comments/9qk54/haskell_should_own_the_edsl_space_a_call_to_arms/ > 4. http://www.serpentine.com/blog/2009/10/09/announcing-a-major-revision-of-the-haskell-text-library/ > 5. http://article.gmane.org/gmane.comp.lang.haskell.general/17555 > 6. http://article.gmane.org/gmane.comp.lang.haskell.general/17552 > 7. http://article.gmane.org/gmane.comp.lang.haskell.general/17546 > 8. http://article.gmane.org/gmane.comp.lang.haskell.cafe/64591 > 9. http://article.gmane.org/gmane.comp.lang.haskell.cafe/64529 > 10. http://article.gmane.org/gmane.comp.lang.haskell.cafe/64516 > 11. http://article.gmane.org/gmane.comp.lang.haskell.cafe/64399 > 12. http://article.gmane.org/gmane.comp.lang.haskell.cafe/64301 > 13. http://thread.gmane.org/gmane.comp.lang.haskell.cafe/64616 > 14. http://thread.gmane.org/gmane.comp.lang.haskell.cafe/64558 > 15. http://thread.gmane.org/gmane.comp.lang.haskell.cafe/64552 > 16. http://thread.gmane.org/gmane.comp.lang.haskell.cafe/64533 > 17. http://thread.gmane.org/gmane.comp.lang.haskell.cafe/ > 18. http://dsl09.blogspot.com/ > 19. http://dsl09.blogspot.com/2009/07/panel.html > 20. http://thread.gmane.org/gmane.comp.lang.haskell.cafe/64474 > 21. http://thread.gmane.org/gmane.comp.lang.haskell.cafe/64343 > 22. http://planet.haskell.org/ > 23. http://haskell.org/haskellwiki/Blog_articles > 24. http://blog.darcs.net/2009/10/darcs-weekly-news-43.html > 25. http://jpmoresmau.blogspot.com/2009/10/what-client-for-haskell-multi-player.html > 26. http://blog.mikael.johanssons.org/archive/2009/10/math198-third-lecture-is-up/ > 27. http://www.serpentine.com/blog/2009/10/09/announcing-a-major-revision-of-the-haskell-text-library/ > 28. http://koweycode.blogspot.com/2009/10/darcs-hashed-storage-work-merged-woo.html > 29. http://haskellformaths.blogspot.com/2009/10/symmetries-of-pgnfq.html > 30. http://ghcmutterings.wordpress.com/2009/10/06/parallelism-concurrency/ > 31. http://my.life-is-virtual.com/2009/10/07/fake-world-haskell-part-1/ > 32. http://tomschrijvers.blogspot.com/2009/10/release-06-of-monadic-constraint.html > 33. http://chplib.wordpress.com/2009/10/07/concurrency-can-be-deterministic/ > 34. http://www.l2mlogistics.com/2009/10/curiously-parallel.html > 35. http://www.galois.com/blog/2009/10/06/huffman-universal/ > 36. http://chplib.wordpress.com/2009/10/06/the-printing-process/ > 37. http://feedproxy.google.com/~r/splonderzoek/~3/4E8TZPDZ-aM/ > upwards-and-downwards-accumulations-on.html > 38. http://blog.mikael.johanssons.org/archive/2009/10/math-198-second-lecture/ > 39. http://cdsmith.wordpress.com/2009/10/04/view-patterns-as-pattern-matching-for-records/ > 40. http://cdsmith.wordpress.com/2009/10/03/playing-with-records/ > 41. http://sneezy.cs.nott.ac.uk/fplunch/weblog/?p=237 > 42. http://www.haskell.org/mailman/listinfo/haskell > 43. http://sequence.complete.org/ > 44. http://planet.haskell.org/ > 45. http://sequence.complete.org/node/feed > 46. http://haskell.org/ > 47. http://haskell.org/haskellwiki/HWN > 48. http://patch-tag.com/r/jfredett/HWN2/pullrepo%20HWN2 > _______________________________________________ > Haskell-Cafe mailing list > Haskell-Cafe@haskell.org > http://www.haskell.org/mailman/listinfo/haskell-cafe > > > > -- > ===================== > Patrick LeBoutillier > Rosem?re, Qu?bec, Canada > From ben.franksen at online.de Sun Oct 11 16:04:06 2009 From: ben.franksen at online.de (Ben Franksen) Date: Sun Oct 11 16:17:26 2009 Subject: [Haskell-beginners] Re: [Haskell-cafe] Haskell Weekly News: Issue 134 - October 10, 2009 References: <4ad03c0a.9753f10a.238d.3a88@mx.google.com> Message-ID: Patrick LeBoutillier wrote: > Could/should the Haskell Weekly News be posted to the beginners list as > well? > > I normally don't follow haskell-cafe (too much traffic and generally above > my level I must admit...), but I like to follow what's going on in the > Haskell community. I find reading the HWN is a lot is a lot more convenient with a web browser, you don't have to jump up and down the document to find the links. There is also an RSS feed (http://sequence.complete.org/node/feed) to keep you up to date. Cheers Ben From k.srikanth.opensource at gmail.com Mon Oct 12 02:21:49 2009 From: k.srikanth.opensource at gmail.com (Srikanth K) Date: Mon Oct 12 01:59:09 2009 Subject: [Haskell-beginners] using quickcheck for blackbox testing for 3rd party apps. Message-ID: Hi All, I am exploring use of quickCheck to define the test-specification for a 3rd party component. The component is basically an Command/Response type of application. Hence to validate it, I thought that the best way might be to take the following approach(based on the quickCheckM paper) a. describe the component in a purely functional way. The functions would take the "AppState", "Command" as parameters and would return a Response. Let me call this the "Abstract Application" The signature for this would therefore be of the form abstract_App :: AppState -> Command -> Response b. define an agent (Let me call it as Real_App) that that would (i) take a initial State, "Command" and send it to the "Application Under test" using some IPC mechanisms(probably socket or FIFO files) (ii) parse the received response and send it back as "Response". As a result the the API for the agent would be of the form real_APP :: AppState -> Command -> IO Response c. write a quick-check specification that would send a bunch of commands to the "Abstract Application" and check for equivalence with the value returned by the "real_App" However, being a newbie, I am not able to understand how I can extract the "Response" part from the IO Response. I want something as follows prop_App state_x command_y = (abstract_App state_x command_y ) == (real_App state_x command_y) The issue here is that since the reall_App would return a "IO Response" and not "Response" I cannot really compare the abstract_App with the real_App. Any thought on how I can extract the value from the "IO Response" so that I can compare the the two implementations for equvivalence. FYI, I have gone through the various quickcheck related docs like http://www.cs.chalmers.se/~rjmh/QuickCheck/ http://www.haskell.org/haskellwiki/QuickCheck_as_a_test_set_generator Thanks - Srikanth -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20091012/ac4cdfde/attachment.html From falecomedgar at gmail.com Mon Oct 12 08:26:28 2009 From: falecomedgar at gmail.com (Edgar GOMES DE ARAUJO) Date: Mon Oct 12 08:02:47 2009 Subject: [Haskell-beginners] Problem to compile an example of Data Parallel Haskell Message-ID: <4AD32074.9020500@gmail.com> Hi, I have tried to compile an example of DPH (Data prallel Haskell) but I am not getting any success. The target is the concmop.hs (http://darcs.haskell.org/ghc-6.10/packages/dph/examples/concomp/) for calculate connected components in a graph. I am using GHC 6.10.4 compiled with extras libs (which also includes DPH 0.3.0). I've got this error. Seems that GHC can't find these functions/types. --------------------------------------------------------------------------------------------------------------------- edgar@edgar-ubuntu:~/Desktop/dph$ ghc --make -fdph-par -XTypeOperators ./concomp.hs [1 of 4] Compiling Graph ( Graph.hs, Graph.o ) Graph.hs:11:45: Not in scope: type constructor or class `:*:' Graph.hs:23:6: Not in scope: `hPutU' Graph.hs:33:15: Not in scope: `hGetU' --------------------------------------------------------------------------------------------------------------------- Using the example from Ben Jao Ming (http://overtag.dk/wordpress/2009/04/data-parallel-haskell/), I can successfully compile and run. Could anyone help me? -- Kind regards, ___________________________________________ *Ir. Edgar Gomes * Metallurgical Engineer /Ghent University/ Department Metallurgy and Materials Science B-9052 Zwijnaarde-Gent Belgium -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20091012/a9bbc791/attachment.html From DekuDekuplex at Yahoo.com Tue Oct 13 02:18:38 2009 From: DekuDekuplex at Yahoo.com (Benjamin L.Russell) Date: Tue Oct 13 01:57:23 2009 Subject: [Haskell-beginners] Re: Haskell Weekly News: Issue 134 - October 10, 2009 References: <4ad03c0a.9753f10a.238d.3a88@mx.google.com> Message-ID: <9i68d5lgo92aunl8pqaq808kmttpqafsld@4ax.com> Either way is fine with me. Since HWN also contains beginner-related topics, personally, I don't think that posting it to Haskell-Beginners takes the group any more off-topic than posting it to Haskell-Cafe (although I can see the argument against cross-posting it to mailing lists in addition to the main Haskell mailing list). If it winds up getting posted to Haskell-Beginners, I'll gladly read it there, but if it just gets posted to the main Haskell mailing list, I'll read it there, also. -- Benjamin L. Russell On Sun, 11 Oct 2009 14:31:22 +0200, Max Rabkin wrote: >Why don't you subscribe to haskell? It's much lower volume, and I >think it's a better option than taking -beginners off-topic. > >--Max > >On Sun, Oct 11, 2009 at 2:10 PM, Patrick LeBoutillier > wrote: >> Hi, >> >> Could/should the Haskell Weekly News be posted to the beginners list as >> well? >> >> I normally don't follow haskell-cafe (too much traffic and generally above >> my level I must admit...), but I like to follow what's going on in the >> Haskell community. >> >> >> Patrick -- Benjamin L. Russell / DekuDekuplex at Yahoo dot com http://dekudekuplex.wordpress.com/ Translator/Interpreter / Mobile: +011 81 80-3603-6725 "Furuike ya, kawazu tobikomu mizu no oto." -- Matsuo Basho^ From byorgey at seas.upenn.edu Tue Oct 13 11:51:39 2009 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Tue Oct 13 11:28:55 2009 Subject: [Haskell-beginners] Problem to compile an example of Data Parallel Haskell In-Reply-To: <4AD32074.9020500@gmail.com> References: <4AD32074.9020500@gmail.com> Message-ID: <20091013155139.GA2687@seas.upenn.edu> On Mon, Oct 12, 2009 at 02:26:28PM +0200, Edgar GOMES DE ARAUJO wrote: > Hi, > > I have tried to compile an example of DPH (Data prallel Haskell) but I am > not getting any success. > The target is the concmop.hs > (http://darcs.haskell.org/ghc-6.10/packages/dph/examples/concomp/) for > calculate connected components in a graph. I am using GHC 6.10.4 compiled > with extras libs (which also includes DPH 0.3.0). > I've got this error. Seems that GHC can't find these functions/types. > > --------------------------------------------------------------------------------------------------------------------- > edgar@edgar-ubuntu:~/Desktop/dph$ ghc --make -fdph-par -XTypeOperators > ./concomp.hs > [1 of 4] Compiling Graph ( Graph.hs, Graph.o ) > > Graph.hs:11:45: Not in scope: type constructor or class `:*:' > > Graph.hs:23:6: Not in scope: `hPutU' > > Graph.hs:33:15: Not in scope: `hGetU' > --------------------------------------------------------------------------------------------------------------------- Perhaps that file is just missing an import? IIRC these things are from Data.Array.Vector, from the uvector package. -Brent From byorgey at seas.upenn.edu Tue Oct 13 12:04:52 2009 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Tue Oct 13 11:42:08 2009 Subject: [Haskell-beginners] using quickcheck for blackbox testing for 3rd party apps. In-Reply-To: References: Message-ID: <20091013160452.GB2687@seas.upenn.edu> On Mon, Oct 12, 2009 at 11:51:49AM +0530, Srikanth K wrote: > > However, being a newbie, I am not able to understand how I can extract the > "Response" part from the IO Response. > > I want something as follows > prop_App state_x command_y = (abstract_App state_x command_y ) == (real_App > state_x command_y) > > > The issue here is that since the reall_App would return a "IO Response" and > not "Response" I cannot really compare the abstract_App with the real_App. > > Any thought on how I can extract the value from the "IO Response" so that I > can compare the the two implementations for equvivalence. Hi Srikanth, The short answer is: you can't extract a Response from an IO Response. Once something is infected with IO, there's no cure!* However, you can compare the responses if you make the result of prop_App an IO action itself: prop_App :: State -> Command -> IO Bool prop_App state_x command_y = do realResponse <- real_App state_x command_y return $ abstract_App state_x command_y == realResponse Off the top of my head I'm not sure of the proper way to run such monadic tests with QuickCheck, but it can definitely be done. -Brent * Some smart-alecks might pipe up with something about unsafePerformIO here. But that's not a cure, it's more like performing an emergency tracheotomy with a ballpoint pen. From daniel.is.fischer at web.de Tue Oct 13 13:34:36 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Tue Oct 13 13:13:09 2009 Subject: [Haskell-beginners] using quickcheck for blackbox testing for 3rd party apps. In-Reply-To: <20091013160452.GB2687@seas.upenn.edu> References: <20091013160452.GB2687@seas.upenn.edu> Message-ID: <200910131934.36808.daniel.is.fischer@web.de> Am Dienstag 13 Oktober 2009 18:04:52 schrieb Brent Yorgey: > Brent > > * Some smart-alecks might pipe up with something about unsafePerformIO > ? here. ?But that's not a cure, it's more like performing an emergency > ? tracheotomy with a ballpoint pen. Quote of the month! From wrwills at gmail.com Wed Oct 14 00:44:34 2009 From: wrwills at gmail.com (Robert Wills) Date: Wed Oct 14 00:21:50 2009 Subject: [Haskell-beginners] new locale? Message-ID: <4AD55732.6010106@gmail.com> Hello, http://hackage.haskell.org/package/old-locale-1.0.0.1 says "This package provides the old locale library. For new code, the new locale library is recommended." Does anyone know where I would find the new library? Thanks, Rob From mpm at alumni.caltech.edu Wed Oct 14 16:50:11 2009 From: mpm at alumni.caltech.edu (Michael Mossey) Date: Wed Oct 14 16:27:41 2009 Subject: [Haskell-beginners] "sleep" in Haskell Message-ID: <4AD63983.8000307@alumni.caltech.edu> I can't find a "sleep" function in Haskell. This relates to my wish to use PortMidi and control the timing of actions. Maybe there is another way to do this sort of thing in Haskell. Can anyone help, either with PortMidi or just a general explanation of how IO operations could be placed at certain points in time? Thanks, Mike From jfredett at gmail.com Wed Oct 14 16:51:50 2009 From: jfredett at gmail.com (Joe Fredette) Date: Wed Oct 14 16:29:12 2009 Subject: [Haskell-beginners] "sleep" in Haskell In-Reply-To: <4AD63983.8000307@alumni.caltech.edu> References: <4AD63983.8000307@alumni.caltech.edu> Message-ID: <2815F39F-AEB3-49D6-9337-B44690DA0E7A@gmail.com> As I recall, System.Thread (or something similar) provides "threadSleep". A quick hoogle ought to find it. hoogle -> haskell.org/hoogle On Oct 14, 2009, at 4:50 PM, Michael Mossey wrote: > I can't find a "sleep" function in Haskell. This relates to my wish > to use PortMidi and control the timing of actions. Maybe there is > another way to do this sort of thing in Haskell. Can anyone help, > either with PortMidi or just a general explanation of how IO > operations could be placed at certain points in time? > > Thanks, > Mike > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners From mpm at alumni.caltech.edu Wed Oct 14 16:54:43 2009 From: mpm at alumni.caltech.edu (Michael Mossey) Date: Wed Oct 14 16:31:59 2009 Subject: [Haskell-beginners] "sleep" in Haskell In-Reply-To: <2815F39F-AEB3-49D6-9337-B44690DA0E7A@gmail.com> References: <4AD63983.8000307@alumni.caltech.edu> <2815F39F-AEB3-49D6-9337-B44690DA0E7A@gmail.com> Message-ID: <4AD63A93.2040007@alumni.caltech.edu> Hi Joe, My program does not need to be threaded, so initially I ignored System.Thread, but perhaps either (1) This particular function can be used in a single-thread process or (2) I need to learn how to use threads if I want to place IO operations in time. ? Joe Fredette wrote: > As I recall, System.Thread (or something similar) provides > "threadSleep". A quick hoogle ought to find it. > > hoogle -> haskell.org/hoogle > > On Oct 14, 2009, at 4:50 PM, Michael Mossey wrote: > >> I can't find a "sleep" function in Haskell. This relates to my wish to >> use PortMidi and control the timing of actions. Maybe there is another >> way to do this sort of thing in Haskell. Can anyone help, either with >> PortMidi or just a general explanation of how IO operations could be >> placed at certain points in time? >> >> Thanks, >> Mike >> _______________________________________________ >> Beginners mailing list >> Beginners@haskell.org >> http://www.haskell.org/mailman/listinfo/beginners > From mpm at alumni.caltech.edu Wed Oct 14 16:58:00 2009 From: mpm at alumni.caltech.edu (Michael Mossey) Date: Wed Oct 14 16:35:16 2009 Subject: [Haskell-beginners] "sleep" in Haskell In-Reply-To: <4AD63A41.4090705@flippac.org> References: <4AD63983.8000307@alumni.caltech.edu> <4AD63A41.4090705@flippac.org> Message-ID: <4AD63B58.5060705@alumni.caltech.edu> Hi Philippa, I think you have the right idea, and I want to try that too, but right now I'm using a kind of scattershot approach to try and learn everything I can, both about MIDI and Haskell in general. Since nothing is currently working, getting anything to work would be a learning experience. Thanks, Mike Philippa Cowderoy wrote: > Michael Mossey wrote: >> I can't find a "sleep" function in Haskell. This relates to my wish to >> use PortMidi and control the timing of actions. Maybe there is another >> way to do this sort of thing in Haskell. Can anyone help, either with >> PortMidi or just a general explanation of how IO operations could be >> placed at certain points in time? >> > > Y'know, I can't think of a single language where I'd consider using the > sleep function to time MIDI output. It's normally done with buffered > I/O. I don't know PortMidi, though. > From jfredett at gmail.com Wed Oct 14 16:58:55 2009 From: jfredett at gmail.com (Joe Fredette) Date: Wed Oct 14 16:36:11 2009 Subject: [Haskell-beginners] "sleep" in Haskell In-Reply-To: <4AD63A93.2040007@alumni.caltech.edu> References: <4AD63983.8000307@alumni.caltech.edu> <2815F39F-AEB3-49D6-9337-B44690DA0E7A@gmail.com> <4AD63A93.2040007@alumni.caltech.edu> Message-ID: Your program may not be multithreaded, but it is threaded. :) so threadSleep will still work, it'll just pause the thread it's executed in, namely the main thread, which is the one you want. So I guess the answer to your first question is yes, it can be used. And 2, no you don't need to learn to use threads, beyond the very basic calling of threadSleep. On Oct 14, 2009, at 4:54 PM, Michael Mossey wrote: > > Hi Joe, > > My program does not need to be threaded, so initially I ignored > System.Thread, but perhaps either > > (1) This particular function can be used in a single-thread process > > or > > (2) I need to learn how to use threads if I want to place IO > operations in time. > > ? > > Joe Fredette wrote: >> As I recall, System.Thread (or something similar) provides >> "threadSleep". A quick hoogle ought to find it. >> hoogle -> haskell.org/hoogle >> On Oct 14, 2009, at 4:50 PM, Michael Mossey wrote: >>> I can't find a "sleep" function in Haskell. This relates to my >>> wish to use PortMidi and control the timing of actions. Maybe >>> there is another way to do this sort of thing in Haskell. Can >>> anyone help, either with PortMidi or just a general explanation of >>> how IO operations could be placed at certain points in time? >>> >>> Thanks, >>> Mike >>> _______________________________________________ >>> Beginners mailing list >>> Beginners@haskell.org >>> http://www.haskell.org/mailman/listinfo/beginners > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners From jfredett at gmail.com Wed Oct 14 17:00:07 2009 From: jfredett at gmail.com (Joe Fredette) Date: Wed Oct 14 16:37:31 2009 Subject: [Haskell-beginners] "sleep" in Haskell In-Reply-To: <4AD63B58.5060705@alumni.caltech.edu> References: <4AD63983.8000307@alumni.caltech.edu> <4AD63A41.4090705@flippac.org> <4AD63B58.5060705@alumni.caltech.edu> Message-ID: <650292FE-20BC-40C1-B6C1-0C54A7295B06@gmail.com> >> Second reply to the same email, completely off topic. MIDI + Haskell = Computer Music, I'm already excited to see results, make sure you put stuff on hackage! /Joe On Oct 14, 2009, at 4:58 PM, Michael Mossey wrote: > Hi Philippa, > > I think you have the right idea, and I want to try that too, but > right now I'm using a kind of scattershot approach to try and learn > everything I can, both about MIDI and Haskell in general. Since > nothing is currently working, getting anything to work would be a > learning experience. > > Thanks, > Mike > > Philippa Cowderoy wrote: >> Michael Mossey wrote: >>> I can't find a "sleep" function in Haskell. This relates to my >>> wish to use PortMidi and control the timing of actions. Maybe >>> there is another way to do this sort of thing in Haskell. Can >>> anyone help, either with PortMidi or just a general explanation of >>> how IO operations could be placed at certain points in time? >>> >> Y'know, I can't think of a single language where I'd consider using >> the sleep function to time MIDI output. It's normally done with >> buffered I/O. I don't know PortMidi, though. > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners From mpm at alumni.caltech.edu Wed Oct 14 19:14:45 2009 From: mpm at alumni.caltech.edu (Michael Mossey) Date: Wed Oct 14 18:51:59 2009 Subject: [Haskell-beginners] "sleep" in Haskell In-Reply-To: <2815F39F-AEB3-49D6-9337-B44690DA0E7A@gmail.com> References: <4AD63983.8000307@alumni.caltech.edu> <2815F39F-AEB3-49D6-9337-B44690DA0E7A@gmail.com> Message-ID: <4AD65B65.80906@alumni.caltech.edu> Hmm, I can't find anything like this. Do you have any other ideas? Joe Fredette wrote: > As I recall, System.Thread (or something similar) provides > "threadSleep". A quick hoogle ought to find it. > > hoogle -> haskell.org/hoogle > > On Oct 14, 2009, at 4:50 PM, Michael Mossey wrote: > >> I can't find a "sleep" function in Haskell. This relates to my wish to >> use PortMidi and control the timing of actions. Maybe there is another >> way to do this sort of thing in Haskell. Can anyone help, either with >> PortMidi or just a general explanation of how IO operations could be >> placed at certain points in time? >> >> Thanks, >> Mike >> _______________________________________________ >> Beginners mailing list >> Beginners@haskell.org >> http://www.haskell.org/mailman/listinfo/beginners > From allbery at ece.cmu.edu Wed Oct 14 19:16:39 2009 From: allbery at ece.cmu.edu (Brandon S. Allbery KF8NH) Date: Wed Oct 14 18:54:09 2009 Subject: [Haskell-beginners] "sleep" in Haskell In-Reply-To: <4AD65B65.80906@alumni.caltech.edu> References: <4AD63983.8000307@alumni.caltech.edu> <2815F39F-AEB3-49D6-9337-B44690DA0E7A@gmail.com> <4AD65B65.80906@alumni.caltech.edu> Message-ID: On Oct 14, 2009, at 19:14 , Michael Mossey wrote: > Hmm, I can't find anything like this. Do you have any other ideas? It's actually in Control.Concurrent. -- brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu electrical and computer engineering, carnegie mellon university KF8NH -------------- next part -------------- A non-text attachment was scrubbed... Name: PGP.sig Type: application/pgp-signature Size: 195 bytes Desc: This is a digitally signed message part Url : http://www.haskell.org/pipermail/beginners/attachments/20091014/e8c4363f/PGP.bin From aditya.siram at gmail.com Wed Oct 14 19:20:42 2009 From: aditya.siram at gmail.com (aditya siram) Date: Wed Oct 14 18:57:52 2009 Subject: [Haskell-beginners] "sleep" in Haskell In-Reply-To: References: <4AD63983.8000307@alumni.caltech.edu> <2815F39F-AEB3-49D6-9337-B44690DA0E7A@gmail.com> <4AD65B65.80906@alumni.caltech.edu> Message-ID: <594f78210910141620i5622cd7bnbedfc7385e97f24b@mail.gmail.com> Look here: http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Concurrent.html#v:threadDelay Here is a sample function that delays for 3 seconds and then dies: main :: IO () main = do threadDelay 30000 return () -deech On Wed, Oct 14, 2009 at 6:16 PM, Brandon S. Allbery KF8NH < allbery@ece.cmu.edu> wrote: > On Oct 14, 2009, at 19:14 , Michael Mossey wrote: > >> Hmm, I can't find anything like this. Do you have any other ideas? >> > > > It's actually in Control.Concurrent. > > -- > brandon s. allbery [solaris,freebsd,perl,pugs,haskell] allbery@kf8nh.com > system administrator [openafs,heimdal,too many hats] allbery@ece.cmu.edu > electrical and computer engineering, carnegie mellon university KF8NH > > > > _______________________________________________ > 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/20091014/4568ad4c/attachment.html From magnus at therning.org Thu Oct 15 03:49:27 2009 From: magnus at therning.org (Magnus Therning) Date: Thu Oct 15 03:26:38 2009 Subject: [Haskell-beginners] "sleep" in Haskell In-Reply-To: <594f78210910141620i5622cd7bnbedfc7385e97f24b@mail.gmail.com> References: <4AD63983.8000307@alumni.caltech.edu> <2815F39F-AEB3-49D6-9337-B44690DA0E7A@gmail.com> <4AD65B65.80906@alumni.caltech.edu> <594f78210910141620i5622cd7bnbedfc7385e97f24b@mail.gmail.com> Message-ID: On Thu, Oct 15, 2009 at 12:20 AM, aditya siram wrote: > Look here: > http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Concurrent.html#v:threadDelay > > Here is a sample function that delays for 3 seconds and then dies: > main :: IO () > main = do > ??? threadDelay 30000 > ??? return () Sorry, but I can't help myself... the above is equivalent to main :: IO () main = threadDelay 3000 /M -- Magnus Therning (OpenPGP: 0xAB4DFBA4) magnus?therning?org Jabber: magnus?therning?org http://therning.org/magnus identi.ca|twitter: magthe From lorcan.mcdonald at gmail.com Thu Oct 15 04:03:31 2009 From: lorcan.mcdonald at gmail.com (Lorcan McDonald) Date: Thu Oct 15 03:40:41 2009 Subject: [Haskell-beginners] "sleep" in Haskell In-Reply-To: References: <4AD63983.8000307@alumni.caltech.edu> <2815F39F-AEB3-49D6-9337-B44690DA0E7A@gmail.com> <4AD65B65.80906@alumni.caltech.edu> <594f78210910141620i5622cd7bnbedfc7385e97f24b@mail.gmail.com> Message-ID: Hi, I was looking at something similar recently and I found the examples in the hmidi package to be useful for this. cabal install hmidi and the examples are in ~/.cabal/packages/hackage.haskell.org/hmidi/0.1/hmidi-0.1.tar.gz (on Mac OS X) On Thu, Oct 15, 2009 at 8:49 AM, Magnus Therning wrote: > On Thu, Oct 15, 2009 at 12:20 AM, aditya siram wrote: >> Look here: >> http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Concurrent.html#v:threadDelay >> >> Here is a sample function that delays for 3 seconds and then dies: >> main :: IO () >> main = do >> ??? threadDelay 30000 >> ??? return () > > Sorry, but I can't help myself... the above is equivalent to > > ?main :: IO () > ?main = threadDelay 3000 > > /M > > -- > Magnus Therning ? ? ? ? ? ? ? ? ? ? ? ?(OpenPGP: 0xAB4DFBA4) > magnus?therning?org ? ? ? ? ?Jabber: magnus?therning?org > http://therning.org/magnus ? ? ? ? identi.ca|twitter: magthe > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > From mpm at alumni.caltech.edu Thu Oct 15 04:30:10 2009 From: mpm at alumni.caltech.edu (Michael Mossey) Date: Thu Oct 15 04:07:25 2009 Subject: [Haskell-beginners] "sleep" in Haskell In-Reply-To: References: <4AD63983.8000307@alumni.caltech.edu> <2815F39F-AEB3-49D6-9337-B44690DA0E7A@gmail.com> <4AD65B65.80906@alumni.caltech.edu> <594f78210910141620i5622cd7bnbedfc7385e97f24b@mail.gmail.com> Message-ID: <4AD6DD92.3080007@alumni.caltech.edu> That's very helpful info. I'm glad hmidi comes with an example. I can't find one for PortMidi, but maybe I'll figure out some basics with hmidi. Lorcan McDonald wrote: > Hi, > > I was looking at something similar recently and I found the examples > in the hmidi package to be useful for this. > > cabal install hmidi > and the examples are in > ~/.cabal/packages/hackage.haskell.org/hmidi/0.1/hmidi-0.1.tar.gz (on > Mac OS X) > From ryantemple145 at googlemail.com Thu Oct 15 19:19:34 2009 From: ryantemple145 at googlemail.com (Ryan Temple) Date: Fri Oct 16 01:25:47 2009 Subject: [Haskell-beginners] sorting by elements in a tuple Message-ID: <33ff8d520910151619k572e1defi8f7fc0d043adf595@mail.gmail.com> I am wanting to sort a list of tuples by the first and then the second item in the tuple. So [(1,2),(4,2),(2,5)] would become [(1,2),(2,5),(4,2)] I am attempting to use the Sorty function but don't seem to be having much luck tuplesort :: [(a,b)] -> [(a,b)] tuplesort [(_,_)] = sortBy (comparing fst) -- this is an attempt to organise by the first element in the tuple Any help would be much appreciated -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20091015/1ed3244e/attachment.html From daniel.is.fischer at web.de Fri Oct 16 02:07:11 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Fri Oct 16 01:45:37 2009 Subject: [Haskell-beginners] sorting by elements in a tuple In-Reply-To: <33ff8d520910151619k572e1defi8f7fc0d043adf595@mail.gmail.com> References: <33ff8d520910151619k572e1defi8f7fc0d043adf595@mail.gmail.com> Message-ID: <200910160807.11766.daniel.is.fischer@web.de> Am Freitag 16 Oktober 2009 01:19:34 schrieb Ryan Temple: > I am wanting to sort a list of tuples by the first and then the second item > in the tuple. So [(1,2),(4,2),(2,5)] would become [(1,2),(2,5),(4,2)] That's what "sort" already does: sort [(1,2),(4,2),(2,5),(4,1)] gives [(1,2),(2,5),(4,1),(4,2)] > I am attempting to use the Sorty function but don't seem to be having much luck > > tuplesort :: [(a,b)] -> [(a,b)] > tuplesort [(_,_)] That defines tuplesort on lists containing exactly one pair, probably not what you want. > = sortBy (comparing fst) This doesn't conform to the type you gave. "sortBy (comparing fst)" has type Ord a => [(a,b)] -> [(a,b)] So your definition of tuplesort has type Ord a => [(c,d)] -> [(a,b)] -> [(a,b)] which doesn't look like the type of a sorting function. > -- this is an attempt to > organise by the first element in the tuple You would achieve that by tuplesort = sortBy (comparing fst) or, eta-expanded, tuplesort xs = sortBy (comparing fst) xs But if you finally want to sort by the second component also (subordinate to sorting by the first component), it's much easier to go the whole way at once and use plain "sort". > > Any help would be much appreciated From chaddai.fouche at gmail.com Fri Oct 16 04:37:38 2009 From: chaddai.fouche at gmail.com (=?UTF-8?B?Q2hhZGRhw68gRm91Y2jDqQ==?=) Date: Fri Oct 16 04:14:45 2009 Subject: [Haskell-beginners] sorting by elements in a tuple In-Reply-To: <200910160807.11766.daniel.is.fischer@web.de> References: <33ff8d520910151619k572e1defi8f7fc0d043adf595@mail.gmail.com> <200910160807.11766.daniel.is.fischer@web.de> Message-ID: On Fri, Oct 16, 2009 at 8:07 AM, Daniel Fischer wrote: > You would achieve that by > > tuplesort = sortBy (comparing fst) > > or, eta-expanded, > > tuplesort xs = sortBy (comparing fst) xs > > But if you finally want to sort by the second component also (subordinate to sorting by > the first component), it's much easier to go the whole way at once and use plain "sort". > For completeness sake, you can do it like that : tuplesort = sortBy (comparing fst `mplus` comparing snd) It's interesting to know how to do it if you want to sort using a different function than the compare of Ord. The "mplus" use some instances of Monoid that you'll find in Data.Monoid so don't forget to import it. -- Jeda? From chaddai.fouche at gmail.com Fri Oct 16 04:42:30 2009 From: chaddai.fouche at gmail.com (=?UTF-8?B?Q2hhZGRhw68gRm91Y2jDqQ==?=) Date: Fri Oct 16 04:19:37 2009 Subject: [Haskell-beginners] sorting by elements in a tuple In-Reply-To: References: <33ff8d520910151619k572e1defi8f7fc0d043adf595@mail.gmail.com> <200910160807.11766.daniel.is.fischer@web.de> Message-ID: On Fri, Oct 16, 2009 at 10:37 AM, Chadda? Fouch? wrote: > On Fri, Oct 16, 2009 at 8:07 AM, Daniel Fischer > wrote: >> You would achieve that by >> >> tuplesort = sortBy (comparing fst) >> >> or, eta-expanded, >> >> tuplesort xs = sortBy (comparing fst) xs >> >> But if you finally want to sort by the second component also (subordinate to sorting by >> the first component), it's much easier to go the whole way at once and use plain "sort". > > tuplesort = sortBy (comparing fst `mplus` comparing snd) > Ooops, that's "mappend" rather than "mplus" : > tuplesort = sortBy (comparing fst `mappend` comparing snd) (it would be really cool if (++) was a synonym for mappend though as it used to be) -- Jeda? From david.demainlalune at gmail.com Fri Oct 16 08:46:16 2009 From: david.demainlalune at gmail.com (david hodgetts) Date: Fri Oct 16 08:23:25 2009 Subject: [Haskell-beginners] is there a best os to easily install external libraries ? Message-ID: <42a5d61a0910160546r73495af8oe1de66f7e2412823@mail.gmail.com> Hello everyone, to give some context: I am not a real developer. I am a motion designer who enjoys doing procedural animations and installations with tools such as processing, flash, openframeworks. This might explain some of the difficulties I describe below. Wanting to broaden my thinking skills and CS knowledge, I have started to learn Haskell, and I must say I am having a great time. I am at a point now where I would like to play around with some of the popular UI and graphical libraries (gtk+, freeGlut, sdl etc). But my experience up to now has been a bit painfull in the sense that most of these packages don't install nicely with caball ( on windows XP). I managed to get gtk, freeglut, and curl to install after long hours googling around and doing voodoo in msys and cygwin. Last night I tried to install the SDL bindings to follow a blog post about automata, but completely failed. This got me to wondering if the haskell experience with regards to installing external libraries might be any easier on Linux? in advance many thanks best regards david hodgetts -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20091016/fafaf748/attachment.html From agarwal1975 at gmail.com Fri Oct 16 11:41:47 2009 From: agarwal1975 at gmail.com (Ashish Agarwal) Date: Fri Oct 16 11:19:13 2009 Subject: [Haskell-beginners] parsing upto n items with parsec Message-ID: Hi. I'm just learning Parsec and Haskell. It is a great library, and I see how "many" lets you parse 0 or more items, "many1" parses 1 or more items, and "count" parses exactly n items. However, there is no combinator that parses between m and n items. What would be the best implementation for this? -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20091016/7417473d/attachment.html From orclev at gmail.com Fri Oct 16 21:35:25 2009 From: orclev at gmail.com (Kyle Murphy) Date: Fri Oct 16 21:12:30 2009 Subject: [Haskell-beginners] is there a best os to easily install external libraries ? In-Reply-To: <42a5d61a0910160546r73495af8oe1de66f7e2412823@mail.gmail.com> References: <42a5d61a0910160546r73495af8oe1de66f7e2412823@mail.gmail.com> Message-ID: <2db78cee0910161835q494a0685tf013c17848651f83@mail.gmail.com> Short answer is yes, it is easier on Linux. The long answer is that most of the libraries are cross platform, and there are some fairly nice libraries/utilities that can make getting an install in Windows working fairly pain-free, but they're hard to track down and they tend to be... touchy... about version numbers and such, in particular they're often a few versions behind what most people are using. From the standpoint of doing development, Linux is definitely much easier to work with, but it has its own quirks to deal with. Getting the system up and running the way you want tends to be a little intimidating for a newcomer, but once you've got it all taken care of it tends to chug along fairly well on its own. Ubuntu Linux is the most newbie friendly in general (they tend to try to do everything for you if possible) and you'll find the answers to most common questions/issues with it using a simple google search, but from the standpoint of Haskell development it tends to be a little tougher to work with (most of the packaged libraries for it are a bit out of date and/or have been modified from the standard ones). In contrast something like Gentoo or Arch Linux are very newbie un-friendly (you're expected to know and understand what kind of hardware you're installing it on to start with), but because they're built from source (more or less) they tend to have the very latest versions of everything. Using Arch or Gentoo you'll probably learn more, and once you've got it all straightened out it will be very easy to work with, but it's going to be an uphill battle just getting it installed and working right. Ubuntu is about as close as you can get to a point and click install with Linux, but is likely to cause you some stress when working with Haskell (although not as much as Windows). Really it comes down to what you want to get out of the process. If you've got a spare computer you don't mind taking out of commission for a while (possibly as long as a month or two) and you've got some time to play around with things and maybe learn a bit about your hardware, I'd recommend Arch Linux. If you want something you can have up and running within a day and don't want to worry about any of the details of, I'd go with Ubuntu. For a nice compromise that doesn't involve replacing you're OS you might want to take a look at Cygwin if you haven't yet. It's more or less a port of the standard Linux shells and utilities to Windows and can go a long way towards bringing the Linux experience into the Windows world. On Fri, Oct 16, 2009 at 8:46 AM, david hodgetts < david.demainlalune@gmail.com> wrote: > Hello everyone, > to give some context: I am not a real developer. I am a motion designer who > enjoys doing procedural animations and installations with tools such as > processing, flash, openframeworks. This might explain some of the > difficulties I describe below. > > Wanting to broaden my thinking skills and CS knowledge, I have started to > learn Haskell, and I must say I am having a great time. > > I am at a point now where I would like to play around with some of the > popular UI and graphical libraries (gtk+, freeGlut, sdl etc). But my > experience up to now has been a bit painfull in the sense that most of these > packages don't install nicely with caball ( on windows XP). I managed to get > gtk, freeglut, and curl to install after long hours googling around and > doing voodoo in msys and cygwin. Last night I tried to install the SDL > bindings to follow a blog post about automata, but completely failed. This > got me to wondering if the haskell experience with regards to installing > external libraries might be any easier on Linux? > > in advance many thanks > > best regards > > > david hodgetts > > > > > _______________________________________________ > 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/20091016/93b3f60d/attachment.html From jfredett at gmail.com Fri Oct 16 21:42:15 2009 From: jfredett at gmail.com (Joe Fredette) Date: Fri Oct 16 21:19:24 2009 Subject: [Haskell-beginners] is there a best os to easily install external libraries ? In-Reply-To: <2db78cee0910161835q494a0685tf013c17848651f83@mail.gmail.com> References: <42a5d61a0910160546r73495af8oe1de66f7e2412823@mail.gmail.com> <2db78cee0910161835q494a0685tf013c17848651f83@mail.gmail.com> Message-ID: <570752B2-23EA-400C-B6B2-56C2DC312813@gmail.com> Excellent answer, but let me add, instead of Cygwin, andLinux gives you an actual linux system which runs _on top of_ your windows system -- as in a full kernel, etc (actually a distro of ubuntu). It's based on coLinux, but is very nice. You get the full weight of linux, but without having to dual-boot or otherwise do anything drastic with your OS. /Joe On Oct 16, 2009, at 9:35 PM, Kyle Murphy wrote: > Short answer is yes, it is easier on Linux. The long answer is that > most of the libraries are cross platform, and there are some fairly > nice libraries/utilities that can make getting an install in Windows > working fairly pain-free, but they're hard to track down and they > tend to be... touchy... about version numbers and such, in > particular they're often a few versions behind what most people are > using. From the standpoint of doing development, Linux is definitely > much easier to work with, but it has its own quirks to deal with. > Getting the system up and running the way you want tends to be a > little intimidating for a newcomer, but once you've got it all taken > care of it tends to chug along fairly well on its own. Ubuntu Linux > is the most newbie friendly in general (they tend to try to do > everything for you if possible) and you'll find the answers to most > common questions/issues with it using a simple google search, but > from the standpoint of Haskell development it tends to be a little > tougher to work with (most of the packaged libraries for it are a > bit out of date and/or have been modified from the standard ones). > In contrast something like Gentoo or Arch Linux are very newbie un- > friendly (you're expected to know and understand what kind of > hardware you're installing it on to start with), but because they're > built from source (more or less) they tend to have the very latest > versions of everything. Using Arch or Gentoo you'll probably learn > more, and once you've got it all straightened out it will be very > easy to work with, but it's going to be an uphill battle just > getting it installed and working right. Ubuntu is about as close as > you can get to a point and click install with Linux, but is likely > to cause you some stress when working with Haskell (although not as > much as Windows). > > Really it comes down to what you want to get out of the process. If > you've got a spare computer you don't mind taking out of commission > for a while (possibly as long as a month or two) and you've got some > time to play around with things and maybe learn a bit about your > hardware, I'd recommend Arch Linux. If you want something you can > have up and running within a day and don't want to worry about any > of the details of, I'd go with Ubuntu. > > For a nice compromise that doesn't involve replacing you're OS you > might want to take a look at Cygwin if you haven't yet. It's more or > less a port of the standard Linux shells and utilities to Windows > and can go a long way towards bringing the Linux experience into the > Windows world. > > On Fri, Oct 16, 2009 at 8:46 AM, david hodgetts > wrote: > Hello everyone, > > to give some context: I am not a real developer. I am a motion > designer who enjoys doing procedural animations and installations > with tools such as processing, flash, openframeworks. This might > explain some of the difficulties I describe below. > > Wanting to broaden my thinking skills and CS knowledge, I have > started to learn Haskell, and I must say I am having a great time. > > I am at a point now where I would like to play around with some of > the popular UI and graphical libraries (gtk+, freeGlut, sdl etc). > But my experience up to now has been a bit painfull in the sense > that most of these packages don't install nicely with caball ( on > windows XP). I managed to get gtk, freeglut, and curl to install > after long hours googling around and doing voodoo in msys and > cygwin. Last night I tried to install the SDL bindings to follow a > blog post about automata, but completely failed. This got me to > wondering if the haskell experience with regards to installing > external libraries might be any easier on Linux? > > in advance many thanks > > best regards > > > david hodgetts > > > > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners From Christian.Maeder at dfki.de Sat Oct 17 05:15:40 2009 From: Christian.Maeder at dfki.de (Christian Maeder) Date: Sat Oct 17 04:52:45 2009 Subject: [Haskell-beginners] Re: parsing upto n items with parsec In-Reply-To: References: Message-ID: <4AD98B3C.7080804@dfki.de> Ashish Agarwal schrieb: > Hi. I'm just learning Parsec and Haskell. It is a great library, and I > see how "many" lets you parse 0 or more items, "many1" parses 1 or more > items, and "count" parses exactly n items. However, there is no > combinator that parses between m and n items. What would be the best > implementation for this? I would write an "upTo" parser with the same type as "count" that parses not exactly but at most n items. Your desired parser is than the concatenated results of "count m p" and "upTo (n - m) p" (achieved by "liftM2 (++)"). For "upTo" a recursive definition seems best (other may come up with tricky combinator application.) "upTo 0 p" (or something less than 0) returns "[]" and "upTo n p" is an "option [] ..." parser of one "p" result followed by the "upTo (n - 1) p" result: "option [] (liftM2 (:) p (upTo (n - 1) p))" HTH Christian Another possibility is to use "many" and check if the resulting list has the desired length (if not fail), but that may consume too many tokens that subsequent parsers are supposed to consume. From david.demainlalune at gmail.com Sat Oct 17 06:00:23 2009 From: david.demainlalune at gmail.com (david hodgetts) Date: Sat Oct 17 05:37:27 2009 Subject: [Haskell-beginners] is there a best os to easily install external libraries ? In-Reply-To: <570752B2-23EA-400C-B6B2-56C2DC312813@gmail.com> References: <42a5d61a0910160546r73495af8oe1de66f7e2412823@mail.gmail.com> <2db78cee0910161835q494a0685tf013c17848651f83@mail.gmail.com> <570752B2-23EA-400C-B6B2-56C2DC312813@gmail.com> Message-ID: <42a5d61a0910170300nffb73co2ff69725c9f0048e@mail.gmail.com> Thank you very much for the long and very informative answer. I was going to try Ubuntu, but since I have a spare machine, I think I will rather "plunge deep" and try to get arch linux running. Also, thanks for mentioning andLinux, I will certainly check it out. best regards david hodgetts -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20091017/4bf4d21f/attachment.html From will_n48 at yahoo.com Sat Oct 17 14:29:39 2009 From: will_n48 at yahoo.com (Will Ness) Date: Sat Oct 17 14:07:08 2009 Subject: [Haskell-beginners] Re: map question References: <20090917113138.GC15221@enterprise.localdomain> <20090917120150.GA20349@seas.upenn.edu> Message-ID: Brent Yorgey seas.upenn.edu> writes: > > By the way, the reason > > map (+1) [1,2,3,4] > > works but > > map (-1) [1,2,3,4] > > doesn't is because of an ugly corner of Haskell syntax: -1 here is > parsed as negative one, rather than an operator section with > subtraction. The 'subtract' function is provided exactly for this > purpose, so that you can write > > map (subtract 1) [1,2,3,4] > Then why wouldn't (`-`1) parse at at all? And not even (`(-)`1) ? I know this doesn't parse, my question is, why wouldn't it be made valid syntax? It seems consistent. (`mod`2) parses, why not (`-`2) ? From deniz.a.m.dogan at gmail.com Sat Oct 17 14:41:30 2009 From: deniz.a.m.dogan at gmail.com (Deniz Dogan) Date: Sat Oct 17 14:18:53 2009 Subject: [Haskell-beginners] Re: map question In-Reply-To: References: <20090917113138.GC15221@enterprise.localdomain> <20090917120150.GA20349@seas.upenn.edu> Message-ID: <7b501d5c0910171141y56be9da3wa70658d09009719c@mail.gmail.com> 2009/10/17 Will Ness : > Brent Yorgey seas.upenn.edu> writes: > >> >> By the way, the reason >> >> ? map (+1) [1,2,3,4] >> >> works but >> >> ? map (-1) [1,2,3,4] >> >> doesn't is because of an ugly corner of Haskell syntax: -1 here is >> parsed as negative one, rather than an operator section with >> subtraction. ?The 'subtract' function is provided exactly for this >> purpose, so that you can write >> >> ? map (subtract 1) [1,2,3,4] >> > > Then why wouldn't (`-`1) parse at at all? And not even (`(-)`1) ? > > I know this doesn't parse, my question is, why wouldn't it be made valid > syntax? It seems consistent. (`mod`2) parses, why not (`-`2) ? > > > > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > `This` syntax is used to make functions work infix, whereas operators already are infix. For this reason, it wouldn't make much sense to use `this` syntax (what's that called anyways?) on an operator. `(-)` would in some sense be the same thing as just "-", since () is used to make operators prefix and `` is used to make functions infix. -- Deniz Dogan From byorgey at seas.upenn.edu Sat Oct 17 14:43:36 2009 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Sat Oct 17 14:20:37 2009 Subject: [Haskell-beginners] Re: map question In-Reply-To: References: <20090917113138.GC15221@enterprise.localdomain> <20090917120150.GA20349@seas.upenn.edu> Message-ID: <20091017184336.GA18717@seas.upenn.edu> On Sat, Oct 17, 2009 at 06:29:39PM +0000, Will Ness wrote: > Brent Yorgey seas.upenn.edu> writes: > > > > > By the way, the reason > > > > map (+1) [1,2,3,4] > > > > works but > > > > map (-1) [1,2,3,4] > > > > doesn't is because of an ugly corner of Haskell syntax: -1 here is > > parsed as negative one, rather than an operator section with > > subtraction. The 'subtract' function is provided exactly for this > > purpose, so that you can write > > > > map (subtract 1) [1,2,3,4] > > > > Then why wouldn't (`-`1) parse at at all? And not even (`(-)`1) ? > > I know this doesn't parse, my question is, why wouldn't it be made valid > syntax? It seems consistent. (`mod`2) parses, why not (`-`2) ? `backticks` are only for making (prefix) functions into (infix) operators. - is already an infix operator, so putting it in backticks would be redundant. As for `(-)`, arbitrary expressions cannot go inside backticks, and for good reason: what would `(2 `mod`)` parse as? However, certainly different choices might have been possible. -Brent From byorgey at seas.upenn.edu Sat Oct 17 14:45:22 2009 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Sat Oct 17 14:22:24 2009 Subject: [Haskell-beginners] new locale? In-Reply-To: <4AD55732.6010106@gmail.com> References: <4AD55732.6010106@gmail.com> Message-ID: <20091017184522.GA19147@seas.upenn.edu> On Wed, Oct 14, 2009 at 05:44:34AM +0100, Robert Wills wrote: > Hello, > > http://hackage.haskell.org/package/old-locale-1.0.0.1 > > says "This package provides the old locale library. For new code, the new > locale library is recommended." > > Does anyone know where I would find the new library? What locale functions are you trying to use? As far as I can tell, there actually is no "new locale library" and functions for working with the locale have been spread across various other libraries as appropriate. -Brent From will_n48 at yahoo.com Sun Oct 18 07:11:07 2009 From: will_n48 at yahoo.com (Will Ness) Date: Sun Oct 18 06:48:34 2009 Subject: [Haskell-beginners] Re: map question References: <20090917113138.GC15221@enterprise.localdomain> <20090917120150.GA20349@seas.upenn.edu> <20091017184336.GA18717@seas.upenn.edu> Message-ID: Brent Yorgey seas.upenn.edu> writes: > > On Sat, Oct 17, 2009 at 06:29:39PM +0000, Will Ness wrote: > > > > Then why wouldn't (`-`1) parse at at all? And not even (`(-)`1) ? > > > > I know this doesn't parse, my question is, why wouldn't it be made valid > > syntax? It seems consistent. (`mod`2) parses, why not (`-`2) ? > > `backticks` are only for making (prefix) functions into (infix) > operators. - is already an infix operator, so putting it in backticks > would be redundant. As for `(-)`, arbitrary expressions cannot go > inside backticks, and for good reason: what would `(2 `mod`)` parse > as? > > However, certainly different choices might have been possible. > > -Brent > backticks could've been made no-op for operators. What's so wrong with (`:`[])? It'd just be the same as (:[]). Except for `-`, where it would finally provide us with possibility to write a shortcut for the ugly (flip (-) 1) as (`-`1). (2`mod`) is a unary operation :: (Integral a) => a -> a. Putting it inside backticks would require it be a binary infix op, causing a type mis-match. For (-) it could choose the binary version over the unary. Or it could stay illegal for the parenthesised expressions, and just made legal syntax for operators, as (`:`[]). I don't know how easy or even possible it is to implement; I'm just saying it makes sense, for me. From john.moore54 at gmail.com Sun Oct 18 09:42:35 2009 From: john.moore54 at gmail.com (John Moore) Date: Sun Oct 18 09:19:35 2009 Subject: [Haskell-beginners] I/O Message-ID: <4f7ad1ad0910180642u687317edu64d3eb86a377e0e7@mail.gmail.com> Hi, Could someone please tell me how to check this program. The problem is where do I find the output? I laod this into ghc and it comes up OK modules loaded: Main gchi> How do I now check if it works or see the result. Program below (taken from real world haskell) -- file: ch07/toupper-imp.hs import System.IO import Data.Char(toUpper) main :: IO () main = do inh <- openFile "quux.txt" ReadMode outh <- openFile "output.txt" WriteMode mainloop inh outh hClose inh hClose outh mainloop :: Handle -> Handle -> IO () mainloop inh outh = do ineof <- hIsEOF inh if ineof then return () else do inpStr <- hGetLine inh hPutStrLn outh (map toUpper inpStr) mainloop inh outh John -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20091018/470b6ecc/attachment.html From daniel.is.fischer at web.de Sun Oct 18 09:56:32 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Sun Oct 18 09:34:51 2009 Subject: [Haskell-beginners] I/O In-Reply-To: <4f7ad1ad0910180642u687317edu64d3eb86a377e0e7@mail.gmail.com> References: <4f7ad1ad0910180642u687317edu64d3eb86a377e0e7@mail.gmail.com> Message-ID: <200910181556.33520.daniel.is.fischer@web.de> Am Sonntag 18 Oktober 2009 15:42:35 schrieb John Moore: > Hi, > Could someone please tell me how to check this program. The problem is > where do I find the output? I laod this into ghc and it comes up OK modules > loaded: Main > gchi> > > How do I now check if it works or see the result. > > Program below (taken from real world haskell) > -- file: ch07/toupper-imp.hs > import System.IO > import Data.Char(toUpper) > > main :: IO () > main = do > inh <- openFile "quux.txt" ReadMode > outh <- openFile "output.txt" WriteMode > mainloop inh outh > hClose inh > hClose outh > mainloop :: Handle -> Handle -> IO () > mainloop inh outh = > do ineof <- hIsEOF inh > if ineof > then return () > else do inpStr <- hGetLine inh > hPutStrLn outh (map toUpper inpStr) > mainloop inh outh > John Make sure you have a file quux.txt in your directory, but no valuable file output.txt. Load the module, type ":main" or just "main" at the ghci prompt and compare the then present file output.txt (if it isn't present, that's bad) to quux.txt (open both in an editor). It should be the uppercase version of that. Or compile it (ghc --make toupper-imp.hs) and run the binary. From daniel.is.fischer at web.de Sun Oct 18 10:12:56 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Sun Oct 18 09:51:14 2009 Subject: [Haskell-beginners] Re: map question In-Reply-To: References: <20090917113138.GC15221@enterprise.localdomain> <20091017184336.GA18717@seas.upenn.edu> Message-ID: <200910181612.57167.daniel.is.fischer@web.de> Am Sonntag 18 Oktober 2009 13:11:07 schrieb Will Ness: > Brent Yorgey seas.upenn.edu> writes: > > On Sat, Oct 17, 2009 at 06:29:39PM +0000, Will Ness wrote: > > > Then why wouldn't (`-`1) parse at at all? And not even (`(-)`1) ? > > > > > > I know this doesn't parse, my question is, why wouldn't it be made > > > valid syntax? It seems consistent. (`mod`2) parses, why not (`-`2) ? > > > > `backticks` are only for making (prefix) functions into (infix) > > operators. - is already an infix operator, so putting it in backticks > > would be redundant. As for `(-)`, arbitrary expressions cannot go > > inside backticks, and for good reason: what would `(2 `mod`)` parse > > as? > > > > However, certainly different choices might have been possible. > > > > -Brent > > backticks could've been made no-op for operators. What's so wrong with > (`:`[])? It'd just be the same as (:[]). Makes writing the parser more complicated. Makes reading code more irritating. > > Except for `-`, where it would finally provide us with possibility to write > a shortcut for the ugly (flip (-) 1) as (`-`1). That's ugly too. My favourite is subtract 1. > > (2`mod`) is a unary operation :: (Integral a) => a -> a. Putting it inside > backticks would require it be a binary infix op, causing a type mis-match. instance (Integral a) => Integral (b -> a) where ... Evil, yes, but then f `(2 `mod`)` x would type-check. But anyway, there are operators where a backticked section would type-check: f `(g `.`)` x That's not good. > > For (-) it could choose the binary version over the unary. > > Or it could stay illegal for the parenthesised expressions, and just made > legal syntax for operators, as (`:`[]). > > I don't know how easy or even possible it is to implement; I'm just saying > it makes sense, for me. But it has downsides. From john.moore54 at gmail.com Sun Oct 18 10:52:28 2009 From: john.moore54 at gmail.com (John Moore) Date: Sun Oct 18 10:29:29 2009 Subject: [Haskell-beginners] count function Message-ID: <4f7ad1ad0910180752u1c146966rcd6788ecd0d422d0@mail.gmail.com> Hi, Trying to get aprogram to count all the lines or words from a file. Not sure about the syntax. Probably missing some. Here's what I have so far any help in the right direction would be great. import System.IO main :: IO () main = do incom <- openFile "file.txt" ReadMode otcom <- openFile "prob.txt" WriteMode fCount incom otcom hClose incom hClose otcom fCount incomh otcomh = do eof <- hIsEof incomh if eof then return() else do c <- hGetCount incom it tells me parse error on = John -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20091018/a46cf273/attachment.html From alexander.dunlap at gmail.com Sun Oct 18 11:01:53 2009 From: alexander.dunlap at gmail.com (Alexander Dunlap) Date: Sun Oct 18 10:39:13 2009 Subject: [Haskell-beginners] count function In-Reply-To: <4f7ad1ad0910180752u1c146966rcd6788ecd0d422d0@mail.gmail.com> References: <4f7ad1ad0910180752u1c146966rcd6788ecd0d422d0@mail.gmail.com> Message-ID: <57526e770910180801h8e0d6f8h4dcf37cd0f8fad6f@mail.gmail.com> On Sun, Oct 18, 2009 at 7:52 AM, John Moore wrote: > Hi, > ?Trying to get aprogram to count all the lines or words from a file. Not > sure about the syntax. Probably missing some. > Here's what I have so far any help in the right direction would be great. > > import System.IO > main :: IO () > main = do > ?????? incom <- openFile "file.txt" ReadMode > ?????? otcom <- openFile "prob.txt" WriteMode > ?????? fCount incom otcom > ?????? hClose incom > ?????? hClose otcom > ?????? fCount incomh otcomh = > ???????? do eof <- hIsEof incomh > ???????????? if eof > ????????????? then return() > ????????????? else do c <- hGetCount incom > > it tells me parse error on = > > John Add a "where" before fCount - this lets you introduce auxiliary functions. Alternatively, you could define fCount at the top level. The way I would count all of the lines in a file is (untested) fCountLines :: String -> IO Int fCountLines = length . lines . readFile Hope that helps. Alex From rziemba at gmail.com Sun Oct 18 12:40:48 2009 From: rziemba at gmail.com (Robert Ziemba) Date: Sun Oct 18 12:18:07 2009 Subject: [Haskell-beginners] count function In-Reply-To: <57526e770910180801h8e0d6f8h4dcf37cd0f8fad6f@mail.gmail.com> References: <4f7ad1ad0910180752u1c146966rcd6788ecd0d422d0@mail.gmail.com> <57526e770910180801h8e0d6f8h4dcf37cd0f8fad6f@mail.gmail.com> Message-ID: <65135790910180940y579e6682i503212cd0b91c874@mail.gmail.com> On Sun, Oct 18, 2009 at 8:01 AM, Alexander Dunlap < alexander.dunlap@gmail.com> wrote: > > > The way I would count all of the lines in a file is (untested) > > fCountLines :: String -> IO Int > fCountLines = length . lines . readFile > > I'm not sure if I did something wrong, but I could not get this to work because 'readFile' returns an IO String. I tried the following and it worked. fileLines str = readFile str >>= return . lines >>= return . length Is this a reasonable way to count lines? -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20091018/48f68bb1/attachment.html From daniel.is.fischer at web.de Sun Oct 18 12:51:41 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Sun Oct 18 12:31:05 2009 Subject: [Haskell-beginners] count function In-Reply-To: <65135790910180940y579e6682i503212cd0b91c874@mail.gmail.com> References: <4f7ad1ad0910180752u1c146966rcd6788ecd0d422d0@mail.gmail.com> <57526e770910180801h8e0d6f8h4dcf37cd0f8fad6f@mail.gmail.com> <65135790910180940y579e6682i503212cd0b91c874@mail.gmail.com> Message-ID: <200910181851.41363.daniel.is.fischer@web.de> Am Sonntag 18 Oktober 2009 18:40:48 schrieb Robert Ziemba: > On Sun, Oct 18, 2009 at 8:01 AM, Alexander Dunlap < > > alexander.dunlap@gmail.com> wrote: > > The way I would count all of the lines in a file is (untested) > > > > fCountLines :: String -> IO Int > > fCountLines = length . lines . readFile > > I'm not sure if I did something wrong, but I could not get this to work > because 'readFile' returns an IO String. I tried the following and it > worked. > > fileLines str = readFile str >>= return . lines >>= return . length > > Is this a reasonable way to count lines? Almost. It should be fileLines str = readFile str >>= return . length . lines (or return . length . lines =<< readFile str or fmap (length . lines) (readFile str) or...) From felipe.lessa at gmail.com Sun Oct 18 13:43:00 2009 From: felipe.lessa at gmail.com (Felipe Lessa) Date: Sun Oct 18 13:20:04 2009 Subject: [Haskell-beginners] count function In-Reply-To: <200910181851.41363.daniel.is.fischer@web.de> References: <4f7ad1ad0910180752u1c146966rcd6788ecd0d422d0@mail.gmail.com> <57526e770910180801h8e0d6f8h4dcf37cd0f8fad6f@mail.gmail.com> <65135790910180940y579e6682i503212cd0b91c874@mail.gmail.com> <200910181851.41363.daniel.is.fischer@web.de> Message-ID: <20091018174300.GA15249@kira.casa> On Sun, Oct 18, 2009 at 06:51:41PM +0200, Daniel Fischer wrote: > fileLines str = readFile str >>= return . length . lines This kind of construction requires a Monad, which is a lot of power. > fmap (length . lines) (readFile str) On the other hand, this only requires a Functor. Yes, in this particular case the type is going to be IO anyway, but 'fmap' should be used instead of '>>= return .'. -- Felipe. From will_n48 at yahoo.com Sun Oct 18 16:29:45 2009 From: will_n48 at yahoo.com (Will Ness) Date: Sun Oct 18 16:07:15 2009 Subject: [Haskell-beginners] Re: map question References: <20090917113138.GC15221@enterprise.localdomain> <20090917120150.GA20349@seas.upenn.edu> <7b501d5c0910171141y56be9da3wa70658d09009719c@mail.gmail.com> Message-ID: Deniz Dogan gmail.com> writes: > > `This` syntax is used to make functions work infix, whereas operators > already are infix. For this reason, it wouldn't make much sense to use > `this` syntax (what's that called anyways?) on an operator. It would be a no-op in general, so nobody would use it. > `(-)` would in some sense be the same thing as just "-", since () is > used to make operators prefix and `` is used to make functions infix. exactly, just that it would be a _binary_ (-) . From will_n48 at yahoo.com Sun Oct 18 16:40:46 2009 From: will_n48 at yahoo.com (Will Ness) Date: Sun Oct 18 16:18:15 2009 Subject: [Haskell-beginners] Re: map question References: <20090917113138.GC15221@enterprise.localdomain> <20091017184336.GA18717@seas.upenn.edu> <200910181612.57167.daniel.is.fischer@web.de> Message-ID: Daniel Fischer web.de> writes: > Am Sonntag 18 Oktober 2009 13:11:07 schrieb Will Ness: > > > > backticks could've been made no-op for operators. What's so wrong with > > (`:`[])? It'd just be the same as (:[]). > > Makes writing the parser more complicated. > Makes reading code more irritating. I suspected as much, but if it has some benefits, that would be justified. > > Except for `-`, where it would finally provide us with possibility to write > > a shortcut for the ugly (flip (-) 1) as (`-`1). > > That's ugly too. > My favourite is subtract 1. We could write (hypothetically) 'concatenate' for ++ too, but we don't. What's so great with writing out long names, that should be processed, mentally, to get at their meaning, when we have this nice operators syntax which makes the meaning immediately - visibly - apparent? The only problem in Haskell syntax is this unary-over-binary (-), so allowing backticks for operators as well as for regular names would finally provide a solution for it. Nobody would use it except for (-) sections. Or even, hypothetically, it could be used to allow unary operators in the language, in general. That whould be a mechanism for distinguishing the binary from the unary versions. Why not? Difficulty of writing a parser is not a valid reason if there's a clear utility in it. IMHO. From will_n48 at yahoo.com Sun Oct 18 16:57:38 2009 From: will_n48 at yahoo.com (Will Ness) Date: Sun Oct 18 16:35:03 2009 Subject: [Haskell-beginners] Re: map question References: <20090917113138.GC15221@enterprise.localdomain> <20091017184336.GA18717@seas.upenn.edu> <200910181612.57167.daniel.is.fischer@web.de> Message-ID: Daniel Fischer web.de> writes: > Am Sonntag 18 Oktober 2009 13:11:07 schrieb Will Ness: > > (2`mod`) is a unary operation :: (Integral a) => a -> a. Putting it inside > > backticks would require it be a binary infix op, causing a type mis-match. > > instance (Integral a) => Integral (b -> a) where ... > > Evil, yes, but then f `(2 `mod`)` x would type-check. Any problem that could be introduced for operators could be introduced for the regular named values as well. > > But anyway, there are operators where a backticked section would type-check: > > f `(g `.`)` x > > That's not good. Why? Is it not just (g .) f x ? What's the problem with it? But, the backticked parens syntax is less necessary. What I'd really like is for (`+`1) syntax to become legal. Prelude> :t let add=(+) in (`add`2) let add=(+) in (`add`2) :: (Num a) => a -> a Are we not supposed to be able to substitute equal for equal in Haskell? Prelude> :t (`(+)`2) :1:2: parse error on input `(' Prelude> :t (`+`2) :1:2: parse error on input `+' From deniz.a.m.dogan at gmail.com Sun Oct 18 17:04:51 2009 From: deniz.a.m.dogan at gmail.com (Deniz Dogan) Date: Sun Oct 18 16:42:10 2009 Subject: [Haskell-beginners] Re: map question In-Reply-To: References: <20090917113138.GC15221@enterprise.localdomain> <20090917120150.GA20349@seas.upenn.edu> <7b501d5c0910171141y56be9da3wa70658d09009719c@mail.gmail.com> Message-ID: <7b501d5c0910181404u6a5d6b0bm52cfb8bf3bd722af@mail.gmail.com> 2009/10/18 Will Ness : > Deniz Dogan gmail.com> writes: > >> >> `This` syntax is used to make functions work infix, whereas operators >> already are infix. For this reason, it wouldn't make much sense to use >> `this` syntax (what's that called anyways?) on an operator. > > It would be a no-op in general, so nobody would use it. > >> `(-)` would in some sense be the same thing as just "-", since () is >> used to make operators prefix and `` is used to make functions infix. > > exactly, just that it would be a _binary_ (-) . > So the only case where `(this)` syntax would be useful is where you want to make GHC understand you mean the binary (-) operator and you want to use point-free code. Now you have the choice between `(-)` and subtract, which one will it be? :) -- Deniz Dogan From will_n48 at yahoo.com Sun Oct 18 18:32:31 2009 From: will_n48 at yahoo.com (Will Ness) Date: Sun Oct 18 18:09:52 2009 Subject: [Haskell-beginners] Re: map question References: <20090917113138.GC15221@enterprise.localdomain> <20090917120150.GA20349@seas.upenn.edu> <7b501d5c0910171141y56be9da3wa70658d09009719c@mail.gmail.com> <7b501d5c0910181404u6a5d6b0bm52cfb8bf3bd722af@mail.gmail.com> Message-ID: Deniz Dogan gmail.com> writes: > 2009/10/18 Will Ness yahoo.com>: > > > > exactly, just that it would be a _binary_ (-) . > > So the only case where `(this)` syntax would be useful is where you > want to make GHC understand you mean the binary (-) operator and you > want to use point-free code. Now you have the choice between `(-)` and > subtract, which one will it be? :) subtract = flip (-) -- binary (`-`) = (-) -- binary The only one place where I'd like to use it, is in (`-`2), instead of (flip (-) 2) or (subtract 2) or (\a->a-2) or anything else. Also, it is not `(-)` syntax that I'm advocating for, but `-` syntax, in sections, to guide the selection of the operator's _binary_ version over its _unary_ version. Actually, I suspect there is no unary (-), but rather in (-2) the "-" is a part of number read syntax. Having it enclosed in backticks would stop it being read as a part of a number, and thus ensure it being correctly interpreted as an operator, binary as any other Haskell operator: Prelude> :t (-) (-) :: (Num a) => a -> a -> a Prelude> :t (`-`2) :1:2: parse error on input `-' Even (`-`) may be left as invalid syntax, as it is today: Prelude> :t (`foldl`) :1:8: parse error on input `)' Prelude> :t (`foldl`0) (`foldl`0) :: (Num a) => (a -> b -> a) -> [b] -> a From daniel.is.fischer at web.de Sun Oct 18 20:27:42 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Sun Oct 18 20:06:07 2009 Subject: [Haskell-beginners] Re: map question In-Reply-To: References: <20090917113138.GC15221@enterprise.localdomain> <200910181612.57167.daniel.is.fischer@web.de> Message-ID: <200910190227.42750.daniel.is.fischer@web.de> Am Sonntag 18 Oktober 2009 22:40:46 schrieb Will Ness: > Daniel Fischer web.de> writes: > > Am Sonntag 18 Oktober 2009 13:11:07 schrieb Will Ness: > > > backticks could've been made no-op for operators. What's so wrong with > > > (`:`[])? It'd just be the same as (:[]). > > > > Makes writing the parser more complicated. > > Makes reading code more irritating. > > I suspected as much, but if it has some benefits, that would be justified. I think you mean "if the benefits outweigh the downsides". If they do (or rather, if enough people believe they do and few enough believe it's the other way round), then it's justified. As of now, I don't believe it has substantial benefits (consequences which I regard as benefits). > > > > Except for `-`, where it would finally provide us with possibility to > > > write a shortcut for the ugly (flip (-) 1) as (`-`1). > > > > That's ugly too. > > My favourite is subtract 1. > > We could write (hypothetically) 'concatenate' for ++ too, but we don't. > What's so great with writing out long names, that should be processed, > mentally, to get at their meaning, when we have this nice operators syntax > which makes the meaning immediately - visibly - apparent? Operator syntax per se doesn't make the meaning immediately apparent. Only the meaning of well known operators (like +,-,*,/) and, to some extent, close derivations of them (like ++) is readily apparent. ($) or (>>=) aren't obvious when you first see them. However, I didn't want to advocate the use of alphabetic identifiers instead of operators, I meant that since (- 1) doesn't work, my favourite way to express it is (subtract 1). I find that nicer than (flip (-) 1), and also aesthetically superior to (`-` 1). Purely personal taste, of course. > > The only problem in Haskell syntax is this unary-over-binary (-), so Yes, that's unpleasant. However, for me it's a minor point. > allowing backticks for operators as well as for regular names would finally > provide a solution for it. > > Nobody would use it except for (-) sections. Are you sure? People abuse every oportunity you give them and then some, so I wouldn't be too surprised if gratuitous backticks seeped into Haskell code over time. > Or even, hypothetically, it > could be used to allow unary operators in the language, in general. That > whould be a mechanism for distinguishing the binary from the unary > versions. > > Why not? Difficulty of writing a parser is not a valid reason if there's a > clear utility in it. IMHO. From daniel.is.fischer at web.de Sun Oct 18 20:36:14 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Sun Oct 18 20:14:29 2009 Subject: [Haskell-beginners] Re: map question In-Reply-To: References: <20090917113138.GC15221@enterprise.localdomain> <200910181612.57167.daniel.is.fischer@web.de> Message-ID: <200910190236.14834.daniel.is.fischer@web.de> Am Sonntag 18 Oktober 2009 22:57:38 schrieb Will Ness: > Daniel Fischer web.de> writes: > > Am Sonntag 18 Oktober 2009 13:11:07 schrieb Will Ness: > > > (2`mod`) is a unary operation :: (Integral a) => a -> a. Putting it > > > inside backticks would require it be a binary infix op, causing a type > > > mis-match. > > > > instance (Integral a) => Integral (b -> a) where ... > > > > Evil, yes, but then f `(2 `mod`)` x would type-check. > > Any problem that could be introduced for operators could be introduced for > the regular named values as well. But allowing only one level of backticks, and only on plain identifiers limits the scope of possible problems. > > > But anyway, there are operators where a backticked section would > > type-check: > > > > f `(g `.`)` x > > > > That's not good. > > Why? Is it not just (g .) f x ? What's the problem with it? It's incredibly ugly, looks almost like a perl regex. From mpm at alumni.caltech.edu Mon Oct 19 04:49:17 2009 From: mpm at alumni.caltech.edu (Michael Mossey) Date: Mon Oct 19 04:26:22 2009 Subject: [Haskell-beginners] \x -> x < 0.5 && x > -0.5 Message-ID: <4ADC280D.1070109@alumni.caltech.edu> Is there a nifty way to write filter (\x -> x < 0.5 && x > -0.5) xs without explicitly using x? Maybe arrows? I have a vague understanding that arrows can "send" an argument to more than one computation. -Mike From magnus at therning.org Mon Oct 19 05:03:50 2009 From: magnus at therning.org (Magnus Therning) Date: Mon Oct 19 04:40:49 2009 Subject: [Haskell-beginners] \x -> x < 0.5 && x > -0.5 In-Reply-To: <4ADC280D.1070109@alumni.caltech.edu> References: <4ADC280D.1070109@alumni.caltech.edu> Message-ID: On Mon, Oct 19, 2009 at 9:49 AM, Michael Mossey wrote: > Is there a nifty way to write > > filter (\x -> x < 0.5 && x > -0.5) xs > > without explicitly using x? > > Maybe arrows? I have a vague understanding that arrows can "send" an > argument to more than one computation. Using &&& from Control.Arrow you can write something like file (uncurry (&&) . ((< 0.5) &&& (> -0.5))) xs /M -- Magnus Therning (OpenPGP: 0xAB4DFBA4) magnus?therning?org Jabber: magnus?therning?org http://therning.org/magnus identi.ca|twitter: magthe From gtener at gmail.com Mon Oct 19 05:05:20 2009 From: gtener at gmail.com (=?UTF-8?Q?Krzysztof_Skrz=C4=99tnicki?=) Date: Mon Oct 19 04:42:18 2009 Subject: [Haskell-beginners] \x -> x < 0.5 && x > -0.5 In-Reply-To: <4ADC280D.1070109@alumni.caltech.edu> References: <4ADC280D.1070109@alumni.caltech.edu> Message-ID: <220e47b40910190205t53152eb5jb28575cf74270e4e@mail.gmail.com> On Mon, Oct 19, 2009 at 10:49, Michael Mossey wrote: > Is there a nifty way to write > > filter (\x -> x < 0.5 && x > -0.5) xs > > without explicitly using x? > > Maybe arrows? I have a vague understanding that arrows can "send" an > argument to more than one computation. You can do that with arrows like this: Prelude Control.Arrow> let rangeA x y = ((>x) &&& (>> (uncurry (&&)) Prelude Control.Arrow> :t rangeA rangeA :: (Ord a) => a -> a -> a -> Bool Prelude Control.Arrow> filter (rangeA 4 8) [0..10] [5,6,7] Best regards Krzysztof Skrz?tnicki From luca_ciciriello at hotmail.com Mon Oct 19 05:22:26 2009 From: luca_ciciriello at hotmail.com (Luca Ciciriello) Date: Mon Oct 19 04:59:24 2009 Subject: [Haskell-beginners] \x -> x < 0.5 && x > -0.5 In-Reply-To: <4ADC280D.1070109@alumni.caltech.edu> References: <4ADC280D.1070109@alumni.caltech.edu> Message-ID: Had you considered list comprehension? [x | x <- xs, x < 0.5 && x > -0.5] Luca. > Date: Mon, 19 Oct 2009 01:49:17 -0700 > From: mpm@alumni.caltech.edu > To: beginners@haskell.org > Subject: [Haskell-beginners] \x -> x < 0.5 && x > -0.5 > > Is there a nifty way to write > > filter (\x -> x < 0.5 && x > -0.5) xs > > without explicitly using x? > > Maybe arrows? I have a vague understanding that arrows can "send" an > argument to more than one computation. > > -Mike > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners _________________________________________________________________ Use Windows Live Messenger for free on selected mobiles http://clk.atdmt.com/UKM/go/174426567/direct/01/ -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20091019/09a310c4/attachment.html From apfelmus at quantentunnel.de Mon Oct 19 06:02:19 2009 From: apfelmus at quantentunnel.de (Heinrich Apfelmus) Date: Mon Oct 19 05:39:58 2009 Subject: [Haskell-beginners] Re: \x -> x < 0.5 && x > -0.5 In-Reply-To: <4ADC280D.1070109@alumni.caltech.edu> References: <4ADC280D.1070109@alumni.caltech.edu> Message-ID: Michael Mossey wrote: > Is there a nifty way to write > > filter (\x -> x < 0.5 && x > -0.5) xs > > without explicitly using x? > > Maybe arrows? I have a vague understanding that arrows can "send" an > argument to more than one computation. That's a job for the reader monad. Lambda Fu, form 53 - silent reader of truth import Control.Monad import Control.Monad.Reader filter (liftM2 (&&) (< 0.5) (> -0.5)) xs Regards, apfelmus -- http://apfelmus.nfshost.com From Christian.Maeder at dfki.de Mon Oct 19 06:07:19 2009 From: Christian.Maeder at dfki.de (Christian Maeder) Date: Mon Oct 19 05:44:20 2009 Subject: [Haskell-beginners] Re: \x -> x < 0.5 && x > -0.5 In-Reply-To: <4ADC280D.1070109@alumni.caltech.edu> References: <4ADC280D.1070109@alumni.caltech.edu> Message-ID: <4ADC3A57.4020203@dfki.de> Michael Mossey schrieb: > Is there a nifty way to write > > filter (\x -> x < 0.5 && x > -0.5) xs > > without explicitly using x? Hoogle did not find a function of type: (b -> b -> b) -> (a -> b) -> (a -> b) -> a -> b or (b -> c -> d) -> (a -> b) -> (a -> c) -> a -> d But maybe such a function is worth being added to Data.Function. Cheers Christian From mail at paulvisschers.net Mon Oct 19 06:11:30 2009 From: mail at paulvisschers.net (Paul Visschers) Date: Mon Oct 19 05:48:27 2009 Subject: [Haskell-beginners] Re: \x -> x < 0.5 && x > -0.5 In-Reply-To: References: <4ADC280D.1070109@alumni.caltech.edu> Message-ID: <4ADC3B52.4050008@paulvisschers.net> Wouldn't it be better to make an instance of the Boolean class for functions, that way you could just write: filter ((< 0.5) && (> -0.5)) xs Not sure about the syntax, as the Haskell site is down. Also it will require that you install and import the Boolean class. One could ask if any of these methods are more readable than the original though. Paul Heinrich Apfelmus wrote: > Michael Mossey wrote: >> Is there a nifty way to write >> >> filter (\x -> x < 0.5 && x > -0.5) xs >> >> without explicitly using x? >> >> Maybe arrows? I have a vague understanding that arrows can "send" an >> argument to more than one computation. > > That's a job for the reader monad. > > > Lambda Fu, form 53 - silent reader of truth > > import Control.Monad > import Control.Monad.Reader > > filter (liftM2 (&&) (< 0.5) (> -0.5)) xs > > > > Regards, > apfelmus > > -- > http://apfelmus.nfshost.com > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners From vandijk.roel at gmail.com Mon Oct 19 07:25:20 2009 From: vandijk.roel at gmail.com (Roel van Dijk) Date: Mon Oct 19 07:02:18 2009 Subject: [Haskell-beginners] edit-compile-test loop In-Reply-To: References: <19e5d1d00909211435g1c123fb9m79b45d3736d1368@mail.gmail.com> <14ACFC65-6A9C-4549-AE55-BE873E202E7D@gmail.com> <19e5d1d00909211522v1a6eac25y862d1164e60c892c@mail.gmail.com> Message-ID: > [1] right now I use my own little hacked up testrunning script (just loads > the stuff in ghci, and I have one function that sits in a base "test" > directory, underwhich the project module hierarchy is mirrored. So if I > have, as I do now, Text/HWN/Parser/HWN.hs, then I also have > Test/Text/HWN/Parser/HWN_Test.hs. in the Test directory I have > Test_Harness.hs, which has a main function which runs all the tests in the > directories below it. Each directory has a similar Test_Harness.hs (though > all the names are different) which loads all the tests of the directories > below it, and exports a single test group. All of this is manual, though > I've been planning a kind of manager thing for it lately. But thats a story > for another day. I found the test-framework package to be quite useful for this. http://batterseapower.github.com/test-framework/ Using the defaultMain function you can easily define a small program which will run all your tests (quickcheck, hunit or other). The only problem I had with test-framework is that it prints the test results in nice colours. It looks pretty in a console but unreadable in ghci. Hackage seems to be down at the moment, but you can check out which other packages use test-framework on my hackage plaything: http://bifunctor.homelinux.net/~roel/cgi-bin/hackage-scripts/revdeps/test-framework Roel From vandijk.roel at gmail.com Mon Oct 19 07:38:15 2009 From: vandijk.roel at gmail.com (Roel van Dijk) Date: Mon Oct 19 07:15:11 2009 Subject: [Haskell-beginners] Re: \x -> x < 0.5 && x > -0.5 In-Reply-To: <4ADC3B52.4050008@paulvisschers.net> References: <4ADC280D.1070109@alumni.caltech.edu> <4ADC3B52.4050008@paulvisschers.net> Message-ID: Or simply write a function: between :: Ord a => a -> a -> a -> Bool between min max x = x > min && x < max filter (between (-0.5) 0.5) xs Keep it simple, stupid? From magnus at therning.org Mon Oct 19 09:33:36 2009 From: magnus at therning.org (Magnus Therning) Date: Mon Oct 19 09:10:36 2009 Subject: [Haskell-beginners] edit-compile-test loop In-Reply-To: References: <19e5d1d00909211435g1c123fb9m79b45d3736d1368@mail.gmail.com> <14ACFC65-6A9C-4549-AE55-BE873E202E7D@gmail.com> <19e5d1d00909211522v1a6eac25y862d1164e60c892c@mail.gmail.com> Message-ID: On Mon, Oct 19, 2009 at 12:25 PM, Roel van Dijk wrote: >> [1] right now I use my own little hacked up testrunning script (just loads >> the stuff in ghci, and I have one function that sits in a base "test" >> directory, underwhich the project module hierarchy is mirrored. So if I >> have, as I do now, Text/HWN/Parser/HWN.hs, then I also have >> Test/Text/HWN/Parser/HWN_Test.hs. in the Test directory I have >> Test_Harness.hs, which has a main function which runs all the tests in the >> directories below it. Each directory has a similar Test_Harness.hs (though >> all the names are different) which loads all the tests of the directories >> below it, and exports a single test group. All of this is manual, though >> I've been planning a kind of manager thing for it lately. But thats a story >> for another day. > > I found the test-framework package to be quite useful for this. > > http://batterseapower.github.com/test-framework/ > > Using the defaultMain function you can easily define a small program > which will run all your tests (quickcheck, hunit or other). The only > problem I had with test-framework is that it prints the test results > in nice colours. It looks pretty in a console but unreadable in ghci. > > Hackage seems to be down at the moment, but you can check out which > other packages use test-framework on my hackage plaything: > > http://bifunctor.homelinux.net/~roel/cgi-bin/hackage-scripts/revdeps/test-framework Yes, I've found this useful as well. I'm surprised it's not possible to turn off the colourisation... but `my-test --help` doesn't seem to offer any option for it. Maybe that's a good thing to add? ;-) /M -- Magnus Therning (OpenPGP: 0xAB4DFBA4) magnus?therning?org Jabber: magnus?therning?org http://therning.org/magnus identi.ca|twitter: magthe From nefigah at gmail.com Mon Oct 19 11:24:12 2009 From: nefigah at gmail.com (Jordan Cooper) Date: Mon Oct 19 11:01:09 2009 Subject: [Haskell-beginners] Re: \x -> x < 0.5 && x > -0.5 Message-ID: <301488c50910190824w43dde17cta55ac778f7a4661f@mail.gmail.com> Whoa... how on earth does this work? How does it interpret the sections as Reader monads? > That's a job for the reader monad. > > > Lambda Fu, form 53 - silent reader of truth > > import Control.Monad > import Control.Monad.Reader > > filter (liftM2 (&&) (< 0.5) (> -0.5)) xs > > > > Regards, > apfelmus From daniel.is.fischer at web.de Mon Oct 19 11:58:25 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Mon Oct 19 11:36:42 2009 Subject: [Haskell-beginners] Re: \x -> x < 0.5 && x > -0.5 In-Reply-To: <301488c50910190824w43dde17cta55ac778f7a4661f@mail.gmail.com> References: <301488c50910190824w43dde17cta55ac778f7a4661f@mail.gmail.com> Message-ID: <200910191758.26174.daniel.is.fischer@web.de> Am Montag 19 Oktober 2009 17:24:12 schrieb Jordan Cooper: > Whoa... how on earth does this work? How does it interpret the > sections as Reader monads? liftM2 (&&) :: (Monad m) => m Bool -> m Bool -> m Bool So for (liftM2 (&&) (< 0.5) (> -0.5)) to be well typed, we must have (< 0.5) :: m Bool for some Monad m (same for (> -0.5)). Now (< 0.5) :: Fractional a => a -> Bool, hence m must be ((->) a) for some a in the Fractional class. Reader r a is just (r -> a) wrapped in a newtype, so (r -> a) 'is' the reader monad. To use it, we must import Control.Monad for liftM2 and some module which brings the instance Monad ((->) r) into scope. That could be Control.Monad.Instances or, appropriately, Control.Monad.Reader. > > > That's a job for the reader monad. > > > > > > Lambda Fu, form 53 - silent reader of truth > > > > import Control.Monad > > import Control.Monad.Reader > > > > filter (liftM2 (&&) (< 0.5) (> -0.5)) xs > > > > > > > > Regards, > > apfelmus > From agarwal1975 at gmail.com Mon Oct 19 12:22:48 2009 From: agarwal1975 at gmail.com (Ashish Agarwal) Date: Mon Oct 19 12:00:05 2009 Subject: [Haskell-beginners] Re: parsing upto n items with parsec In-Reply-To: <4AD98B3C.7080804@dfki.de> References: <4AD98B3C.7080804@dfki.de> Message-ID: Thanks. So I tried upTo n p = liftM2 (:) p (upTo (n-1) p) which doesn't quite work because the recursion does not have a base case. The following gets me closer: upTo n p = if n <= 0 then return [] else liftM2 (:) p (upTo (n-1) p) > parse (upTo 3 digit) "" "123ab" Right "123" However: > parse (upTo 4 digit) "" "123ab" Left (line 1, column 4): unexpected "a" expecting digit The semantics of (upTo n p) should be to parse at most n tokens, but if less than n tokens are available that should still be a successful parse. And the next token should be the first one upTo failed on. I attempted to use the "try" parser in various locations but that doesn't seem to help, or maybe I'm using it incorrectly. On Sat, Oct 17, 2009 at 5:15 AM, Christian Maeder wrote: > Ashish Agarwal schrieb: > > Hi. I'm just learning Parsec and Haskell. It is a great library, and I > > see how "many" lets you parse 0 or more items, "many1" parses 1 or more > > items, and "count" parses exactly n items. However, there is no > > combinator that parses between m and n items. What would be the best > > implementation for this? > > I would write an "upTo" parser with the same type as "count" that parses > not exactly but at most n items. Your desired parser is than the > concatenated results of "count m p" and "upTo (n - m) p" (achieved by > "liftM2 (++)"). > > For "upTo" a recursive definition seems best (other may come up with > tricky combinator application.) "upTo 0 p" (or something less than 0) > returns "[]" and "upTo n p" is an "option [] ..." parser of one "p" > result followed by the "upTo (n - 1) p" result: > > "option [] (liftM2 (:) p (upTo (n - 1) p))" > > HTH Christian > > Another possibility is to use "many" and check if the resulting list has > the desired length (if not fail), but that may consume too many tokens > that subsequent parsers are supposed to consume. > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20091019/305d5640/attachment.html From aditya.siram at gmail.com Mon Oct 19 12:42:01 2009 From: aditya.siram at gmail.com (aditya siram) Date: Mon Oct 19 12:18:57 2009 Subject: [Haskell-beginners] Re: \x -> x < 0.5 && x > -0.5 In-Reply-To: <301488c50910190824w43dde17cta55ac778f7a4661f@mail.gmail.com> References: <301488c50910190824w43dde17cta55ac778f7a4661f@mail.gmail.com> Message-ID: <594f78210910190942n3f068682l77f1e3b5437f496e@mail.gmail.com> This one had me puzzled too - so did a traced through the program below. Please make sure your viewing window is at least this wide: <----------------------------------------------------------------> It was more helpful to think of this as using the ((->) r) instance of Monad. It is defined in ./libraries/base/Control/Monad/Instances.hs in your GHC source as: >instance Monad ((->) r) where > return = const > f >>= k = \ r -> k (f r) r And const just returns its first argument like: const 1 3 => 1 const "hello" "world" => "hello" And liftM2 is defined in ./libraries/base/Control/Monad.hs as : >liftM2 :: (Monad m) => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r >liftM2 f m1 m2 = do { x1 <- m1; x2 <- m2; return (f x1 x2) } So a trace of the program goes like this: (liftM2 (&&) (< 0.5) (> -0.5)) => do {x1 <- (< 0.5); x2 <- (> -0.5); return ((&&) x1 x2)} => (< 0.5) >>= \x1 (> -0.5) >>= \x2 return ((&&) x1 x2) => \r ->(\x1 -> (> -0.5) >>= \x2 return ((&&) x1 x2)) ((< 0.5) r) r => \r -> (return (> -0.5) >>= \x2 return ((&&) ((< 0.5) r) x2)) r => \r -> (\r' -> (\x2 -> return ((&&) (const (< 0.5) r) x2)) ((> -0.5) r') r') r => \r -> (\r' -> (return ((&&) ((< 0.5) r) ((> -0.5) r'))) r') r => \r -> (\r' -> (const ((&&) ((< 0.5) r) ((> -0.5) r'))) r') r => \r -> (\r' -> ((&&) ((< 0.5) r) ((> -0.5) r'))) r => \r -> (\r' -> ((r < 0.5) && (r' > -0.5))) r hope this helps, -deech On Mon, Oct 19, 2009 at 10:24 AM, Jordan Cooper wrote: > Whoa... how on earth does this work? How does it interpret the > sections as Reader monads? > > > That's a job for the reader monad. > > > > > > Lambda Fu, form 53 - silent reader of truth > > > > import Control.Monad > > import Control.Monad.Reader > > > > filter (liftM2 (&&) (< 0.5) (> -0.5)) xs > > > > > > > > Regards, > > apfelmus > _______________________________________________ > 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/20091019/578ea6af/attachment-0001.html From aditya.siram at gmail.com Mon Oct 19 12:47:32 2009 From: aditya.siram at gmail.com (aditya siram) Date: Mon Oct 19 12:24:29 2009 Subject: [Haskell-beginners] Re: \x -> x < 0.5 && x > -0.5 In-Reply-To: <594f78210910190942n3f068682l77f1e3b5437f496e@mail.gmail.com> References: <301488c50910190824w43dde17cta55ac778f7a4661f@mail.gmail.com> <594f78210910190942n3f068682l77f1e3b5437f496e@mail.gmail.com> Message-ID: <594f78210910190947l54e8ac10g53920b932a539949@mail.gmail.com> There was a mistake in the trace, please ignore the previous one and look at this. Again your viewing window should be this wide: <-----------------------------------------------------------------> (liftM2 (&&) (< 0.5) (> -0.5)) => do {x1 <- (< 0.5); x2 <- (> -0.5); return ((&&) x1 x2)} => (< 0.5) >>= \x1 (> -0.5) >>= \x2 return ((&&) x1 x2) => \r ->(\x1 -> (> -0.5) >>= \x2 return ((&&) x1 x2)) ((< 0.5) r) r => \r -> ((> -0.5) >>= \x2 return ((&&) ((< 0.5) r) x2)) r => \r -> (\r' -> (\x2 -> return ((&&) (const (< 0.5) r) x2)) ((> -0.5) r') r') r => \r -> (\r' -> (return ((&&) ((< 0.5) r) ((> -0.5) r'))) r') r => \r -> (\r' -> (const ((&&) ((< 0.5) r) ((> -0.5) r'))) r') r => \r -> (\r' -> ((&&) ((< 0.5) r) ((> -0.5) r'))) r => \r -> (\r' -> ((r < 0.5) && (r' > -0.5))) r On Mon, Oct 19, 2009 at 11:42 AM, aditya siram wrote: > This one had me puzzled too - so did a traced through the program below. > Please make sure your viewing window is at least this wide: > <----------------------------------------------------------------> > > It was more helpful to think of this as using the ((->) r) instance of > Monad. It is defined in ./libraries/base/Control/Monad/Instances.hs in your > GHC source as: > >instance Monad ((->) r) where > > return = const > > f >>= k = \ r -> k (f r) r > > And const just returns its first argument like: > const 1 3 => 1 > const "hello" "world" => "hello" > > And liftM2 is defined in ./libraries/base/Control/Monad.hs as : > >liftM2 :: (Monad m) => (a1 -> a2 -> r) -> m a1 -> m a2 -> m r > >liftM2 f m1 m2 = do { x1 <- m1; x2 <- m2; return (f x1 x2) } > > So a trace of the program goes like this: > > (liftM2 (&&) (< 0.5) (> -0.5)) > => do {x1 <- (< 0.5); > x2 <- (> -0.5); > return ((&&) x1 x2)} > > => (< 0.5) >>= \x1 > (> -0.5) >>= \x2 > return ((&&) x1 x2) > > => \r ->(\x1 -> > (> -0.5) >>= \x2 > return ((&&) x1 x2)) > ((< 0.5) r) > r > > => \r -> (return (> -0.5) >>= \x2 > return ((&&) ((< 0.5) r) x2)) > r > > => \r -> (\r' -> (\x2 -> > return ((&&) (const (< 0.5) r) x2)) > ((> -0.5) r') > r') > r > => \r -> (\r' -> (return ((&&) ((< 0.5) r) ((> -0.5) r'))) r') r > => \r -> (\r' -> (const ((&&) ((< 0.5) r) ((> -0.5) r'))) r') r > => \r -> (\r' -> ((&&) ((< 0.5) r) ((> -0.5) r'))) r > => \r -> (\r' -> ((r < 0.5) && (r' > -0.5))) r > > hope this helps, > -deech > > > On Mon, Oct 19, 2009 at 10:24 AM, Jordan Cooper wrote: > >> Whoa... how on earth does this work? How does it interpret the >> sections as Reader monads? >> >> > That's a job for the reader monad. >> > >> > >> > Lambda Fu, form 53 - silent reader of truth >> > >> > import Control.Monad >> > import Control.Monad.Reader >> > >> > filter (liftM2 (&&) (< 0.5) (> -0.5)) xs >> > >> > >> > >> > Regards, >> > apfelmus >> _______________________________________________ >> 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/20091019/cd9fb9ca/attachment.html From chaddai.fouche at gmail.com Mon Oct 19 12:55:15 2009 From: chaddai.fouche at gmail.com (=?UTF-8?B?Q2hhZGRhw68gRm91Y2jDqQ==?=) Date: Mon Oct 19 12:32:12 2009 Subject: [Haskell-beginners] Re: parsing upto n items with parsec In-Reply-To: References: <4AD98B3C.7080804@dfki.de> Message-ID: On Mon, Oct 19, 2009 at 6:22 PM, Ashish Agarwal wrote: > The semantics of (upTo n p) should be to parse at most n tokens, but if less > than n tokens are available that should still be a successful parse. And the > next token should be the first one upTo failed on. > I attempted to use the "try" parser in various locations but that doesn't > seem to help, or maybe I'm using it incorrectly. First, you should probably use pattern matching rather than if for your base case/recursive case distinction, it's clearer (at least most Haskellers seems to think it is) : > upTo 0 p = return [] second, option was conceived for these case where you want to try a parser and returns something if it fail : > upTo n p = option [] $ liftM2 (:) p (upTo (n-1) p) Even then, be careful of putting a try before any multi-token parser that could fail harmlessly during the upTo. -- Jeda? From byorgey at seas.upenn.edu Mon Oct 19 13:21:07 2009 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Mon Oct 19 12:58:03 2009 Subject: [Haskell-beginners] Re: \x -> x < 0.5 && x > -0.5 In-Reply-To: <4ADC3A57.4020203@dfki.de> References: <4ADC280D.1070109@alumni.caltech.edu> <4ADC3A57.4020203@dfki.de> Message-ID: <20091019172107.GA28146@seas.upenn.edu> On Mon, Oct 19, 2009 at 12:07:19PM +0200, Christian Maeder wrote: > Michael Mossey schrieb: > > Is there a nifty way to write > > > > filter (\x -> x < 0.5 && x > -0.5) xs > > > > without explicitly using x? > > Hoogle did not find a function of type: > > (b -> b -> b) -> (a -> b) -> (a -> b) -> a -> b > > or (b -> c -> d) -> (a -> b) -> (a -> c) -> a -> d Apparently Hoogle does not know to try unifying m b with a -> b. As others have pointed out, a function of this type (actually, a more general type) does exist, namely, liftM2. -Brent From agarwal1975 at gmail.com Mon Oct 19 14:08:39 2009 From: agarwal1975 at gmail.com (Ashish Agarwal) Date: Mon Oct 19 13:45:56 2009 Subject: [Haskell-beginners] Re: parsing upto n items with parsec In-Reply-To: References: <4AD98B3C.7080804@dfki.de> Message-ID: Right. I took "option" out for some reason. The following definitions have the desired behavior. I also added a base case to fromTo because otherwise the call to count would consume m tokens even if n-m < 0, which is probably not what we'd expect. upTo :: Int -> GenParser tok st a -> GenParser tok st [a] upTo n p | n <= 0 = return [] | otherwise = option [] $ liftM2 (:) p (upTo (n-1) p) fromTo :: Int -> Int -> GenParser tok st a -> GenParser tok st [a] fromTo m n p | n-m < 0 = return [] | otherwise = liftM2 (++) (count m p) (upTo (n-m) p) Thanks! On Mon, Oct 19, 2009 at 12:55 PM, Chadda? Fouch? wrote: > On Mon, Oct 19, 2009 at 6:22 PM, Ashish Agarwal > wrote: > > The semantics of (upTo n p) should be to parse at most n tokens, but if > less > > than n tokens are available that should still be a successful parse. And > the > > next token should be the first one upTo failed on. > > I attempted to use the "try" parser in various locations but that doesn't > > seem to help, or maybe I'm using it incorrectly. > > > First, you should probably use pattern matching rather than if for > your base case/recursive case distinction, it's clearer (at least most > Haskellers seems to think it is) : > > > upTo 0 p = return [] > > second, option was conceived for these case where you want to try a > parser and returns something if it fail : > > > upTo n p = option [] $ liftM2 (:) p (upTo (n-1) p) > > Even then, be careful of putting a try before any multi-token parser > that could fail harmlessly during the upTo. > > -- > Jeda? > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20091019/86f1a98c/attachment.html From mpm at alumni.caltech.edu Mon Oct 19 15:09:02 2009 From: mpm at alumni.caltech.edu (Michael Mossey) Date: Mon Oct 19 14:46:11 2009 Subject: [Haskell-beginners] Re: \x -> x < 0.5 && x > -0.5 In-Reply-To: References: <4ADC280D.1070109@alumni.caltech.edu> Message-ID: <4ADCB94E.4050201@alumni.caltech.edu> Heinrich Apfelmus wrote: > Michael Mossey wrote: >> Is there a nifty way to write >> >> filter (\x -> x < 0.5 && x > -0.5) xs >> >> without explicitly using x? >> >> Maybe arrows? I have a vague understanding that arrows can "send" an >> argument to more than one computation. > > That's a job for the reader monad. > > > Lambda Fu, form 53 - silent reader of truth > > import Control.Monad > import Control.Monad.Reader > > filter (liftM2 (&&) (< 0.5) (> -0.5)) xs > > Cool. I realized there was a way to think about this. I haven't used the reader monad in my own projects, but I recall it's one way to pass the same value into several functions: headTail = do h <- head t <- tail return (h,t) headTail "foo" = ('f',"oo") Note also there's no need for runReader or evalReader (at least not that I'm aware of) because unlike other monads, the reader monad is itself a function that takes the state to be read. This could be generalized to headTail2 g = do h <- head t <- tail return $ g h t headTail2 (,) "foo" = ('f',"oo") But this form: do { x <- m1; y <- m2; return $ g x y} is exactly the definition of liftM2. Specifically, liftM2 g m1 m2. From byorgey at seas.upenn.edu Mon Oct 19 16:18:32 2009 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Mon Oct 19 15:55:27 2009 Subject: [Haskell-beginners] Re: \x -> x < 0.5 && x > -0.5 In-Reply-To: <4ADCB94E.4050201@alumni.caltech.edu> References: <4ADC280D.1070109@alumni.caltech.edu> <4ADCB94E.4050201@alumni.caltech.edu> Message-ID: <20091019201832.GA5538@seas.upenn.edu> On Mon, Oct 19, 2009 at 12:09:02PM -0700, Michael Mossey wrote: > > Note also there's no need for runReader or evalReader (at least not that > I'm aware of) because unlike other monads, the reader monad is itself a > function that takes the state to be read. Note that little-r 'reader' is just an informal name for the ((->) e) monad, which is what your code was using. Control.Monad.Reader also provides the big-R 'Reader' type, which is just a newtype wrapper around a little-r reader, and does indeed have a 'runReader' method (which just removes the newtype constructor). That is, newtype Reader r a = Reader { runReader :: r -> a } C.M.Reader also provides ReaderT, a monad transformer version of Reader. -Brent From wrwills at gmail.com Mon Oct 19 18:17:05 2009 From: wrwills at gmail.com (Robert Wills) Date: Mon Oct 19 17:54:02 2009 Subject: [Haskell-beginners] new locale? In-Reply-To: <20091017184522.GA19147@seas.upenn.edu> References: <4AD55732.6010106@gmail.com> <20091017184522.GA19147@seas.upenn.edu> Message-ID: <4ADCE561.1050301@gmail.com> Hi, Sorry for not replying earlier. I didn't see this until Friday and didn't get much time on my computer this weekend. I asked this question because I was hoping to find System.Locale.defaultTimeLocale doesn't seem to be returning a time locale that matches what's on my system. It returns dateFmt = "%m/%d/%y" while my machine is set to EN_GB and so presumably it should return %d/%m/%y. In Python on my machine I get: >>> import locale >>> locale.getdefaultlocale() ('en_GB', 'UTF8') So anyway, I was wondering whether there was a new locale library that fixed this problem. -Rob Brent Yorgey wrote: > On Wed, Oct 14, 2009 at 05:44:34AM +0100, Robert Wills wrote: > >> Hello, >> >> http://hackage.haskell.org/package/old-locale-1.0.0.1 >> >> says "This package provides the old locale library. For new code, the new >> locale library is recommended." >> >> Does anyone know where I would find the new library? >> > > What locale functions are you trying to use? As far as I can tell, > there actually is no "new locale library" and functions for working > with the locale have been spread across various other libraries as > appropriate. > > -Brent > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > From raven at phoenyx.net Mon Oct 19 22:19:41 2009 From: raven at phoenyx.net (Carl Cravens) Date: Mon Oct 19 21:55:26 2009 Subject: [Haskell-beginners] \x -> x < 0.5 && x > -0.5 In-Reply-To: <4ADC280D.1070109@alumni.caltech.edu> References: <4ADC280D.1070109@alumni.caltech.edu> Message-ID: <4ADD1E3D.5090909@phoenyx.net> Michael Mossey wrote: > Is there a nifty way to write > > filter (\x -> x < 0.5 && x > -0.5) xs > > without explicitly using x? I'm pretty new to Haskell... are you looking for a *better* way to write this, or is this an exercise in exploring alternatives for the sake of understanding? I'm not seeing any of the proposed alternatives as being as clear as the lambda function, and I'd be surprised (in my ignorance) if any of them were more efficient. -- Carl D Cravens (raven@phoenyx.net) Bad Command! Bad, Bad Command! Sit! Staaaaay... From mpm at alumni.caltech.edu Mon Oct 19 22:48:50 2009 From: mpm at alumni.caltech.edu (Michael Mossey) Date: Mon Oct 19 22:26:00 2009 Subject: [Haskell-beginners] \x -> x < 0.5 && x > -0.5 In-Reply-To: <4ADD1E3D.5090909@phoenyx.net> References: <4ADC280D.1070109@alumni.caltech.edu> <4ADD1E3D.5090909@phoenyx.net> Message-ID: <4ADD2512.8090603@alumni.caltech.edu> Carl Cravens wrote: > Michael Mossey wrote: >> Is there a nifty way to write >> >> filter (\x -> x < 0.5 && x > -0.5) xs >> >> without explicitly using x? > > I'm pretty new to Haskell... are you looking for a *better* way to write > this, or is this an exercise in exploring alternatives for the sake of > understanding? > Hi Carl, Either one. Let me chime in with some observations from a few months of studying Haskell. (Brent and Apfelmus can probably elaborate on this.) Eliminating variables and working with function combinations has benefits. The one suggestion I've seen here that seems to be right on the money is liftM2 (&&) (< 0.5) (> -0.5) Although that might seem less clear to a beginner, it is actually _more_ clear than the lambda function in some ways. It's easier to work with proof at a more abstract level like this, and strange as it may seem, what I seem to observe in expert users of Haskell is that their brains will pick up what this is doing faster than the lambda function. Or maybe this example is too small to be meaningful, but this kind of abstraction is the direction I want to move in, for there are benefits waiting for me when I arrive. The Parsec library is an example of how concise and elegant code can get when you choose your abstractions carefully. -Mike From ml at isaac.cedarswampstudios.org Mon Oct 19 23:06:43 2009 From: ml at isaac.cedarswampstudios.org (Isaac Dupree) Date: Mon Oct 19 22:43:56 2009 Subject: [Haskell-beginners] \x -> x < 0.5 && x > -0.5 In-Reply-To: <4ADD2512.8090603@alumni.caltech.edu> References: <4ADC280D.1070109@alumni.caltech.edu> <4ADD1E3D.5090909@phoenyx.net> <4ADD2512.8090603@alumni.caltech.edu> Message-ID: <4ADD2943.1080000@isaac.cedarswampstudios.org> Michael Mossey wrote: > Eliminating variables and working with function combinations has > benefits. The one suggestion I've seen here that seems to be right on > the money is > > liftM2 (&&) (< 0.5) (> -0.5) yep, I might not write it myself, but it's definitely the only one straightforward enough to perhaps justify itself. now for a completely different suggestion; you write > filter (\x -> x < 0.5 && x > -0.5) xs but as a reader I would find it clearer to switch the order to numerical: filter (\x -> x > -0.5 && x < 0.5) xs (in this particular instance, we can observe numerical properties and even change to (\x -> abs x < 0.5) if we so desire. Which can be written point-free as ((< 0.5) . abs), if you want to.) -Isaac From daniel.is.fischer at web.de Mon Oct 19 23:28:45 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Mon Oct 19 23:10:15 2009 Subject: [Haskell-beginners] \x -> x < 0.5 && x > -0.5 In-Reply-To: <4ADD2512.8090603@alumni.caltech.edu> References: <4ADC280D.1070109@alumni.caltech.edu> <4ADD1E3D.5090909@phoenyx.net> <4ADD2512.8090603@alumni.caltech.edu> Message-ID: <200910200528.45372.daniel.is.fischer@web.de> Am Dienstag 20 Oktober 2009 04:48:50 schrieb Michael Mossey: > Carl Cravens wrote: > > Michael Mossey wrote: > >> Is there a nifty way to write > >> > >> filter (\x -> x < 0.5 && x > -0.5) xs > >> > >> without explicitly using x? > > > > I'm pretty new to Haskell... are you looking for a *better* way to write > > this, or is this an exercise in exploring alternatives for the sake of > > understanding? > > Hi Carl, > > Either one. > > Let me chime in with some observations from a few months of studying > Haskell. (Brent and Apfelmus can probably elaborate on this.) > > Eliminating variables and working with function combinations has benefits. But it's not unconditionally a good thing. If you exaggerate it, it's pure obfuscation. Nevertheless, playing around with eliminating variables and using combinators even beyond the border of obfuscation is a good exercise. You gain understanding and a feeling of when it's better to stop by that. > The one suggestion I've seen here that seems to be right on the money is > > liftM2 (&&) (< 0.5) (> -0.5) May I offer (&&) <$> (< 0.5) <*> (> -0.5) ? It works on Applicative Functors (doesn't need the full force of Monads). > > Although that might seem less clear to a beginner, it is actually _more_ > clear than the lambda function in some ways. It's easier to work with proof > at a more abstract level like this, and strange as it may seem, what I seem > to observe in expert users of Haskell is that their brains will pick up > what this is doing faster than the lambda function. In this small example, both are immediately clear, you need more complicated lambda expressions to get a measurable difference :) From mpm at alumni.caltech.edu Tue Oct 20 00:33:37 2009 From: mpm at alumni.caltech.edu (Michael Mossey) Date: Tue Oct 20 00:10:49 2009 Subject: [Haskell-beginners] Stack overflow, but hard to understand Message-ID: <4ADD3DA1.2080903@alumni.caltech.edu> Okay, beginner encountering first major bug. I'm getting a stack overflow on a program that uses a lot of laziness to construct and modify lists---but the overflow happens even if I take just one element from the final list! The overflow seems to have started when I added the functions 'gauss' and 'gaussList' below, but I am at a loss to know why---these functions work perfectly in test cases and can even generate a long list. The following program is distilled (with a lot of work!) down to the essence of what triggers the problem. import Control.Monad import Control.Monad.Random import Data.List -- Here I put a moderate-sized list of filenames which just generic -- text or code. The longer the list, -- the more likely to overflow stack. Which is curious, because if IO were -- lazy here, it shouldn't be reading more than the first character of the -- first file. Methinks a hint to the problem. fileList = ... data TextGroup = Single Char Float deriving (Show) textGroup_addDelay :: TextGroup -> Float -> TextGroup textGroup_addDelay (Single c del) del2 = Single c (del+del2) mkTextGroup :: Char -> Rand StdGen TextGroup mkTextGroup c = liftM (Single c) gauss -- Here's the kinda weird thing I'm doing with Rand. I want to -- use fromList as the first computation to randomly choose -- a second Rand computation. It turns out if you replace -- this funkiness with -- gauss = getRandomR (0,1) -- the whole thing works. gaussList :: Rand StdGen (Rand StdGen Float) gaussList = fromList [ (getRandomR( -1.0 ,-0.8 ), 2) , (getRandomR( -0.8 ,-0.6 ), 2) , (getRandomR( -0.6 ,-0.4 ), 4) , (getRandomR( -0.4 ,-0.2 ), 8) , (getRandomR( -0.2 , 0.0 ), 12) , (getRandomR( 0.0 , 0.2 ), 12) , (getRandomR( 0.2 , 0.4 ), 8) , (getRandomR( 0.4 , 0.6 ), 4) , (getRandomR( 0.6 , 0.8 ), 2) , (getRandomR( 0.8 , 1.0 ), 2) ] gauss :: Rand StdGen Float gauss = do m <- gaussList m main = do gen0 <- newStdGen bufs <- mapM readFile fileList let buf = concat bufs let tgs = evalRand (do orig <- mapM mkTextGroup buf addBreaks orig) gen0 writeFile "output.ank" (show $ take 1 tgs) From mpm at alumni.caltech.edu Tue Oct 20 04:52:31 2009 From: mpm at alumni.caltech.edu (Michael Mossey) Date: Tue Oct 20 04:29:27 2009 Subject: [Haskell-beginners] Is "step by step" the most natural style of thought? In-Reply-To: <200910200528.45372.daniel.is.fischer@web.de> References: <4ADC280D.1070109@alumni.caltech.edu> <4ADD1E3D.5090909@phoenyx.net> <4ADD2512.8090603@alumni.caltech.edu> <200910200528.45372.daniel.is.fischer@web.de> Message-ID: <4ADD7A4F.6000703@alumni.caltech.edu> Daniel Fischer wrote: > Am Dienstag 20 Oktober 2009 04:48:50 schrieb Michael Mossey: >> >> Eliminating variables and working with function combinations has benefits. > > But it's not unconditionally a good thing. If you exaggerate it, it's pure obfuscation. > Nevertheless, playing around with eliminating variables and using combinators > even beyond the border of obfuscation is a good exercise. > You gain understanding and a feeling of when it's better to stop by that. > I think I understand. Another way to put my point is that learning Haskell has trained my eye to notice when a particular variable is flopping around in six different places. I programmed for twenty years or more in imperative languages (in a business setting, not academia) and no one ever said, "Hey we've got too many repetitions of x! Let's see what the underlying abstraction REALLY is." >> The one suggestion I've seen here that seems to be right on the money is >> >> liftM2 (&&) (< 0.5) (> -0.5) > > May I offer > > (&&) <$> (< 0.5) <*> (> -0.5) > > ? It works on Applicative Functors (doesn't need the full force of Monads). > >> Although that might seem less clear to a beginner, it is actually _more_ >> clear than the lambda function in some ways. It's easier to work with proof >> at a more abstract level like this, and strange as it may seem, what I seem >> to observe in expert users of Haskell is that their brains will pick up >> what this is doing faster than the lambda function. > > In this small example, both are immediately clear, you need more complicated lambda > expressions to get a measurable difference :) Yeah, pretty small example, but based on what I've read in "Real World Haskell" and "Craft of Functional Programming," the authors find a lot of "strange-looking" (to the imperative programmer's eye) function combinators to be the most natural way of expressing the solution, and from time to time I find my thoughts aligning with them, and I realize how much less thought it takes. This raises the question: is "step by step" thinking the "most natural" way to think about a mathematical problem? My hunch is "no." There are multiple modes of thought, and no reason to eliminate cognitive models that involve imagery, imaginary things moving and sliding through space... or analogies to the more common physical world... or the list is endless. I like the way Haskell resonates with a larger set of thinking styles. Mike From v.dijk.bas at gmail.com Tue Oct 20 06:01:58 2009 From: v.dijk.bas at gmail.com (Bas van Dijk) Date: Tue Oct 20 05:38:53 2009 Subject: [Haskell-beginners] \x -> x < 0.5 && x > -0.5 In-Reply-To: <200910200528.45372.daniel.is.fischer@web.de> References: <4ADC280D.1070109@alumni.caltech.edu> <4ADD1E3D.5090909@phoenyx.net> <4ADD2512.8090603@alumni.caltech.edu> <200910200528.45372.daniel.is.fischer@web.de> Message-ID: On Tue, Oct 20, 2009 at 5:28 AM, Daniel Fischer wrote: > May I offer > > (&&) <$> (< 0.5) <*> (> -0.5) or: import Control.Applicative.Infix between l h = (> l) <^ (&&) ^> (< h) Bas You can find Control.Applicative.Infix in InfixApplicative: http://hackage.haskell.org/package/InfixApplicative From apfelmus at quantentunnel.de Tue Oct 20 06:08:19 2009 From: apfelmus at quantentunnel.de (Heinrich Apfelmus) Date: Tue Oct 20 05:45:58 2009 Subject: [Haskell-beginners] Re: \x -> x < 0.5 && x > -0.5 In-Reply-To: <20091019201832.GA5538@seas.upenn.edu> References: <4ADC280D.1070109@alumni.caltech.edu> <4ADCB94E.4050201@alumni.caltech.edu> <20091019201832.GA5538@seas.upenn.edu> Message-ID: Brent Yorgey wrote: > > Note that little-r 'reader' is just an informal name for the ((->) e) > monad, which is what your code was using. Control.Monad.Reader also > provides the big-R 'Reader' type, which is just a newtype wrapper > around a little-r reader, and does indeed have a 'runReader' method > (which just removes the newtype constructor). That is, > > newtype Reader r a = Reader { runReader :: r -> a } > > C.M.Reader also provides ReaderT, a monad transformer version of Reader. I thought Control.Monad.Reader also provides the needed instance Monad ((->) r) but this is actually provided by Control.Monad.Instances . Regards, apfelmus -- http://apfelmus.nfshost.com From stephen.tetley at gmail.com Tue Oct 20 11:29:43 2009 From: stephen.tetley at gmail.com (Stephen Tetley) Date: Tue Oct 20 11:06:36 2009 Subject: [Haskell-beginners] Re: \x -> x < 0.5 && x > -0.5 Message-ID: <5fdc56d70910200829h1d4b2094y93f6c8fc19c200fe@mail.gmail.com> Christian Maeder wrote: > Hoogle did not find a function of type: > > (b -> b -> b) -> (a -> b) -> (a -> b) -> a -> b > > or (b -> c -> d) -> (a -> b) -> (a -> c) -> a -> d > > But maybe such a function is worth being added to Data.Function. Hello Christian This is the big Phi or S' combinator. I would certainly second its addition to Data.Function. sprime p q r s = p (q s) (r s) Best wishes Stephen From byorgey at seas.upenn.edu Tue Oct 20 11:37:15 2009 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Tue Oct 20 11:14:10 2009 Subject: [Haskell-beginners] Re: \x -> x < 0.5 && x > -0.5 In-Reply-To: <5fdc56d70910200829h1d4b2094y93f6c8fc19c200fe@mail.gmail.com> References: <5fdc56d70910200829h1d4b2094y93f6c8fc19c200fe@mail.gmail.com> Message-ID: <20091020153715.GA2269@seas.upenn.edu> On Tue, Oct 20, 2009 at 04:29:43PM +0100, Stephen Tetley wrote: > Christian Maeder wrote: > > > Hoogle did not find a function of type: > > > > (b -> b -> b) -> (a -> b) -> (a -> b) -> a -> b > > > > or (b -> c -> d) -> (a -> b) -> (a -> c) -> a -> d > > > > But maybe such a function is worth being added to Data.Function. > > Hello Christian > > This is the big Phi or S' combinator. I would certainly second its > addition to Data.Function. > > sprime p q r s = p (q s) (r s) But this function already exists in the standard libraries: it is called liftM2/liftA2, specialized to the ((->) e) monad/applicative instance. This is what the rest of the thread has been talking about. -Brent From Christian.Maeder at dfki.de Tue Oct 20 12:34:23 2009 From: Christian.Maeder at dfki.de (Christian Maeder) Date: Tue Oct 20 12:11:18 2009 Subject: [Haskell-beginners] Re: \x -> x < 0.5 && x > -0.5 In-Reply-To: <5fdc56d70910200829h1d4b2094y93f6c8fc19c200fe@mail.gmail.com> References: <5fdc56d70910200829h1d4b2094y93f6c8fc19c200fe@mail.gmail.com> Message-ID: <4ADDE68F.9080304@dfki.de> Stephen Tetley schrieb: > Christian Maeder wrote: > >> Hoogle did not find a function of type: >> >> (b -> b -> b) -> (a -> b) -> (a -> b) -> a -> b >> >> or (b -> c -> d) -> (a -> b) -> (a -> c) -> a -> d >> >> But maybe such a function is worth being added to Data.Function. I did not find liftM2 (or liftA2) although I "knew" liftM2 and instance Functor/Monad ((->) a). > Hello Christian > > This is the big Phi or S' combinator. I would certainly second its > addition to Data.Function. > > sprime p q r s = p (q s) (r s) At least for documentation purposes (other than this thread) such an explicit definition makes sense to be put into a source. Be it just to show the (non-)obvious. Cheers Christian Btw. for the actual problem I suggest now: ((< 0.5) . abs) From benasbn at gmail.com Tue Oct 20 12:39:34 2009 From: benasbn at gmail.com (Bernardas Jankauskas) Date: Tue Oct 20 12:16:31 2009 Subject: [Haskell-beginners] haskell on snow leopard Message-ID: Hello, For two past weeks I have been trying to get Haskell working on my macbook pro with Snow Leopard, but unfortunately I was unable to get it. There are some solutions on the internet, but these are mostly useful when you already have ghci and haskell platform installed on your computer, while I can't even get it installed. I have Xcode 3.1 installed, but it seems that it is not the case. Maybe you could recommend any solution to this problem? Sincerely, Bernardas Jankauskas P.S.: I have attached two screenshots of what occurs for me when I am trying to install haskell platform and ghci. From ezyang at MIT.EDU Tue Oct 20 12:53:12 2009 From: ezyang at MIT.EDU (Edward Z. Yang) Date: Tue Oct 20 12:46:05 2009 Subject: [Haskell-beginners] Re: \x -> x < 0.5 && x > -0.5 In-Reply-To: <4ADDE68F.9080304@dfki.de> References: <5fdc56d70910200829h1d4b2094y93f6c8fc19c200fe@mail.gmail.com> <4ADDE68F.9080304@dfki.de> Message-ID: <1256057556-sup-7188@ezyang> Excerpts from Christian Maeder's message of Tue Oct 20 12:34:23 -0400 2009: > Stephen Tetley schrieb: > > Christian Maeder wrote: > > > >> Hoogle did not find a function of type: > >> > >> (b -> b -> b) -> (a -> b) -> (a -> b) -> a -> b > >> > >> or (b -> c -> d) -> (a -> b) -> (a -> c) -> a -> d > >> > >> But maybe such a function is worth being added to Data.Function. > > I did not find liftM2 (or liftA2) although I "knew" liftM2 and instance > Functor/Monad ((->) a). Perhaps a useful feature to add to Hoogle would be the ability to print both the generic type instance, and then a "specialized" version based off of the query that was made. Cheers, Edward From luca_ciciriello at hotmail.com Tue Oct 20 13:20:42 2009 From: luca_ciciriello at hotmail.com (Luca Ciciriello) Date: Tue Oct 20 12:57:39 2009 Subject: [Haskell-beginners] haskell on snow leopard In-Reply-To: References: Message-ID: I belive I undertood that you have tried to install the Haskell platform. Had you tried to install just GHC using the Mac installer that you can find in: http://haskell.org/ghc/download_ghc_6_10_4.html ? Luca. P.S. I Think attachment are filtered in this mailing list. On Oct 20, 2009, at 6:39 PM, Bernardas Jankauskas wrote: > Hello, > > For two past weeks I have been trying to get Haskell working on my > macbook pro with Snow Leopard, but unfortunately I was unable to get > it. > There are some solutions on the internet, but these are mostly > useful when you already have ghci and haskell platform installed on > your computer, while I can't even get it installed. I have Xcode 3.1 > installed, but it seems that it is not the case. > > Maybe you could recommend any solution to this problem? > > Sincerely, > Bernardas Jankauskas > > P.S.: I have attached two screenshots of what occurs for me when I > am trying to install haskell platform and ghci. > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > From korpios at korpios.com Tue Oct 20 14:18:49 2009 From: korpios at korpios.com (Tom Tobin) Date: Tue Oct 20 13:55:42 2009 Subject: [Haskell-beginners] haskell on snow leopard In-Reply-To: References: Message-ID: On Tue, Oct 20, 2009 at 12:20 PM, Luca Ciciriello wrote: > I belive I undertood that you have tried to install the Haskell platform. > Had you tried to install just ?GHC using the Mac installer that you can find > in: http://haskell.org/ghc/download_ghc_6_10_4.html ? That's what finally worked for me, FWIW. I was also able to install the 6.12.1 RC on Snow Leopard. From mpm at alumni.caltech.edu Tue Oct 20 15:32:57 2009 From: mpm at alumni.caltech.edu (Michael Mossey) Date: Tue Oct 20 15:09:57 2009 Subject: [Haskell-beginners] SOLVED - Stack overflow, but hard to understand In-Reply-To: <4ADD3DA1.2080903@alumni.caltech.edu> References: <4ADD3DA1.2080903@alumni.caltech.edu> Message-ID: <4ADE1069.90504@alumni.caltech.edu> Okay, I figured this out. mapM is not lazy, at least not for a monad that has state and under the circumstance you demand the state out the other side. I may rewrite the program. Or I may consider the ISS principle. ("Increase the stack, stupid.") -Mike From magnus at therning.org Wed Oct 21 01:38:01 2009 From: magnus at therning.org (Magnus Therning) Date: Wed Oct 21 01:14:56 2009 Subject: [Haskell-beginners] SOLVED - Stack overflow, but hard to understand In-Reply-To: <4ADE1069.90504@alumni.caltech.edu> References: <4ADD3DA1.2080903@alumni.caltech.edu> <4ADE1069.90504@alumni.caltech.edu> Message-ID: <4ADE9E39.3090000@therning.org> On 20/10/09 20:32, Michael Mossey wrote: > Okay, I figured this out. mapM is not lazy, at least not for a monad that > has state and under the circumstance you demand the state out the other > side. If I understand what you are saying then this behaviour isn't unexpected. If you have a monad with state, and you ask for the final state, then it's likely that everything happening in that particular monad will has to be evaluated. > I may rewrite the program. Or I may consider the ISS principle. > ("Increase the stack, stupid.") You may also be able to improve the situation by adding a bit of strictness. In some cases the thunks resulting from laziness can take up a lot of space. Forcing evaluation, at well thought out locations in your program, may both speed things up and reduce memory/stack usage. /M -- Magnus Therning (OpenPGP: 0xAB4DFBA4) magnus?therning?org Jabber: magnus?therning?org http://therning.org/magnus identi.ca|twitter: magthe From mpm at alumni.caltech.edu Wed Oct 21 04:44:38 2009 From: mpm at alumni.caltech.edu (Michael Mossey) Date: Wed Oct 21 04:21:34 2009 Subject: [Haskell-beginners] SOLVED - Stack overflow, but hard to understand In-Reply-To: <4ADE9E39.3090000@therning.org> References: <4ADD3DA1.2080903@alumni.caltech.edu> <4ADE1069.90504@alumni.caltech.edu> <4ADE9E39.3090000@therning.org> Message-ID: <4ADEC9F6.3050204@alumni.caltech.edu> Magnus Therning wrote: > On 20/10/09 20:32, Michael Mossey wrote: >> Okay, I figured this out. mapM is not lazy, at least not for a monad that >> has state and under the circumstance you demand the state out the other >> side. > > If I understand what you are saying then this behaviour isn't > unexpected. If > you have a monad with state, and you ask for the final state, then it's > likely > that everything happening in that particular monad will has to be > evaluated. Hi Magnus, Yes, that's what I was trying to imply. I realized it is mathematically impossible for mapM to be lazy for a monad with state. mapM doesn't seem to be lazy anywhere. For example, this program main = do buffers <- forM ["file1.txt","file2.txt","file3.txt"] readFile print . take 1 . concat $ buffers will, according to my experiments with the profiler, read all three files. It's possible to imagine lazy behavior here, but no doubt there is a technical reason against it. > >> I may rewrite the program. Or I may consider the ISS principle. >> ("Increase the stack, stupid.") > > You may also be able to improve the situation by adding a bit of > strictness. > In some cases the thunks resulting from laziness can take up a lot of > space. > Forcing evaluation, at well thought out locations in your program, may both > speed things up and reduce memory/stack usage. You are probably right... I am having trouble spanning the gap between "my current understanding" and "well thought-out locations." Real World Haskell has a chapter on this, so I may look there. I was able to get the program to run by removing all places that I had created a chain of Rand computations that forced evaluation of the last one. I replaced these with computations that did not require evaluation of the last one. However, my program was still forced to read more files than it really needed in the end, as the above snippet demonstrates. Thanks, Mike From magnus at therning.org Wed Oct 21 11:49:23 2009 From: magnus at therning.org (Magnus Therning) Date: Wed Oct 21 11:26:17 2009 Subject: [Haskell-beginners] SOLVED - Stack overflow, but hard to understand In-Reply-To: <4ADEC9F6.3050204@alumni.caltech.edu> References: <4ADD3DA1.2080903@alumni.caltech.edu> <4ADE1069.90504@alumni.caltech.edu> <4ADE9E39.3090000@therning.org> <4ADEC9F6.3050204@alumni.caltech.edu> Message-ID: On Wed, Oct 21, 2009 at 9:44 AM, Michael Mossey wrote: > > > Magnus Therning wrote: >> >> On 20/10/09 20:32, Michael Mossey wrote: >>> >>> Okay, I figured this out. mapM is not lazy, at least not for a monad that >>> has state and under the circumstance you demand the state out the other >>> side. >> >> If I understand what you are saying then this behaviour isn't unexpected. >> ?If >> you have a monad with state, and you ask for the final state, then it's >> likely >> that everything happening in that particular monad will has to be >> evaluated. > > Hi Magnus, > > Yes, that's what I was trying to imply. I realized it is mathematically > impossible for mapM to be lazy for a monad with state. > > mapM doesn't seem to be lazy anywhere. For example, this program > > main = do > ? buffers <- forM ["file1.txt","file2.txt","file3.txt"] readFile > ? print . take 1 . concat $ buffers > > will, according to my experiments with the profiler, read all three files. > It's possible to imagine lazy behavior here, but no doubt there is a > technical reason against it. Laziness in combination with IO is a difficult thing. Just search the archives to see the number of threads about it. >>> I may rewrite the program. Or I may consider the ISS principle. >>> ("Increase the stack, stupid.") >> >> You may also be able to improve the situation by adding a bit of >> strictness. >> In some cases the thunks resulting from laziness can take up a lot of >> space. >> Forcing evaluation, at well thought out locations in your program, may >> both >> speed things up and reduce memory/stack usage. > > You are probably right... I am having trouble spanning the gap between "my > current understanding" and "well thought-out locations." Real World Haskell > has a chapter on this, so I may look there. > > I was able to get the program to run by removing all places that I had > created a chain of Rand computations that forced evaluation of the last one. > I replaced these with computations that did not require evaluation of the > last one. > > However, my program was still forced to read more files than it really > needed in the end, as the above snippet demonstrates. Just out of curiosity, did you verify that all three files were completely *read*, as opposed to all three opened, but only the first line of the first file being read? /M -- Magnus Therning (OpenPGP: 0xAB4DFBA4) magnus?therning?org Jabber: magnus?therning?org http://therning.org/magnus identi.ca|twitter: magthe From mpm at alumni.caltech.edu Wed Oct 21 15:46:55 2009 From: mpm at alumni.caltech.edu (Michael Mossey) Date: Wed Oct 21 15:23:54 2009 Subject: [Haskell-beginners] SOLVED - Stack overflow, but hard to understand In-Reply-To: References: <4ADD3DA1.2080903@alumni.caltech.edu> <4ADE1069.90504@alumni.caltech.edu> <4ADE9E39.3090000@therning.org> <4ADEC9F6.3050204@alumni.caltech.edu> Message-ID: <4ADF652F.3040301@alumni.caltech.edu> Magnus Therning wrote: > > Just out of curiosity, did you verify that all three files were > completely *read*, as opposed to all three opened, but only the first > line of the first file being read? > > /M > Actually, no I didn't verify that, in this example. I was thinking about my other program in which I used mapM over the characters of the file onto a Rand monad. That one definitely read all the characters of the file---certain processing routines were called about 30,000 times (according to the profiler). This makes sense to me now, because I was asking for the state out the other side of the mapM. Thanks, Mike From chandni_ca at yahoo.com Thu Oct 22 15:11:07 2009 From: chandni_ca at yahoo.com (Chandni Navani) Date: Thu Oct 22 22:20:01 2009 Subject: [Haskell-beginners] Haskell Output Help Message-ID: <334456.88538.qm@web51608.mail.re2.yahoo.com> I have a list of lists which all contain strings.? [[String]].? I need to figure out how to print them so that after each individual string, there is a new line. If this is the initial list [["abc", "cde"] ["fgh", "ghi"]] [["abc" ? "cde"] ?["fgh", ? "ghi"]] Can anyone help me figure this out? Thanks. -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20091022/0f4da085/attachment.html From aditya.siram at gmail.com Thu Oct 22 22:53:57 2009 From: aditya.siram at gmail.com (aditya siram) Date: Thu Oct 22 22:30:43 2009 Subject: [Haskell-beginners] Haskell Output Help In-Reply-To: <334456.88538.qm@web51608.mail.re2.yahoo.com> References: <334456.88538.qm@web51608.mail.re2.yahoo.com> Message-ID: <594f78210910221953n2268a74cl51dee946fb610a78@mail.gmail.com> Is this what you need? print $ foldl (++) [] [["abc", "cde"], ["fgh", "ghi"]] -deech On Thu, Oct 22, 2009 at 2:11 PM, Chandni Navani wrote: > I have a list of lists which all contain strings. [[String]]. I need to > figure out how to print them so that after each individual string, there is > a new line. > > If this is the initial list [["abc", "cde"] ["fgh", "ghi"]] > [["abc" > "cde"] > ["fgh", > "ghi"]] > > Can anyone help me figure this out? Thanks. > > > > > > _______________________________________________ > 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/20091022/8b5bff23/attachment.html From aditya.siram at gmail.com Thu Oct 22 22:59:22 2009 From: aditya.siram at gmail.com (aditya siram) Date: Thu Oct 22 22:36:08 2009 Subject: [Haskell-beginners] Haskell Output Help In-Reply-To: <594f78210910221953n2268a74cl51dee946fb610a78@mail.gmail.com> References: <334456.88538.qm@web51608.mail.re2.yahoo.com> <594f78210910221953n2268a74cl51dee946fb610a78@mail.gmail.com> Message-ID: <594f78210910221959o7c98fbfo78eec42f915a7d60@mail.gmail.com> Sorry, forgot about the newline. How's this? mapM putStrLn $ concat [["abc", "cde"], ["fgh", "ghi"]] -deech On Thu, Oct 22, 2009 at 9:53 PM, aditya siram wrote: > Is this what you need? > > print $ foldl (++) [] [["abc", "cde"], ["fgh", "ghi"]] > > -deech > > On Thu, Oct 22, 2009 at 2:11 PM, Chandni Navani wrote: > >> I have a list of lists which all contain strings. [[String]]. I need to >> figure out how to print them so that after each individual string, there is >> a o line. >> >> If this is the initial list [["abc", "cde"] ["fgh", "ghi"]] >> [["abc" >> "cde"] >> ["fgh", >> "ghi"]] >> >> Can anyone help me figure this out? Thanks. >> >> >> >> >> >> _______________________________________________ >> 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/20091022/9bd05e2e/attachment.html From mklklk at hotmail.com Fri Oct 23 05:59:51 2009 From: mklklk at hotmail.com (Kui Ma) Date: Fri Oct 23 05:36:36 2009 Subject: [Haskell-beginners] how to set command args in ghci In-Reply-To: <334456.88538.qm@web51608.mail.re2.yahoo.com> References: <334456.88538.qm@web51608.mail.re2.yahoo.com> Message-ID: Is it possible to set the Command Arguments when running the main function in ghci? Thanks, Kui _________________________________________________________________ Windows Live: Friends get your Flickr, Yelp, and Digg updates when they e-mail you. http://www.microsoft.com/middleeast/windows/windowslive/see-it-in-action/social-network-basics.aspx?ocid=PID23461::T:WLMTAGL:ON:WL:en-xm:SI_SB_3:092010 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20091023/dcbc1c63/attachment.html From nonowarn at gmail.com Fri Oct 23 06:40:46 2009 From: nonowarn at gmail.com (Yusaku Hashimoto) Date: Fri Oct 23 06:17:30 2009 Subject: [Haskell-beginners] how to set command args in ghci In-Reply-To: References: <334456.88538.qm@web51608.mail.re2.yahoo.com> Message-ID: :set args foo bar should work. FYI, type :? to ghci to see all commands. HTH -nwn 2009/10/23 Kui Ma : > Is it possible to set the Command Arguments when running the main function > in ghci? > > Thanks, > Kui > > > > ________________________________ > Windows Live: Friends get your Flickr, Yelp, and Digg updates when they > e-mail you. > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > From nonowarn at gmail.com Fri Oct 23 06:43:22 2009 From: nonowarn at gmail.com (Yusaku Hashimoto) Date: Fri Oct 23 06:20:07 2009 Subject: [Haskell-beginners] how to set command args in ghci In-Reply-To: References: <334456.88538.qm@web51608.mail.re2.yahoo.com> Message-ID: or :ma foo bar On Fri, Oct 23, 2009 at 7:40 PM, Yusaku Hashimoto wrote: > :set args foo bar > > should work. FYI, type :? to ghci to see all commands. > > HTH > > -nwn > > 2009/10/23 Kui Ma : >> Is it possible to set the Command Arguments when running the main function >> in ghci? >> >> Thanks, >> Kui >> >> >> >> ________________________________ >> Windows Live: Friends get your Flickr, Yelp, and Digg updates when they >> e-mail you. >> _______________________________________________ >> Beginners mailing list >> Beginners@haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > From haskell-beginners at foo.me.uk Fri Oct 23 07:51:52 2009 From: haskell-beginners at foo.me.uk (Philip Scott) Date: Fri Oct 23 07:28:42 2009 Subject: [Haskell-beginners] Caching evaluation of lazy lists Message-ID: <4AE198D8.8090402@foo.me.uk> Hi folks, Quick question for you. Suppose I have some lazy list, x, which is very expensive to evaluate, and I want to do something with it more than once; for example: f x = g (take 5 x) (take 6 x) Is Haskell clever enough to only evaluate the list as much as is needed, or will it evaluate it once to get the first five values, and again to get the first 6 (when really it only needed to get one more). Or is it really really clever, and works out it needs to take 6 to begin with, then just give you the first 5 for the first call? More tricky - is there a way to make that cache (if it exsists) persist in an interactive session? Eventually I am intending to write my own application with a little console at the bottom which does some ghci-esque magic, but for now lets say I am in ghci and I want to call f again with the same list. Is there a way to avoid it from forcing a recomputation of my lazy list? Any advice greatly appreciated, Philip From daniel.is.fischer at web.de Fri Oct 23 09:08:03 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Fri Oct 23 08:47:15 2009 Subject: [Haskell-beginners] Caching evaluation of lazy lists In-Reply-To: <4AE198D8.8090402@foo.me.uk> References: <4AE198D8.8090402@foo.me.uk> Message-ID: <200910231508.03578.daniel.is.fischer@web.de> Am Freitag 23 Oktober 2009 13:51:52 schrieb Philip Scott: > Hi folks, > > Quick question for you. Suppose I have some lazy list, x, which is very > expensive to evaluate, and I want to do something with it more than > once; for example: > > f x = g (take 5 x) (take 6 x) > > Is Haskell clever enough to only evaluate the list as much as is needed, In that example, when you call "f (makeExpensiveList arg1 arg2)", the list is bound to the name x in the body of f, so g's arguments are taken from the very same list, which is evaluated only once. Each of the list elements is evaluated when it's needed, so maybe there will be fewer than 6 elements evaluated. However, if you call "f (makeExpensiveList arg1 arg2)" again later in the programme with the same arg1 and arg2, it will be probably evaluated again (the optimiser would need to see that it's called with the same arguments again and that it's worth caching the result to avoid that). If your expensive list is not generated by a function but a constant (like the list of Fibonacci numbers), bind it to a name expensiveList = ... and it will be cached between calls to f (unless memory pressure forces it to be garbage collected between calls and then re-evaluated). If it's generated by a function, give names to the lists obtained from frequently used arguments: exList_1_2 = makeExpensiveList 1 2 exList_4_0 = makeExpensiveList 4 0 ... cachedExpensiveList 1 2 = exList_1_2 cachedExpensiveList 4 0 = exList_4_0 ... cachedExpensiveList a b = makeExpensiveList a b > or will it evaluate it once to get the first five values, and again to > get the first 6 (when really it only needed to get one more). Or is it > really really clever, and works out it needs to take 6 to begin with, > then just give you the first 5 for the first call? The order of evaluation is determined by data-dependencies, so it may evaluate the list in order, or evaluate the sixth element first, then the first, after that the third,... > > More tricky - is there a way to make that cache (if it exsists) persist > in an interactive session? Eventually I am intending to write my own > application with a little console at the bottom which does some > ghci-esque magic, but for now lets say I am in ghci and I want to call f > again with the same list. Is there a way to avoid it from forcing a > recomputation of my lazy list? Give it a name: let el = makeExpensiveList args then, barring memory pressure forcing it out, it will be computed only once (each list element will be computed only once, when it's first needed). > > Any advice greatly appreciated, > > Philip From mklklk at hotmail.com Fri Oct 23 09:26:03 2009 From: mklklk at hotmail.com (Kui Ma) Date: Fri Oct 23 09:02:49 2009 Subject: [Haskell-beginners] how to set command args in ghci In-Reply-To: References: <334456.88538.qm@web51608.mail.re2.yahoo.com> Message-ID: Thank you very much! > Date: Fri, 23 Oct 2009 19:40:46 +0900 > Subject: Re: [Haskell-beginners] how to set command args in ghci > From: nonowarn@gmail.com > To: mklklk@hotmail.com > CC: beginners@haskell.org > > :set args foo bar > > should work. FYI, type :? to ghci to see all commands. > > HTH > > -nwn > > 2009/10/23 Kui Ma : > > Is it possible to set the Command Arguments when running the main function > > in ghci? > > > > Thanks, > > Kui > > > > > > > > ________________________________ > > Windows Live: Friends get your Flickr, Yelp, and Digg updates when they > > e-mail you. > > _______________________________________________ > > Beginners mailing list > > Beginners@haskell.org > > http://www.haskell.org/mailman/listinfo/beginners > > > > _________________________________________________________________ Windows Live: Friends get your Flickr, Yelp, and Digg updates when they e-mail you. http://www.microsoft.com/middleeast/windows/windowslive/see-it-in-action/social-network-basics.aspx?ocid=PID23461::T:WLMTAGL:ON:WL:en-xm:SI_SB_3:092010 -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20091023/b0d24f2f/attachment.html From chaddai.fouche at gmail.com Fri Oct 23 09:39:26 2009 From: chaddai.fouche at gmail.com (=?UTF-8?B?Q2hhZGRhw68gRm91Y2jDqQ==?=) Date: Fri Oct 23 09:16:11 2009 Subject: [Haskell-beginners] Haskell Output Help In-Reply-To: <334456.88538.qm@web51608.mail.re2.yahoo.com> References: <334456.88538.qm@web51608.mail.re2.yahoo.com> Message-ID: On Thu, Oct 22, 2009 at 9:11 PM, Chandni Navani wrote: > I have a list of lists which all contain strings. [[String]]. I need to > figure out how to print them so that after each individual string, there is > a new line. > > If this is the initial list [["abc", "cde"] ["fgh", "ghi"]] > [["abc" > "cde"] > ["fgh", > "ghi"]] > > Can anyone help me figure this out? Thanks. > > You can use unlines and concat : > putStr . unlines . concat $ [["abc", "cde"] ["fgh", "ghi"]] -- Jeda? -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20091023/04ca00dc/attachment.html From chaddai.fouche at gmail.com Fri Oct 23 09:41:13 2009 From: chaddai.fouche at gmail.com (=?UTF-8?B?Q2hhZGRhw68gRm91Y2jDqQ==?=) Date: Fri Oct 23 09:17:57 2009 Subject: [Haskell-beginners] how to set command args in ghci In-Reply-To: References: <334456.88538.qm@web51608.mail.re2.yahoo.com> Message-ID: On Fri, Oct 23, 2009 at 12:43 PM, Yusaku Hashimoto wrote: > or > > :ma foo bar > I think this example would be more self-explanatory if you used the whole command name : ghci> :main args1 args2 -- Jeda? From pl.listas at gmail.com Fri Oct 23 10:25:57 2009 From: pl.listas at gmail.com (pl) Date: Fri Oct 23 10:02:42 2009 Subject: [Haskell-beginners] \x -> x < 0.5 && x > -0.5 In-Reply-To: <4ADC280D.1070109@alumni.caltech.edu> References: <4ADC280D.1070109@alumni.caltech.edu> Message-ID: <7b6764f30910230725v7e3b03cdj2e75efddd5fbd578@mail.gmail.com> filter ((<=0.5) . abs) xs On Mon, Oct 19, 2009 at 10:49 AM, Michael Mossey wrote: > Is there a nifty way to write > > filter (\x -> x < 0.5 && x > -0.5) xs > > without explicitly using x? > > Maybe arrows? I have a vague understanding that arrows can "send" an > argument to more than one computation. > > -Mike > _______________________________________________ > 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/20091023/28567100/attachment.html From byorgey at seas.upenn.edu Fri Oct 23 10:26:04 2009 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Fri Oct 23 10:02:47 2009 Subject: [Haskell-beginners] Haskell Output Help In-Reply-To: <334456.88538.qm@web51608.mail.re2.yahoo.com> References: <334456.88538.qm@web51608.mail.re2.yahoo.com> Message-ID: <20091023142604.GA24447@seas.upenn.edu> On Thu, Oct 22, 2009 at 12:11:07PM -0700, Chandni Navani wrote: > I have a list of lists which all contain strings.? [[String]].? I need to figure out how to print them so that after each individual string, there is a new line. > > If this is the initial list [["abc", "cde"] ["fgh", "ghi"]] > [["abc" > ? "cde"] > ?["fgh", > ? "ghi"]] > > Can anyone help me figure this out? Thanks. Most of the other solutions I've seen people post would output something like abc cde fgh ghi But if you actually want brackets and quotes etc. to show the structure, you could do something like putStrLn . bracket . intercalate ",\n " . map (bracket . intercalate ",\n " . map show) where bracket x = "[" ++ x ++ "]" Just transform the list step-by-step into the particular String you want as output. -Brent From daniel.is.fischer at web.de Fri Oct 23 11:23:23 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Fri Oct 23 11:01:28 2009 Subject: [Haskell-beginners] \x -> x < 0.5 && x > -0.5 In-Reply-To: <7b6764f30910230725v7e3b03cdj2e75efddd5fbd578@mail.gmail.com> References: <4ADC280D.1070109@alumni.caltech.edu> <7b6764f30910230725v7e3b03cdj2e75efddd5fbd578@mail.gmail.com> Message-ID: <200910231723.23978.daniel.is.fischer@web.de> Am Freitag 23 Oktober 2009 16:25:57 schrieb pl: > filter ((<=0.5) . abs) xs Would be filter ((< 0.5) . abs). Yes, it's fine. However, if the range is not symmetrical about 0, this is not so nice anymore: cap lo hi = filter (\x -> lo < x && x < hi) cap lo hi = filter (liftM2 (&&) (lo <) (< hi)) cap lo hi = filter ((&&) <$> (lo <) <*> (< hi)) cap lo hi = filter ((< halflength) . abs . subtract mid) where mid = (lo+hi)/2 halflength = (hi-lo)/2 > > On Mon, Oct 19, 2009 at 10:49 AM, Michael Mossey wrote: > > Is there a nifty way to write > > > > filter (\x -> x < 0.5 && x > -0.5) xs > > > > without explicitly using x? > > > > Maybe arrows? I have a vague understanding that arrows can "send" an > > argument to more than one computation. > > > > -Mike From darrinth at gmail.com Fri Oct 23 11:25:57 2009 From: darrinth at gmail.com (Darrin Thompson) Date: Fri Oct 23 11:02:43 2009 Subject: [Haskell-beginners] \x -> x < 0.5 && x > -0.5 In-Reply-To: <7b6764f30910230725v7e3b03cdj2e75efddd5fbd578@mail.gmail.com> References: <4ADC280D.1070109@alumni.caltech.edu> <7b6764f30910230725v7e3b03cdj2e75efddd5fbd578@mail.gmail.com> Message-ID: On Fri, Oct 23, 2009 at 10:25 AM, pl wrote: > > ?? filter ((<=0.5) . abs) xs > pure (&&) <*> (< 0.5) <*> (> -0.5) liftM2 (&&) (< 0.5) (> -0.5) Someone suggested that this was an example of the reader monad but I don't get that. > :i (->) data (->) a b -- Defined in GHC.Prim instance Monad ((->) r) -- Defined in Control.Monad.Instances instance Functor ((->) r) -- Defined in Control.Monad.Instances instance Applicative ((->) a) -- Defined in Control.Applicative That's what I see working here. -- Darrin From daniel.is.fischer at web.de Fri Oct 23 11:32:29 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Fri Oct 23 11:13:08 2009 Subject: [Haskell-beginners] \x -> x < 0.5 && x > -0.5 In-Reply-To: References: <4ADC280D.1070109@alumni.caltech.edu> <7b6764f30910230725v7e3b03cdj2e75efddd5fbd578@mail.gmail.com> Message-ID: <200910231732.29850.daniel.is.fischer@web.de> Am Freitag 23 Oktober 2009 17:25:57 schrieb Darrin Thompson: > On Fri, Oct 23, 2009 at 10:25 AM, pl wrote: > > ?? filter ((<=0.5) . abs) xs > > pure (&&) <*> (< 0.5) <*> (> -0.5) > > liftM2 (&&) (< 0.5) (> -0.5) > > Someone suggested that this was an example of the reader monad but I > don't get that. It's because ((->) r) *is* the reader monad. Control.Monad.Reader's Reader r a is just that wrapped in a newtype: newtype Reader r a = Reader { runReader :: r -> a } > > > :i (->) > > data (->) a b -- Defined in GHC.Prim > instance Monad ((->) r) -- Defined in Control.Monad.Instances > instance Functor ((->) r) -- Defined in Control.Monad.Instances > instance Applicative ((->) a) -- Defined in Control.Applicative > > That's what I see working here. > > -- > Darrin From jakubuv at gmail.com Fri Oct 23 11:42:48 2009 From: jakubuv at gmail.com (Jan Jakubuv) Date: Fri Oct 23 11:20:59 2009 Subject: [Haskell-beginners] Haskell Output Help In-Reply-To: <334456.88538.qm@web51608.mail.re2.yahoo.com> References: <334456.88538.qm@web51608.mail.re2.yahoo.com> Message-ID: <20091023154248.GA8468@lxultra2.macs.hw.ac.uk> Hi, seems to me like a job for `Text.PrettyPrint`: import Text.PrettyPrint ppString :: String -> Doc ppString = doubleQuotes . text ppList :: [Doc] -> Doc ppList = brackets . vcat . punctuate (text ",") pretty = ppList . map (ppList . map ppString) The code is hopefully almost self-explaining (`vcat` does the line breaking). The result looks as follows: *Main> pretty [["abc", "cde"], ["fgh", "ghi"]] [["abc", "cde"], ["fgh", "ghi"]] Sincerely, jan. On Thu, Oct 22, 2009 at 12:11:07PM -0700, Chandni Navani wrote: > I have a list of lists which all contain strings.? [[String]].? I need to figure out how to print them so that after each individual string, there is a new line. > > If this is the initial list [["abc", "cde"] ["fgh", "ghi"]] > [["abc" > ? "cde"] > ?["fgh", > ? "ghi"]] > > Can anyone help me figure this out? Thanks. -- Heriot-Watt University is a Scottish charity registered under charity number SC000278. From daniel.is.fischer at web.de Fri Oct 23 13:34:44 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Fri Oct 23 13:12:49 2009 Subject: [Haskell-beginners] Caching evaluation of lazy lists In-Reply-To: <4AE1DA3D.6000107@foo.me.uk> References: <4AE198D8.8090402@foo.me.uk> <200910231508.03578.daniel.is.fischer@web.de> <4AE1DA3D.6000107@foo.me.uk> Message-ID: <200910231934.44610.daniel.is.fischer@web.de> Am Freitag 23 Oktober 2009 18:30:53 schrieb Philip Scott: > Hello again, > > > then, barring memory pressure forcing it out, it will be computed only > > once (each list element will be computed only once, when it's first > > needed). > > Thanks Daniel, that was what I was after. Is there any way of > investigating these things without using the profiler? E.g. is there any > way to stick a debug print statement inside a function without moving > over to sideeffects and IO Monads etc.. I know printing is a side > effect, but it would be nice to say 'I can has itsy sneeky side effect > plz Haskell, just for little testing while' > > Cheers, > > Philip import Debug.Trace infixl 0 `debug` debug = flip trace dfib :: Int -> Integer dfib = let fib 0 = 0 fib 1 = 1 fib n = dfib (n-2) + dfib (n-1) `debug` "eval fib " ++ show n in (map fib [0 .. ] !!) Ok, modules loaded: MFib. *MFib> dfib 4 eval fib 4 eval fib 2 eval fib 3 3 *MFib> dfib 7 eval fib 7 eval fib 5 eval fib 6 13 *MFib> dfib 15 eval fib 15 eval fib 13 eval fib 11 eval fib 9 eval fib 8 eval fib 10 eval fib 12 eval fib 14 610 *MFib> The trick with debug = flip trace makes commenting out the debug-code easier: fun x = trace ("fun " ++ show x) $ body x ~> fun x = {- trace ("fun " ++ show x) $ -} body x vs. fun x = body x `debug` "fun " ++ show x ~> fun x = body x -- `debug` "fun " ++ show x But beware, including the argument in the trace message can lead to recalculation of values which would be cached without it, it's a hairy issue. From darrinth at gmail.com Fri Oct 23 16:24:38 2009 From: darrinth at gmail.com (Darrin Thompson) Date: Fri Oct 23 16:01:28 2009 Subject: [Haskell-beginners] \x -> x < 0.5 && x > -0.5 In-Reply-To: <200910231732.29850.daniel.is.fischer@web.de> References: <4ADC280D.1070109@alumni.caltech.edu> <7b6764f30910230725v7e3b03cdj2e75efddd5fbd578@mail.gmail.com> <200910231732.29850.daniel.is.fischer@web.de> Message-ID: On Fri, Oct 23, 2009 at 11:32 AM, Daniel Fischer wrote: > It's because ((->) r) *is* the reader monad. > Control.Monad.Reader's Reader r a is just that wrapped in a newtype: > > newtype Reader r a = Reader { runReader :: r -> a } > So I was thinking: :t runReader $ liftM2 (&&) (Reader (< 0.5)) (Reader (> -0.5)) Thanks. -- Darrin From k.srikanth.opensource at gmail.com Sat Oct 24 11:55:10 2009 From: k.srikanth.opensource at gmail.com (Srikanth K) Date: Sat Oct 24 11:31:52 2009 Subject: [Haskell-cafe] Re: [Haskell-beginners] using quickcheck for blackbox testing for 3rd party apps. In-Reply-To: <200910131934.36808.daniel.is.fischer@web.de> References: <20091013160452.GB2687@seas.upenn.edu> <200910131934.36808.daniel.is.fischer@web.de> Message-ID: Thanks. unsafePerformIO seems to suffice me for the moment... However, I am ignorant about what would happen when multiple such unsafePerformIO are done inside one function. On Tue, Oct 13, 2009 at 11:04 PM, Daniel Fischer wrote: > Am Dienstag 13 Oktober 2009 18:04:52 schrieb Brent Yorgey: > > Brent > > > > * Some smart-alecks might pipe up with something about unsafePerformIO > > here. But that's not a cure, it's more like performing an emergency > > tracheotomy with a ballpoint pen. > > Quote of the month! > _______________________________________________ > Haskell-Cafe mailing list > Haskell-Cafe@haskell.org > http://www.haskell.org/mailman/listinfo/haskell-cafe > -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20091024/2031466d/attachment.html From john.moore54 at gmail.com Sat Oct 24 12:59:35 2009 From: john.moore54 at gmail.com (John Moore) Date: Sat Oct 24 12:36:16 2009 Subject: [Haskell-beginners] random Message-ID: <4f7ad1ad0910240959v67c47bd3h2162230c1770e01b@mail.gmail.com> Hi All, Can anyone help me I want to produce a list of three random numbers for e.g. [7,8,1] I tried using x <- getStdRandom $ randomR (1,10) but don't really understand this and it only generates one number. Any help greatly appreciated. Regards John -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20091024/6bd55d58/attachment.html From byorgey at seas.upenn.edu Sat Oct 24 20:05:40 2009 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Sat Oct 24 19:42:19 2009 Subject: [Haskell-beginners] random In-Reply-To: <4f7ad1ad0910240959v67c47bd3h2162230c1770e01b@mail.gmail.com> References: <4f7ad1ad0910240959v67c47bd3h2162230c1770e01b@mail.gmail.com> Message-ID: <20091025000539.GA27659@seas.upenn.edu> On Sat, Oct 24, 2009 at 05:59:35PM +0100, John Moore wrote: > Hi All, > Can anyone help me I want to produce a list of three random > numbers for e.g. [7,8,1] > I tried using x <- getStdRandom $ randomR (1,10) but don't really understand > this and it only generates one number. Any help greatly appreciated. replicateM is your friend: replicateM :: (Monad m) => Int -> m a -> m [a] so if 'foo' produces a single random number, then 'replicateM 3 foo' produces a list of three. -Brent From tom.davie at gmail.com Sat Oct 24 20:25:58 2009 From: tom.davie at gmail.com (Tom Davie) Date: Sat Oct 24 20:02:38 2009 Subject: [Haskell-beginners] random In-Reply-To: <20091025000539.GA27659@seas.upenn.edu> References: <4f7ad1ad0910240959v67c47bd3h2162230c1770e01b@mail.gmail.com> <20091025000539.GA27659@seas.upenn.edu> Message-ID: <8b70a98a0910241725x554717cfld3151a89b628bb55@mail.gmail.com> Or just randomRs :: (Random a, RandomGen g) => (a,a) -> g -> [a] Bob On Sun, Oct 25, 2009 at 2:05 AM, Brent Yorgey wrote: > On Sat, Oct 24, 2009 at 05:59:35PM +0100, John Moore wrote: > > Hi All, > > Can anyone help me I want to produce a list of three random > > numbers for e.g. [7,8,1] > > I tried using x <- getStdRandom $ randomR (1,10) but don't really > understand > > this and it only generates one number. Any help greatly appreciated. > > replicateM is your friend: > > replicateM :: (Monad m) => Int -> m a -> m [a] > > so if 'foo' produces a single random number, then 'replicateM 3 foo' > produces a list of three. > > -Brent > _______________________________________________ > 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/20091024/105066e0/attachment.html From aditya.siram at gmail.com Sat Oct 24 21:04:08 2009 From: aditya.siram at gmail.com (aditya siram) Date: Sat Oct 24 20:40:48 2009 Subject: [Haskell-beginners] random In-Reply-To: <20091025000539.GA27659@seas.upenn.edu> References: <4f7ad1ad0910240959v67c47bd3h2162230c1770e01b@mail.gmail.com> <20091025000539.GA27659@seas.upenn.edu> Message-ID: <594f78210910241804u696d8506pebf53b69ebb0538e@mail.gmail.com> Hi John, When I was first encountered replicateM I found it really hard to understand. So,of course, I am audacious enough to assume that it is hard for you too! The code suggested by Brent , 'replicateM 3 foo' is a nicer way of writing the following: foo = do x <- getStdRandom $ randomR (1,10) y <- getStdRandom $ randomR (1,10) z <- getStdRandom $ randomR (1,10) return [x,y,z] Hope this helped. -deech On Sat, Oct 24, 2009 at 7:05 PM, Brent Yorgey wrote: > On Sat, Oct 24, 2009 at 05:59:35PM +0100, John Moore wrote: > > Hi All, > > Can anyone help me I want to produce a list of three random > > numbers for e.g. [7,8,1] > > I tried using x <- getStdRandom $ randomR (1,10) but don't really > understand > > this and it only generates one number. Any help greatly appreciated. > > replicateM is your friend: > > replicateM :: (Monad m) => Int -> m a -> m [a] > > so if 'foo' produces a single random number, then 'replicateM 3 foo' > produces a list of three. > > -Brent > _______________________________________________ > 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/20091024/39a39e70/attachment-0001.html From john.moore54 at gmail.com Sun Oct 25 14:57:38 2009 From: john.moore54 at gmail.com (John Moore) Date: Sun Oct 25 14:34:17 2009 Subject: [Haskell-beginners] nim programme Message-ID: <4f7ad1ad0910251157v3d49c5f6jb5ef643eefc1b42a@mail.gmail.com> Hi All, I'm attempting to write a program for the game nim.(The game of Nim is played with two players and several piles of stones. On each move a player removes as many stones as they would like but form only one pile. The player who takes the last stone wins) It not as simple as I first thought. Here is my basic starting points. Any comments would be greatly appreciated. I not well versed in Haskell yet so simple(basic) Haskell rather than well written haskell if you understand what I mean. Complicated monads are way out of my league. 1) I first get the program to give me three random piles by doing nim = do x <- getStdRandom $ randomR (1,10) y <- getStdRandom $ randomR (1,10) z <- getStdRandom $ randomR (1,10) return [x,y,z] Cant get this to work! 2) Now I need to get the program to ask for a number and which pile to remove the number from. This is tricky I thought about asking to find the elementAt elementAt :: [a] -> Int -> a elementAt list i = list !! (i-1) put this in a variable then asking the palyer how many to take away. and then subtracting the number from and then putting it back into the list but this seem impossible. Then the second player would do the same. 3) Finally we would end up with a case statement like f x = in case of x [0,0,1]-> You win [0,1,0]-> You win [0,0,1]-> You win [_,_,_]-> keep playing. Lets know what you think please, getting confused. John -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20091025/5a3da27e/attachment.html From bugfact at gmail.com Sun Oct 25 15:07:26 2009 From: bugfact at gmail.com (Peter Verswyvelen) Date: Sun Oct 25 14:44:03 2009 Subject: [Haskell-beginners] nim programme In-Reply-To: <4f7ad1ad0910251157v3d49c5f6jb5ef643eefc1b42a@mail.gmail.com> References: <4f7ad1ad0910251157v3d49c5f6jb5ef643eefc1b42a@mail.gmail.com> Message-ID: Hi John, regarding the first problem, just provide the type of your range explicitly: nim = do let range = (1,10) :: (Int,Int) x <- getStdRandom $ randomR range y <- getStdRandom $ randomR range z <- getStdRandom $ randomR range return [x,y,z] This is because the annoying monomorphism restriction, which will most likely be reduced in the next version of Haskell. You can also disable this restriction: {-# LANGUAGE NoMonomorphismRestriction #-} import System.Random nim = do let range = (1,10) -- no explicit type annotation needed anymore x <- getStdRandom $ randomR range y <- getStdRandom $ randomR range z <- getStdRandom $ randomR range return [x,y,z] Cheers, Peter On Sun, Oct 25, 2009 at 7:57 PM, John Moore wrote: > Hi All, > ??????? I'm attempting to write a program for the game nim.(The game of Nim > is played with two players and several piles of stones. On each move a > player removes as many stones as they would like but form only one pile. The > player who takes the last stone wins) It not as simple as I first thought. > Here is my basic starting points. Any comments would be greatly appreciated. > I not well versed in Haskell yet so simple(basic) Haskell rather than well > written haskell if you understand what I mean. Complicated monads are way > out of my league. > > > 1) I first get the program to give me three random piles by doing > ??? nim = do > ? x <- getStdRandom $ randomR (1,10) > ? y <- getStdRandom $ randomR (1,10) > ? z <- getStdRandom $ randomR (1,10) > ? return [x,y,z] > ?Cant get this to work! > 2) Now I need to get the program to ask for a number and which pile to > remove the number? from. This is tricky I thought about asking to find the > elementAt > elementAt :: [a] -> Int -> a > elementAt list i = list !! (i-1) put this in a variable > then asking the palyer how many to take away. > ?and then subtracting the number from and then putting it back into the list > but this seem impossible. > Then the second player would do the same. > 3) Finally we would end up with a case statement like > f x = in case of x > [0,0,1]-> You win > [0,1,0]-> You win > [0,0,1]-> You win > [_,_,_]-> keep playing. > > Lets know what you think please, getting confused. > > John > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > From bugfact at gmail.com Sun Oct 25 15:09:28 2009 From: bugfact at gmail.com (Peter Verswyvelen) Date: Sun Oct 25 14:46:06 2009 Subject: [Haskell-beginners] nim programme In-Reply-To: References: <4f7ad1ad0910251157v3d49c5f6jb5ef643eefc1b42a@mail.gmail.com> Message-ID: Btw, it's often a good idea to introduce type signatures: nim :: IO [Int] then you don't need to provide type signatures either, and don't have to disable the monomo restriction On Sun, Oct 25, 2009 at 8:07 PM, Peter Verswyvelen wrote: > Hi John, > > regarding the first problem, just provide the type of your range explicitly: > > nim = do > ?let range = (1,10) :: (Int,Int) > ?x <- getStdRandom $ randomR range > ?y <- getStdRandom $ randomR range > ?z <- getStdRandom $ randomR range > ?return [x,y,z] > > This is because the annoying monomorphism restriction, which will most > likely be reduced in the next version of Haskell. > > You can also disable this restriction: > > {-# LANGUAGE NoMonomorphismRestriction #-} > > import System.Random > > nim = do > ?let range = (1,10) -- no explicit type annotation needed anymore > ?x <- getStdRandom $ randomR range > ?y <- getStdRandom $ randomR range > ?z <- getStdRandom $ randomR range > ?return [x,y,z] > > Cheers, > Peter > > On Sun, Oct 25, 2009 at 7:57 PM, John Moore wrote: >> Hi All, >> ??????? I'm attempting to write a program for the game nim.(The game of Nim >> is played with two players and several piles of stones. On each move a >> player removes as many stones as they would like but form only one pile. The >> player who takes the last stone wins) It not as simple as I first thought. >> Here is my basic starting points. Any comments would be greatly appreciated. >> I not well versed in Haskell yet so simple(basic) Haskell rather than well >> written haskell if you understand what I mean. Complicated monads are way >> out of my league. >> >> >> 1) I first get the program to give me three random piles by doing >> ??? nim = do >> ? x <- getStdRandom $ randomR (1,10) >> ? y <- getStdRandom $ randomR (1,10) >> ? z <- getStdRandom $ randomR (1,10) >> ? return [x,y,z] >> ?Cant get this to work! >> 2) Now I need to get the program to ask for a number and which pile to >> remove the number? from. This is tricky I thought about asking to find the >> elementAt >> elementAt :: [a] -> Int -> a >> elementAt list i = list !! (i-1) put this in a variable >> then asking the palyer how many to take away. >> ?and then subtracting the number from and then putting it back into the list >> but this seem impossible. >> Then the second player would do the same. >> 3) Finally we would end up with a case statement like >> f x = in case of x >> [0,0,1]-> You win >> [0,1,0]-> You win >> [0,0,1]-> You win >> [_,_,_]-> keep playing. >> >> Lets know what you think please, getting confused. >> >> John >> _______________________________________________ >> Beginners mailing list >> Beginners@haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> > From mpm at alumni.caltech.edu Sun Oct 25 16:27:22 2009 From: mpm at alumni.caltech.edu (Michael Mossey) Date: Sun Oct 25 16:04:20 2009 Subject: [Haskell-beginners] nim programme In-Reply-To: <4f7ad1ad0910251157v3d49c5f6jb5ef643eefc1b42a@mail.gmail.com> References: <4f7ad1ad0910251157v3d49c5f6jb5ef643eefc1b42a@mail.gmail.com> Message-ID: <4AE4B4AA.7010505@alumni.caltech.edu> Hi John, I'm not sure how much experience you have with Haskell, but there is a general problem-solving principle: solve a simpler problem first. For example, you could write a version of this that uses only one pile of stones. You will still have to create a loop, exit condition, prompt and read, and basic things like that. Once those are solved, then you can turn your attention to arrays. -Mike John Moore wrote: > Hi All, > I'm attempting to write a program for the game nim. From orclev at gmail.com Sun Oct 25 17:24:07 2009 From: orclev at gmail.com (Kyle Murphy) Date: Sun Oct 25 17:00:47 2009 Subject: [Haskell-beginners] nim programme In-Reply-To: References: <4f7ad1ad0910251157v3d49c5f6jb5ef643eefc1b42a@mail.gmail.com> Message-ID: <2db78cee0910251424q42b76d54t3d48d7a9a0f2f77e@mail.gmail.com> This is a good example of Haskell trying to be smart and causing confusion. Remember that the compiler needs a type for everything and if you don't provide one it will try to guess. The best way to avoid this issue is to start writing a function by first figuring out its type signature and then writing the function. You need some way to track game state. There's two (at least) ways to do that. One way is using a monad which we'll ignore for now. The second way is by defining some sort of state type and explicitly passing it in and out of your functions. This could look like: data State = State { turn :: Int, piles :: [Int] } deriving (Show, Eq) runOn :: (a -> a) -> Int -> [a] -> [a] runOn g i xs = runOn' g (splitAt i xs) where runOn' g (h, t) = h ++ ((g (head t)) : (drop 1 t)) removeStone :: State -> Int -> Int -> State removeStone s i num = State ((turn s)+1) (runOn (\x -> x - num) i (piles s)) Note that there's a potential failure condition here if you provide an index off the end of the pile array (because of the use of head without a guard). removeStone would be used like so: Main> removeStone (State 4 [5,6,7]) 0 2 State { turn = 5, piles = [3,6,7]} Main> removeStone (State 5 [3,6,7]) 1 3 State { turn = 6, piles = [3,3,7]} Using a Monad to do this is very similar, but instead of manual passing the State around, you store the State inside of the enclosing Monad and the various functions fetch and store the state from the Monad. Also bear in mind that once again you're not modifying a List in any of these functions, that's impossible, rather you are making a *new* list using pieces of the old list. On Sunday, October 25, 2009, Peter Verswyvelen wrote: > Btw, it's often a good idea to introduce type signatures: > > nim :: IO [Int] > > then you don't need to provide type signatures either, and don't have > to disable the monomo restriction > > On Sun, Oct 25, 2009 at 8:07 PM, Peter Verswyvelen wrote: >> Hi John, >> >> regarding the first problem, just provide the type of your range explicitly: >> >> nim = do >> ?let range = (1,10) :: (Int,Int) >> ?x <- getStdRandom $ randomR range >> ?y <- getStdRandom $ randomR range >> ?z <- getStdRandom $ randomR range >> ?return [x,y,z] >> >> This is because the annoying monomorphism restriction, which will most >> likely be reduced in the next version of Haskell. >> >> You can also disable this restriction: >> >> {-# LANGUAGE NoMonomorphismRestriction #-} >> >> import System.Random >> >> nim = do >> ?let range = (1,10) -- no explicit type annotation needed anymore >> ?x <- getStdRandom $ randomR range >> ?y <- getStdRandom $ randomR range >> ?z <- getStdRandom $ randomR range >> ?return [x,y,z] >> >> Cheers, >> Peter >> >> On Sun, Oct 25, 2009 at 7:57 PM, John Moore wrote: >>> Hi All, >>> ??????? I'm attempting to write a program for the game nim.(The game of Nim >>> is played with two players and several piles of stones. On each move a >>> player removes as many stones as they would like but form only one pile. The >>> player who takes the last stone wins) It not as simple as I first thought. >>> Here is my basic starting points. Any comments would be greatly appreciated. >>> I not well versed in Haskell yet so simple(basic) Haskell rather than well >>> written haskell if you understand what I mean. Complicated monads are way >>> out of my league. >>> >>> >>> 1) I first get the program to give me three random piles by doing >>> ??? nim = do >>> ? x <- getStdRandom $ randomR (1,10) >>> ? y <- getStdRandom $ randomR (1,10) >>> ? z <- getStdRandom $ randomR (1,10) >>> ? return [x,y,z] >>> ?Cant get this to work! >>> 2) Now I need to get the program to ask for a number and which pile to >>> remove the number? from. This is tricky I thought about asking to find the >>> elementAt >>> elementAt :: [a] -> Int -> a >>> elementAt list i = list !! (i-1) put this in a variable >>> then asking the palyer how many to take away. >>> ?and then subtracting the number from and then putting it back into the list >>> but this seem impossible. >>> Then the second player would do the same. >>> 3) Finally we would end up with a case statement like >>> f x = in case of x >>> [0,0,1]-> You win >>> [0,1,0]-> You win >>> [0,0,1]-> You win >>> [_,_,_]-> keep playing. >>> >>> Lets know what you think please, getting confused. >>> >>> John >>> _______________________________________________ >>> Beginners mailing list >>> Beginners@haskell.org >>> http://www.haskell.org/mailman/listinfo/beginners >>> >>> >> > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > From chaddai.fouche at gmail.com Sun Oct 25 17:25:55 2009 From: chaddai.fouche at gmail.com (=?UTF-8?B?Q2hhZGRhw68gRm91Y2jDqQ==?=) Date: Sun Oct 25 17:02:32 2009 Subject: [Haskell-beginners] nim programme In-Reply-To: References: <4f7ad1ad0910251157v3d49c5f6jb5ef643eefc1b42a@mail.gmail.com> Message-ID: On Sun, Oct 25, 2009 at 8:09 PM, Peter Verswyvelen wrote: > Btw, it's often a good idea to introduce type signatures: > > nim :: IO [Int] > > then you don't need to provide type signatures either, and don't have > to disable the monomo restriction > That's the solution I would use in this case. :) Also, there's too much redundancy in this code, you don't need getStdRandom, you don't need to repeat 3 times the same snippet, and so on, this code does the same thing in a arguably clearer fashion and is unarguably shorter : > initNim :: IO [Int] > initNim = replicateM 3 $ randomRIO (1,10) (you need to import Control.Monad and System.Random for this) "replicateM n action" just does "action" n times and returns the results in a list, randomRIO is equal to "getStdRandom . randomR". All that you want to do is pretty easy to do, as long as you do it a small bit at a time. -- Jeda? From pscott at foo.me.uk Fri Oct 23 12:30:53 2009 From: pscott at foo.me.uk (Philip Scott) Date: Sun Oct 25 22:28:48 2009 Subject: [Haskell-beginners] Caching evaluation of lazy lists In-Reply-To: <200910231508.03578.daniel.is.fischer@web.de> References: <4AE198D8.8090402@foo.me.uk> <200910231508.03578.daniel.is.fischer@web.de> Message-ID: <4AE1DA3D.6000107@foo.me.uk> Hello again, > then, barring memory pressure forcing it out, it will be computed only once (each list > element will be computed only once, when it's first needed). > Thanks Daniel, that was what I was after. Is there any way of investigating these things without using the profiler? E.g. is there any way to stick a debug print statement inside a function without moving over to sideeffects and IO Monads etc.. I know printing is a side effect, but it would be nice to say 'I can has itsy sneeky side effect plz Haskell, just for little testing while' Cheers, Philip From pscott at foo.me.uk Fri Oct 23 17:12:20 2009 From: pscott at foo.me.uk (Philip Scott) Date: Sun Oct 25 22:29:09 2009 Subject: [Haskell-beginners] Caching evaluation of lazy lists In-Reply-To: <200910231934.44610.daniel.is.fischer@web.de> References: <4AE198D8.8090402@foo.me.uk> <200910231508.03578.daniel.is.fischer@web.de> <4AE1DA3D.6000107@foo.me.uk> <200910231934.44610.daniel.is.fischer@web.de> Message-ID: <4AE21C34.8050408@foo.me.uk> >> Thanks Daniel, that was what I was after. Is there any way of >> investigating these things without using the profiler? >> > > import Debug.Trace > Genius, thank you! From john.moore54 at gmail.com Mon Oct 26 07:51:47 2009 From: john.moore54 at gmail.com (John Moore) Date: Mon Oct 26 07:28:23 2009 Subject: [Haskell-beginners] nim Message-ID: <4f7ad1ad0910260451l60fe7df2q21f239c919d2305f@mail.gmail.com> Hi All, In the game of nim I want to make a move such as take x from pile A. This will then be subtracted from the starting list.Is this correct in as much as I want to have a move Take 3 from pile A: input would be A3 I need it to return a list as I wish to take this result and use zipwith (-) [starting list][result]. How do I use the result, do I store in a variable? typeOfMove :: (a,b) -> [Int] typeOfMove ax |ax = [x,0,0] |bx = [0,x,0] |cx = [0,0,x] John -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20091026/457f503f/attachment.html From nefigah at gmail.com Mon Oct 26 13:49:51 2009 From: nefigah at gmail.com (Jordan Cooper) Date: Mon Oct 26 13:26:26 2009 Subject: [Haskell-beginners] Built-in/more elegant version of this function? Message-ID: <301488c50910261049k2646abd0i9bd94d5b50845660@mail.gmail.com> I was wondering if there is a built-in way to do what this function I wrote does (or at least a more elegant way, preferably without explicit recursion). f xss = map safeHead xss : f (map safeTail xss) (where safeHead/Tail are versions of head/tail that don't crash on [] input) From orclev at gmail.com Mon Oct 26 14:59:16 2009 From: orclev at gmail.com (Kyle Murphy) Date: Mon Oct 26 14:35:53 2009 Subject: [Haskell-beginners] nim In-Reply-To: <4f7ad1ad0910260451l60fe7df2q21f239c919d2305f@mail.gmail.com> References: <4f7ad1ad0910260451l60fe7df2q21f239c919d2305f@mail.gmail.com> Message-ID: <2db78cee0910261159x47bde937w915c6230abd875c4@mail.gmail.com> The type signature you provided for typeOfMove doesn't match the function body. "typeOfMove :: (a,b) -> [Int]" says that typeOfMove takes a tuple of two different values, and returns a list of Ints, but the definition you provided for typeOfMove only takes a list. I think perhaps what you want is something more like: import Data.Char (toLower) import Control.Monad (liftM) typeOfMove :: (Char, Int) -> [Int] -> [Int] typeOfMove ('a', x) xs = zipWith (-) xs [x,0,0] typeOfMove ('b', x) xs = zipWith (-) xs [0,x,0] typeOfMove ('c', x) xs = zipWith (-) xs [0,0,x] main :: IO () main = do putStrLn "Which pile (A,B,C)?" x <- liftM toLower $ getChar putStrLn "" putStrLn "How many stones?" y <- readLn let z = typeOfMove (x,y) [5,6,7] putStrLn . show $ z Of course, another way to do much the same thing is the following, which has the advantage that you don't get an exception inside the typeOfMove function if the user passes in a value besides A, B, or C, although you do get an exception when readLn attempts to perarse the value (which is better because you can trap the exception there and re-ask for the correct value). Similarly this version still isn't very good because it doesn't do any sort of bounds checking on the number of stones to subtract, but I leave that as an exercise to you. data PileName = A | B | C deriving (Show, Eq, Read) typeOfMove :: (PileName, Int) -> [Int] -> [Int] typeOfMove (A, x) xs = zipWith (-) xs [x,0,0] typeOfMove (B, x) xs = zipWith (-) xs [0,x,0] typeOfMove (C, x) xs = zipWith (-) xs [0,0,x] main :: IO () main = do putStrLn "Which pile A, B, or C (case matters)?" x <- readLn putStrLn "How many stones?" y <- readLn let z = typeOfMove (x,y) [5,6,7] putStrLn . show $ z -R. Kyle Murphy -- Curiosity was framed, Ignorance killed the cat. On Mon, Oct 26, 2009 at 07:51, John Moore wrote: > Hi All, > In the game of nim I want to make a move such as take x from > pile A. This will then be subtracted from the starting list.Is this correct > in as much as I want to have a move Take 3 from pile A: input would be A3 I > need it to return a list as I wish to take this result and use zipwith (-) > [starting list][result]. How do I use the result, do I store in a variable? > > typeOfMove :: (a,b) -> [Int] > typeOfMove ax > |ax = [x,0,0] > |bx = [0,x,0] > |cx = [0,0,x] > > John > > _______________________________________________ > 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/20091026/6f164a18/attachment.html From daniel.is.fischer at web.de Mon Oct 26 14:59:32 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Mon Oct 26 14:39:29 2009 Subject: [Haskell-beginners] Built-in/more elegant version of this function? In-Reply-To: <301488c50910261049k2646abd0i9bd94d5b50845660@mail.gmail.com> References: <301488c50910261049k2646abd0i9bd94d5b50845660@mail.gmail.com> Message-ID: <200910261959.32330.daniel.is.fischer@web.de> Am Montag 26 Oktober 2009 18:49:51 schrieb Jordan Cooper: > I was wondering if there is a built-in way to do what this function I > wrote does (or at least a more elegant way, preferably without > explicit recursion). > > f xss = map safeHead xss : f (map safeTail xss) > > (where safeHead/Tail are versions of head/tail that don't crash on [] > input) Not quite the same, but Prelude> :m +Data.List Prelude Data.List> transpose [[1,2],[],[3,4,5]] [[1,3],[2,4],[5]] could be more or less what you want. From mauricio.antunes at gmail.com Mon Oct 26 18:24:26 2009 From: mauricio.antunes at gmail.com (=?ISO-8859-1?Q?Maur=ED=ADcio_CA?=) Date: Mon Oct 26 18:01:26 2009 Subject: [Haskell-beginners] Why is this type ambiguous? Message-ID: import Foreign import Foreign.C genericCast :: (Storable a, Storable b) => a -> IO b genericCast v = let dummy = undefined size = max (sizeOf v) (sizeOf dummy) in if False then return dummy else allocaBytes size $ \p -> poke p v >> peek (castPtr p) ---- Code above gives me this: Ambiguous type variable `a' in the constraint: `Storable a' arising from a use of `sizeOf' at src/Bindings/C.hs:28:27-38 ---- It seems to refer to '(sizeOf dummy)'. But isn't the type of 'dummy' defined by 'return dummy' beeing a possible return value (and, so, dummy :: b)? Thanks, Maur?cio From bugfact at gmail.com Mon Oct 26 18:38:50 2009 From: bugfact at gmail.com (Peter Verswyvelen) Date: Mon Oct 26 18:15:25 2009 Subject: [Haskell-beginners] Why is this type ambiguous? In-Reply-To: References: Message-ID: I have no idea what you're trying to do here (looks as if you just want to recreate Unsafe.Coerce.unsafeCoerce?) However, dummy is first used an 'a' and then as a 'b', so that can't work. Next, in sizeOf undefined, undefined does not provide enough information about that type undefined should be. Although the compiler can infer that sizeOf undefined must be of the same type as sizeOf v, that's not enough, it needs to know the type of undefined (I guess you want it to be the same type as v?) Can be fixed like: genericCast :: (Storable a, Storable b) => a -> IO b genericCast v = let size = max (sizeOf v) (sizeOf $ undefined `asTypeOf` v) in if False then return undefined else allocaBytes size $ \p -> poke p v >> peek (castPtr p) But... feels insanely hacky, and is not something a beginner should attempt to play with I guess ;) 2009/10/26 Maur??cio CA : > import Foreign > import Foreign.C > > genericCast :: (Storable a, Storable b) => a -> IO b > genericCast v = let > ? ?dummy = undefined > ? ?size = max (sizeOf v) (sizeOf dummy) > ?in if False > ? ?then return dummy > ? ?else allocaBytes size $ \p -> poke p v >> peek (castPtr p) > > ---- > > Code above gives me this: > > ?Ambiguous type variable `a' in the constraint: > ? ?`Storable a' > ? ? ?arising from a use of `sizeOf' at src/Bindings/C.hs:28:27-38 > > ---- > > It seems to refer to '(sizeOf dummy)'. But isn't the > type of 'dummy' defined by 'return dummy' beeing a > possible return value (and, so, dummy :: b)? > > Thanks, > Maur?cio > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > From daniel.is.fischer at web.de Mon Oct 26 19:11:20 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Mon Oct 26 18:49:16 2009 Subject: [Haskell-beginners] Why is this type ambiguous? In-Reply-To: References: Message-ID: <200910270011.20988.daniel.is.fischer@web.de> Am Montag 26 Oktober 2009 23:24:26 schrieb Maur??cio CA: > import Foreign > import Foreign.C > > genericCast :: (Storable a, Storable b) => a -> IO b > genericCast v = let > dummy = undefined > size = max (sizeOf v) (sizeOf dummy) > in if False > then return dummy > else allocaBytes size $ \p -> poke p v >> peek (castPtr p) > > ---- > > Code above gives me this: > > Ambiguous type variable `a' in the constraint: > `Storable a' > arising from a use of `sizeOf' at src/Bindings/C.hs:28:27-38 > > ---- > > It seems to refer to '(sizeOf dummy)'. But isn't the > type of 'dummy' defined by 'return dummy' beeing a > possible return value (and, so, dummy :: b)? No. let-bindings are polymorphic, so dummy :: forall a. a {-# LANGUAGE ScopedTypeVariables #-} -- This is unsafe, don't use genericCast :: forall a b. (Storable a, Storable b) => a -> IO b genericCast v = let dummy :: b dummy = undefined ... > > Thanks, > Maur?cio From mauricio.antunes at gmail.com Mon Oct 26 19:12:15 2009 From: mauricio.antunes at gmail.com (=?ISO-8859-1?Q?Maur=ED=ADcio_CA?=) Date: Mon Oct 26 18:49:19 2009 Subject: [Haskell-beginners] Re: Why is this type ambiguous? In-Reply-To: References: Message-ID: > I have no idea what you're trying to do here (looks as if you > just want to recreate Unsafe.Coerce.unsafeCoerce?) Yes, except that it works with Storables only and is safe against runtime corruption. Evil, but can save you from worst FFI hacks. > However, dummy is first used an 'a' and then as a 'b', so that > can't work. Where is dummy used as 'a'? The only place with a specific type I used it is in 'return dummy', and it's there exactly to "get" that 'b' type. > Although the compiler can infer that sizeOf undefined must be of > the same type as sizeOf v, (...) > > size = max (sizeOf v) (sizeOf $ undefined `asTypeOf` v) No, it's not the same type! The idea is that 'size' value should be the bigger of 'a' and 'b' sizes, so that I guarantee enough memory will be allocated for the cast. > But... feels insanely hacky, and is not something a beginner > should attempt to play with I guess ;) I have an unusual experience with Haskell. I used it a lot, but mainly with FFI. So, I'm an expert in FFI -- I have my own package of hsc2hs macros :) -- but I have no understanding of the type system, except for just the basics. Maur?cio >> import Foreign >> import Foreign.C >> >> genericCast :: (Storable a, Storable b) => a -> IO b >> genericCast v = let >> dummy = undefined >> size = max (sizeOf v) (sizeOf dummy) >> in if False >> then return dummy >> else allocaBytes size $ \p -> poke p v >> peek (castPtr p) >> From nefigah at gmail.com Mon Oct 26 19:48:25 2009 From: nefigah at gmail.com (Jordan Cooper) Date: Mon Oct 26 19:24:58 2009 Subject: [Haskell-beginners] Re: Built-in/more elegant version of this function? Message-ID: <301488c50910261648tc4b35bdx7c71bb2d7421d6a0@mail.gmail.com> Am Montag 26 Oktober 2009 18:49:51 schrieb Jordan Cooper: >> I was wondering if there is a built-in way to do what this function I >> wrote does (or at least a more elegant way, preferably without >> explicit recursion). >> >> f xss = map safeHead xss : f (map safeTail xss) >> >> (where safeHead/Tail are versions of head/tail that don't crash on [] >> input) > >Not quite the same, but > >Prelude> :m +Data.List >Prelude Data.List> transpose [[1,2],[],[3,4,5]] >[[1,3],[2,4],[5]] > >could be more or less what you want. Thank you, I think that will work nicely! From mauricio.antunes at gmail.com Mon Oct 26 21:34:23 2009 From: mauricio.antunes at gmail.com (=?ISO-8859-1?Q?Maur=ED=ADcio_CA?=) Date: Mon Oct 26 21:11:21 2009 Subject: [Haskell-beginners] Re: Why is this type ambiguous? In-Reply-To: <200910270011.20988.daniel.is.fischer@web.de> References: <200910270011.20988.daniel.is.fischer@web.de> Message-ID: >> But isn't the type of 'dummy' defined by 'return dummy' beeing >> a possible return value (and, so, dummy :: b)? > No. let-bindings are polymorphic, so dummy :: forall a. a Oh, I once learned that some code of mine didn't work because lambda bindings are monomorphic. Now I can use it for good. genericCast :: (Storable a, Storable b) => a -> IO b genericCast v = return undefined >>= \dummy -> allocaBytes (max (sizeOf v) (sizeOf dummy)) $ \p -> poke p v >> if False then return dummy else peek (castPtr p) In ghci: > let a = 4 :: WordPtr > b <- genericCast a :: IO (Ptr ()) > b 0x00000004 Thanks! Maur?cio From john.moore54 at gmail.com Tue Oct 27 08:39:55 2009 From: john.moore54 at gmail.com (John Moore) Date: Tue Oct 27 08:16:28 2009 Subject: [Haskell-beginners] nim Message-ID: <4f7ad1ad0910270539n20f3f7b8jb22231459ae3f2b2@mail.gmail.com> Hi getting there with nimprogram well have it working in different areas. The last part is where the most trouble is import Control.Monad import System.Random initNim :: IO [Int] initNim = replicateM 3 $ randomRIO (1,10)--- This get the random numbers data PileName = A | B | C deriving (Show, Eq, Read) typeOfMove :: (PileName, Int) -> [Int] -> [Int] typeOfMove (A, x) xs = zipWith (-) xs [x,0,0] typeOfMove (B, x) xs = zipWith (-) xs [0,x,0] typeOfMove (C, x) xs = zipWith (-) xs [0,0,x] main :: IO () main = do putStrLn "Which pile A, B, or C ?" x <- readLn putStrLn "How many stones?" y <- readLn let z = typeOfMove (x,y) [9,9,9]-- cannot get the random numbers here putStrLn . show $ z *This is where the main problem is I 'm trying to run the game?* play nim = do z <- getLine newAnswer <- return (diff z) if newAnswer == [0,0,1]||[0,1,0]||[1,0,0] then putStrn "You win" else play nim newAnswer diff z ws hs =[ if z==w then w else h]-- trying to return different list here John -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20091027/7aca1600/attachment.html From deniz.a.m.dogan at gmail.com Tue Oct 27 08:49:10 2009 From: deniz.a.m.dogan at gmail.com (Deniz Dogan) Date: Tue Oct 27 08:26:02 2009 Subject: [Haskell-beginners] nim In-Reply-To: <4f7ad1ad0910270539n20f3f7b8jb22231459ae3f2b2@mail.gmail.com> References: <4f7ad1ad0910270539n20f3f7b8jb22231459ae3f2b2@mail.gmail.com> Message-ID: <7b501d5c0910270549ibbff6ddq66b40a8d32404374@mail.gmail.com> 2009/10/27 John Moore : > Hi getting there with nimprogram well have it working in different areas. > > initNim :: IO [Int] > initNim = replicateM 3 $ randomRIO (1,10)--- This get the random numbers > data PileName = A | B | C deriving (Show, Eq, Read) > > main = do > ??? putStrLn "Which pile A, B, or C ?" > ??? x <- readLn > ??? putStrLn "How many stones?" > ??? y <- readLn > ??? let z = typeOfMove (x,y) [9,9,9]-- cannot get the random numbers here > ??? putStrLn . show $ z If I understand this part of your problem correctly, try this: main = do putStrLn "Which pile A, B, or C ?" x <- readLn putStrLn "How many stones?" y <- readLn nums <- initNim -- Get the three random numbers let z = typeOfMove (x,y) nums -- Pass them along putStrLn . show $ z The same effect can be achieved in a number of different ways, but this should be fairly clear. (I haven't tested this by the way, so excuse any typos and bugs.) -- Deniz Dogan From mhamro at gmail.com Tue Oct 27 10:31:15 2009 From: mhamro at gmail.com (MH) Date: Tue Oct 27 10:07:48 2009 Subject: [Haskell-beginners] unix pkg on cygwin Message-ID: <648da0750910270731w5a1a1790x6a621068d08d0a8b@mail.gmail.com> I tried to use unix package on Cygwin and it didn't work. I found a far better solution, use Sun's VirtualBox from virtualbox.org. It runs a virtual OS on your host operating system and it works very well. I installed virtualbox on Windows host, and I am running Ubuntu inside virtualbox with Haskell on it. Works like charm. Vitualbox like VMWare, except it is free. Malik From orclev at gmail.com Tue Oct 27 11:39:52 2009 From: orclev at gmail.com (Kyle Murphy) Date: Tue Oct 27 11:16:25 2009 Subject: [Haskell-beginners] nim In-Reply-To: <4f7ad1ad0910270539n20f3f7b8jb22231459ae3f2b2@mail.gmail.com> References: <4f7ad1ad0910270539n20f3f7b8jb22231459ae3f2b2@mail.gmail.com> Message-ID: <2db78cee0910270839q46611323n5ba7c1f30b4356a7@mail.gmail.com> I'm still not sure you're thinking about this the right way. Maybe it would help to see a working version (although not a *good* working version). This e-mail is a literate haskell program, copy everything from the next line till the EOF into nim.lhs and run it. ---- Import the modules we need. > import Control.Monad > import System.Random Define the types we're going to be using. > data PileName = A | B | C deriving (Show, Eq, Read) > type State = [Int] > type Move = (PileName, Int) initNim setups a new random game State. > initNim :: IO State > initNim = replicateM 3 $ randomRIO (1,10) doMove takes a Move, and a initial State and returns the updated game State after the Move. > doMove :: Move -> State -> State > doMove (A,x) xs = zipWith (-) xs [x,0,0] > doMove (B,x) xs = zipWith (-) xs [0,x,0] > doMove (C,x) xs = zipWith (-) xs [0,0,x] getMove returns the next Move. > getMove :: IO Move > getMove = do > putStrLn "Which pile A, B, or C?" > x <- readLn > putStrLn "How many stones?" > y <- readLn > return (x,y) checkWin takes a State and returns whether the game has been won or not. > checkWin :: State -> Bool > checkWin [1,0,0] = True > checkWin [0,1,0] = True > checkWin [0,0,1] = True > checkWin _ = False doRound takes the current game State, performs one round of play, and returns the updated State. > doRound :: State -> IO State > doRound oldState = do > putStrLn . show $ oldState > move <- getMove > let newState = doMove move oldState > return newState untilM is a little helper function. To understand what it does take a look at the until function defined in Prelude. untilM does the same thing but inside of a Monad (in this case IO). The short version is that it runs the second function it's given until the first function returns True. > untilM :: (Mona m) => (a -> Bool) -> (a -> m a) -> a -> m a > untilM p f x | p x = return x | otherwise = f x >>= untilM p f The main ties everything together by initializing the starting state, and then kicking off the doRound loop (via untilM). > main :: IO () > main = do > state <- initNim > untilM checkWin doRound state > putStrLn "You win!" Areas for improvement include the following: No error checking on user input, if you enter an invalid pile or something that isn't a number (or is more then the number of stones in the pile) bad things happen (hint: lookup try catch and exception handling tutorials) The checkWin function, the doMove, and the doRound function don't do any sort of check to make sure the number of stones removed can actually be removed (that is, none of the piles go negative), nor is there a check done for if *all* stones get removed. It might be educational to change the type of State from [Int] to (Int, [Int]) with the first value being the number of the player who's turn it is. Adjust the various functions to accept the new type of State and to update it appropriately. ---- EOF -R. Kyle Murphy -- Curiosity was framed, Ignorance killed the cat. On Tue, Oct 27, 2009 at 08:39, John Moore wrote: > Hi getting there with nimprogram well have it working in different areas. > > The last part is where the most trouble is > > import Control.Monad > > import System.Random > > > > initNim :: IO [Int] > > initNim = replicateM 3 $ randomRIO (1,10)--- This get the random numbers > > > > > > data PileName = A | B | C deriving (Show, Eq, Read) > > > > typeOfMove :: (PileName, Int) -> [Int] -> [Int] > > typeOfMove (A, x) xs = zipWith (-) xs [x,0,0] > > typeOfMove (B, x) xs = zipWith (-) xs [0,x,0] > > typeOfMove (C, x) xs = zipWith (-) xs [0,0,x] > > > > main :: IO () > > main = do > > putStrLn "Which pile A, B, or C ?" > > x <- readLn > > putStrLn "How many stones?" > > y <- readLn > > let z = typeOfMove (x,y) [9,9,9]-- cannot get the random numbers here > > putStrLn . show $ z > > > > > > *This is where the main problem is I 'm trying to run the game?* > > play nim = do > > z <- getLine > > newAnswer <- return (diff z) > > if newAnswer == [0,0,1]||[0,1,0]||[1,0,0] > > then putStrn "You win" > > else play nim newAnswer > > > > diff z ws hs =[ if z==w then w else h]-- trying to return different list > here > > > > > > John > > _______________________________________________ > 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/20091027/1af0874f/attachment-0001.html From mpm at alumni.caltech.edu Tue Oct 27 20:23:00 2009 From: mpm at alumni.caltech.edu (Michael Mossey) Date: Tue Oct 27 19:59:33 2009 Subject: [Haskell-beginners] nim In-Reply-To: <4f7ad1ad0910270539n20f3f7b8jb22231459ae3f2b2@mail.gmail.com> References: <4f7ad1ad0910270539n20f3f7b8jb22231459ae3f2b2@mail.gmail.com> Message-ID: <4AE78EE4.8050307@alumni.caltech.edu> I still think you should get a one-pile version working first, with no randomness. You are wrestling with manipulation of lists and random numbers, but you could create a version that has none of that: just a loop to prompt for a number and subtract and print it. From iaefai at me.com Wed Oct 28 01:56:07 2009 From: iaefai at me.com (=?iso-8859-1?Q?i=E6fai?=) Date: Wed Oct 28 01:32:39 2009 Subject: [Haskell-beginners] =?windows-1252?q?Chessboard_Module=2C_opinion?= =?windows-1252?q?s_on=85?= Message-ID: I have just recently finished a 'ChessBoard' module that is meant to represent a chess board. I could use some opinions and/or suggestions on the module. To give an example of how this can be used right now, and was my immediate goal, you can do this: *ChessBoard> putStr $ cout defaultBoard +----+----+----+----+----+----+----+----+ | RB | NB | BB | QB | KB | BB | NB | RB | +----+----+----+----+----+----+----+----+ | PB | PB | PB | PB | PB | PB | PB | PB | +----+----+----+----+----+----+----+----+ | | | | | | | | | +----+----+----+----+----+----+----+----+ | | | | | | | | | +----+----+----+----+----+----+----+----+ | | | | | | | | | +----+----+----+----+----+----+----+----+ | | | | | | | | | +----+----+----+----+----+----+----+----+ | PW | PW | PW | PW | PW | PW | PW | PW | +----+----+----+----+----+----+----+----+ | RW | NW | BW | QW | KW | BW | NW | RW | +----+----+----+----+----+----+----+----+ I have not determined exactly how I will be making moves, but the logic will not be in my program. I am going to be using a chess engine in another process (I haven't chosen a chess engine yet that works on both windows and mac through stdin/stdout). The module itself follows, I appreciate any thoughts you might have. module ChessBoard where import Data.Sequence import Data.Foldable import Data.Maybe import Data.List as List class NiceLook a where cout :: a -> String data Piece = Bishop | Rook | Knight | King | Queen | Pawn | NoPiece deriving (Show, Eq) instance NiceLook Piece where cout Bishop = "B" cout Rook = "R" cout Knight = "N" cout Queen = "Q" cout Pawn = "P" cout King = "K" cout _ = " " data Colour = Black | White | NoColour deriving (Show, Eq) instance NiceLook Colour where cout Black = "B" cout White = "W" cout NoColour = " " -- error "..." might be useful data Square = Square Piece Colour deriving (Show, Eq) instance NiceLook (Square) where cout (Square p c) = (cout p) ++ (cout c) data Row = Row (Seq Square) deriving (Show, Eq) instance NiceLook (Row) where cout (Row s) = "|" ++ foldMap (\x -> " " ++ cout x ++ " |") s -- thnx Saizan makeRow n = case (List.length n) of 8 -> Row (fromList n) _ -> error "Row is not 8 squares" makeColouredSquares n c = makeRow $ map makeSquare (zip n (replicate 8 c)) makeSquare (n,c) = Square n c pawns = [Pawn, Pawn, Pawn, Pawn, Pawn, Pawn, Pawn, Pawn] back = [Rook, Knight, Bishop, Queen, King, Bishop, Knight, Rook] blank = [NoPiece, NoPiece, NoPiece, NoPiece, NoPiece, NoPiece, NoPiece, NoPiece] data Board = Board (Seq Row) deriving (Show, Eq) instance NiceLook (Board) where cout (Board c) = borderOutput ++ "\n" ++ (foldMap (\x -> cout x + + "\n" ++ borderOutput ++ "\n") c) defaultBoard = Board (makeColouredSquares back Black <| makeColouredSquares pawns Black <| makeColouredSquares blank NoColour <| makeColouredSquares blank NoColour <| makeColouredSquares blank NoColour <| makeColouredSquares blank NoColour <| makeColouredSquares pawns White <| makeColouredSquares back White <| empty) borderOutput = "+" ++ (List.foldr1 (++) $ replicate 8 "----+") From jfredett at gmail.com Wed Oct 28 02:11:26 2009 From: jfredett at gmail.com (Joe Fredette) Date: Wed Oct 28 01:47:59 2009 Subject: =?WINDOWS-1252?Q?Re:_[Haskell-beginners]_Chessboard_Module,_opin?= =?WINDOWS-1252?Q?ions_on=85?= In-Reply-To: References: Message-ID: <383522F2-1A56-4F9E-9C65-426CE2AFBBF8@gmail.com> Awesome, have you cabal-ized it? If not, it's pretty simple (look up 'cabal' on the haskellwiki). Then you can upload it to hackage to be toyed with. One thing that might be a cool direction to go w/ your project (sounds like you intend to make a chess playing program, this is somewhat orthogonal to that goal) is to build a "playback" machine. For instance, I play chess with people by email on a fairly regular basis. Specifically, we submit moves to one another in semi-standard[1] algebraic chess notation. So I might see a game like: 1. Kb3 e5 2. d3 d6 ... n. a4->a5 e6->d7 Where the first move is White, moving his knight to B-3, then black moves his pawn from e7 to e5. etc. a move followed by a * is a check, followed by two stars is a mate. etc. You can poke at the wiki page for ACN for the appropriate syntax. My suggestion is that- often times we go many days in between moves, and so I don't keep track (in my head) of the last few moves he made, which can sometimes indicate weak points/general strategies. It would be _really_ nice to be able to replay old board positions at will, given this ACN notation of the game. Might be a nice (simple) use case for Parsec, and I imagine that most chess engines will have something like that (assuming they operate on STDIN/OUT) -- even if the syntax may be different. This will give you the "backend" to plug it onto anyway. Anywho, good luck with your project, it looks nice! /Joe PS, Just noticed the little function you use to display the board (and stuff). You may want to poke around the 2d Pretty printers on hackage, they may make it easier/more extensible to render the board. Also, `cout`? Someone's got a bit o' the ++ in 'em... :) [1] Okay, we mostly make it up, but it's _consistently_ arbitrary... On Oct 28, 2009, at 1:56 AM, i?fai wrote: > > I have just recently finished a 'ChessBoard' module that is meant to > represent a chess board. I could use some opinions and/or > suggestions on the module. > > To give an example of how this can be used right now, and was my > immediate goal, you can do this: > > *ChessBoard> putStr $ cout defaultBoard > +----+----+----+----+----+----+----+----+ > | RB | NB | BB | QB | KB | BB | NB | RB | > +----+----+----+----+----+----+----+----+ > | PB | PB | PB | PB | PB | PB | PB | PB | > +----+----+----+----+----+----+----+----+ > | | | | | | | | | > +----+----+----+----+----+----+----+----+ > | | | | | | | | | > +----+----+----+----+----+----+----+----+ > | | | | | | | | | > +----+----+----+----+----+----+----+----+ > | | | | | | | | | > +----+----+----+----+----+----+----+----+ > | PW | PW | PW | PW | PW | PW | PW | PW | > +----+----+----+----+----+----+----+----+ > | RW | NW | BW | QW | KW | BW | NW | RW | > +----+----+----+----+----+----+----+----+ > > I have not determined exactly how I will be making moves, but the > logic will not be in my program. I am going to be using a chess > engine in another process (I haven't chosen a chess engine yet that > works on both windows and mac through stdin/stdout). > > The module itself follows, I appreciate any thoughts you might have. > > > module ChessBoard where > > import Data.Sequence > import Data.Foldable > import Data.Maybe > import Data.List as List > > class NiceLook a where > cout :: a -> String > > > data Piece = Bishop | Rook | Knight | King | Queen | Pawn | NoPiece > deriving (Show, Eq) > > instance NiceLook Piece where > cout Bishop = "B" > cout Rook = "R" > cout Knight = "N" > cout Queen = "Q" > cout Pawn = "P" > cout King = "K" > cout _ = " " > > data Colour = Black | White | NoColour > deriving (Show, Eq) > > instance NiceLook Colour where > cout Black = "B" > cout White = "W" > cout NoColour = " " > > -- error "..." might be useful > > data Square = Square Piece Colour > deriving (Show, Eq) > > instance NiceLook (Square) where > cout (Square p c) = (cout p) ++ (cout c) > > data Row = Row (Seq Square) > deriving (Show, Eq) > > instance NiceLook (Row) where > cout (Row s) = "|" ++ foldMap (\x -> " " ++ cout x ++ " |") > s -- thnx Saizan > > makeRow n = case (List.length n) of > 8 -> Row (fromList n) > _ -> error "Row is not 8 squares" > > makeColouredSquares n c = makeRow $ map makeSquare (zip n (replicate > 8 c)) > > makeSquare (n,c) = Square n c > > pawns = [Pawn, Pawn, Pawn, Pawn, Pawn, Pawn, Pawn, Pawn] > back = [Rook, Knight, Bishop, Queen, King, Bishop, Knight, Rook] > blank = [NoPiece, NoPiece, NoPiece, NoPiece, NoPiece, NoPiece, > NoPiece, NoPiece] > > data Board = Board (Seq Row) > deriving (Show, Eq) > > instance NiceLook (Board) where > cout (Board c) = borderOutput ++ "\n" ++ (foldMap (\x -> cout x + > + "\n" ++ borderOutput ++ "\n") c) > > defaultBoard = Board (makeColouredSquares back Black <| > makeColouredSquares pawns Black <| > makeColouredSquares blank NoColour <| > makeColouredSquares blank NoColour <| > makeColouredSquares blank NoColour <| > makeColouredSquares blank NoColour <| > makeColouredSquares pawns White <| > makeColouredSquares back White <| empty) > > > borderOutput = "+" ++ (List.foldr1 (++) $ replicate 8 "----+") > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners From iaefai at me.com Wed Oct 28 02:33:30 2009 From: iaefai at me.com (=?iso-8859-1?Q?i=E6fai?=) Date: Wed Oct 28 02:10:08 2009 Subject: =?windows-1252?Q?Re:_[Haskell-beginners]_Chessboard_Module,_opin?= =?windows-1252?Q?ions_on=85?= In-Reply-To: <383522F2-1A56-4F9E-9C65-426CE2AFBBF8@gmail.com> References: <383522F2-1A56-4F9E-9C65-426CE2AFBBF8@gmail.com> Message-ID: If I may be so bold, this project is much more interesting than you might suspect. This is of course only the first part, but the next step is to have an opengl display (I hope to get something running on mac and windows) going and it must support opengl 1.4 due to some limitations I have. The chess AI process is something I still have to hunt for mind you, but the part that is the most interesting is that I am going to be controlling a $50,000 robot with this in class :P. This robot is a CRS-3000 I believe, it looks something like this: http://www.phym.sdu.edu.cn/rolf/image/arm_overview.jpg and it is picking up real chess pieces at the direction of the user on screen. The communication with the robot is going to be over the serial port. An interesting problem related to this is communication, luckily I have tested a serial port library that does work on windows with ghc. I will probably implement a program on the robot's 486 controller to instruct the robot on what to do specifically. My next step that could definitely use some direction would be the display part. I am limited to using power of 2 textures due to some unfortunate limitations on the machines I have available. I am thinking about this from a layered display approach. So I would be able to have a layer that would be the chess board with some interaction. Another layer would help calibrate the robot positions (luckily I am using only 4 and interpolating the rest - I figured out how to do that with some effort on Friday). I would probably start using glut for this, and hack together something, but I would imagine what I am speaking of would benefit very much from some of what haskell can do. There might even be a library that already exists that I might not have found yet. - i?fai On 2009-10-28, at 2:11 AM, Joe Fredette wrote: > Awesome, have you cabal-ized it? If not, it's pretty simple (look up > 'cabal' on the haskellwiki). Then you can upload it to hackage to be > toyed with. > > One thing that might be a cool direction to go w/ your project > (sounds like you intend to make a chess playing program, this is > somewhat orthogonal to that goal) is to build a "playback" machine. > For instance, I play chess with people by email on a fairly regular > basis. Specifically, we submit moves to one another in semi-standard > [1] algebraic chess notation. So I might see a game like: > > > 1. Kb3 e5 > 2. d3 d6 > ... > n. a4->a5 e6->d7 > > Where the first move is White, moving his knight to B-3, then black > moves his pawn from e7 to e5. etc. > a move followed by a * is a check, followed by two stars is a mate. > etc. You can poke at the wiki page for ACN for the appropriate > syntax. My suggestion is that- often times we go many days in > between moves, and so I don't keep track (in my head) of the last > few moves he made, which can sometimes indicate weak points/general > strategies. It would be _really_ nice to be able to replay old board > positions at will, given this ACN notation of the game. Might be a > nice (simple) use case for Parsec, and I imagine that most chess > engines will have something like that (assuming they operate on > STDIN/OUT) -- even if the syntax may be different. This will give > you the "backend" to plug it onto anyway. > > Anywho, good luck with your project, it looks nice! > > /Joe > > PS, Just noticed the little function you use to display the board > (and stuff). You may want to poke around the 2d Pretty printers on > hackage, they may make it easier/more extensible to render the > board. Also, `cout`? Someone's got a bit o' the ++ in 'em... :) > > > > [1] Okay, we mostly make it up, but it's _consistently_ arbitrary... > > On Oct 28, 2009, at 1:56 AM, i?fai wrote: > >> >> I have just recently finished a 'ChessBoard' module that is meant >> to represent a chess board. I could use some opinions and/or >> suggestions on the module. >> >> To give an example of how this can be used right now, and was my >> immediate goal, you can do this: >> >> *ChessBoard> putStr $ cout defaultBoard >> +----+----+----+----+----+----+----+----+ >> | RB | NB | BB | QB | KB | BB | NB | RB | >> +----+----+----+----+----+----+----+----+ >> | PB | PB | PB | PB | PB | PB | PB | PB | >> +----+----+----+----+----+----+----+----+ >> | | | | | | | | | >> +----+----+----+----+----+----+----+----+ >> | | | | | | | | | >> +----+----+----+----+----+----+----+----+ >> | | | | | | | | | >> +----+----+----+----+----+----+----+----+ >> | | | | | | | | | >> +----+----+----+----+----+----+----+----+ >> | PW | PW | PW | PW | PW | PW | PW | PW | >> +----+----+----+----+----+----+----+----+ >> | RW | NW | BW | QW | KW | BW | NW | RW | >> +----+----+----+----+----+----+----+----+ >> >> I have not determined exactly how I will be making moves, but the >> logic will not be in my program. I am going to be using a chess >> engine in another process (I haven't chosen a chess engine yet that >> works on both windows and mac through stdin/stdout). >> >> The module itself follows, I appreciate any thoughts you might have. >> >> >> module ChessBoard where >> >> import Data.Sequence >> import Data.Foldable >> import Data.Maybe >> import Data.List as List >> >> class NiceLook a where >> cout :: a -> String >> >> >> data Piece = Bishop | Rook | Knight | King | Queen | Pawn | NoPiece >> deriving (Show, Eq) >> >> instance NiceLook Piece where >> cout Bishop = "B" >> cout Rook = "R" >> cout Knight = "N" >> cout Queen = "Q" >> cout Pawn = "P" >> cout King = "K" >> cout _ = " " >> >> data Colour = Black | White | NoColour >> deriving (Show, Eq) >> >> instance NiceLook Colour where >> cout Black = "B" >> cout White = "W" >> cout NoColour = " " >> >> -- error "..." might be useful >> >> data Square = Square Piece Colour >> deriving (Show, Eq) >> >> instance NiceLook (Square) where >> cout (Square p c) = (cout p) ++ (cout c) >> >> data Row = Row (Seq Square) >> deriving (Show, Eq) >> >> instance NiceLook (Row) where >> cout (Row s) = "|" ++ foldMap (\x -> " " ++ cout x ++ " |") >> s -- thnx Saizan >> >> makeRow n = case (List.length n) of >> 8 -> Row (fromList n) >> _ -> error "Row is not 8 squares" >> >> makeColouredSquares n c = makeRow $ map makeSquare (zip n >> (replicate 8 c)) >> >> makeSquare (n,c) = Square n c >> >> pawns = [Pawn, Pawn, Pawn, Pawn, Pawn, Pawn, Pawn, Pawn] >> back = [Rook, Knight, Bishop, Queen, King, Bishop, Knight, Rook] >> blank = [NoPiece, NoPiece, NoPiece, NoPiece, NoPiece, NoPiece, >> NoPiece, NoPiece] >> >> data Board = Board (Seq Row) >> deriving (Show, Eq) >> >> instance NiceLook (Board) where >> cout (Board c) = borderOutput ++ "\n" ++ (foldMap (\x -> cout x + >> + "\n" ++ borderOutput ++ "\n") c) >> >> defaultBoard = Board (makeColouredSquares back Black <| >> makeColouredSquares pawns Black <| >> makeColouredSquares blank NoColour <| >> makeColouredSquares blank NoColour <| >> makeColouredSquares blank NoColour <| >> makeColouredSquares blank NoColour <| >> makeColouredSquares pawns White <| >> makeColouredSquares back White <| empty) >> >> >> borderOutput = "+" ++ (List.foldr1 (++) $ replicate 8 "----+") >> >> _______________________________________________ >> Beginners mailing list >> Beginners@haskell.org >> http://www.haskell.org/mailman/listinfo/beginners > From tonymorris at gmail.com Wed Oct 28 04:00:47 2009 From: tonymorris at gmail.com (Tony Morris) Date: Wed Oct 28 03:37:15 2009 Subject: =?ISO-8859-1?Q?Re=3A_=5BHaskell-beginners=5D_Chessboard_Mo?= =?ISO-8859-1?Q?dule=2C_opinions_on=2E=2E=2E?= In-Reply-To: <383522F2-1A56-4F9E-9C65-426CE2AFBBF8@gmail.com> References: <383522F2-1A56-4F9E-9C65-426CE2AFBBF8@gmail.com> Message-ID: <4AE7FA2F.3030809@gmail.com> Nitpicking, a white knight cannot move to b3 on its first move. Kb3 denotes King to b3 which is not a possible first move. Nb3 is the correct notation for Knight to b3. Possible first moves for a white knight are Na3 Nc3 Nf3 and Nh3. Players in chess tournaments are required to notate their games using algebraic notation and also as a result of training/reading, the notation rolls off one's tongue. Joe Fredette wrote: > Awesome, have you cabal-ized it? If not, it's pretty simple (look up > 'cabal' on the haskellwiki). Then you can upload it to hackage to be > toyed with. > > One thing that might be a cool direction to go w/ your project (sounds > like you intend to make a chess playing program, this is somewhat > orthogonal to that goal) is to build a "playback" machine. For > instance, I play chess with people by email on a fairly regular basis. > Specifically, we submit moves to one another in semi-standard[1] > algebraic chess notation. So I might see a game like: > > > 1. Kb3 e5 > 2. d3 d6 > ... > n. a4->a5 e6->d7 > > Where the first move is White, moving his knight to B-3, then black > moves his pawn from e7 to e5. etc. > a move followed by a * is a check, followed by two stars is a mate. > etc. You can poke at the wiki page for ACN for the appropriate syntax. > My suggestion is that- often times we go many days in between moves, > and so I don't keep track (in my head) of the last few moves he made, > which can sometimes indicate weak points/general strategies. It would > be _really_ nice to be able to replay old board positions at will, > given this ACN notation of the game. Might be a nice (simple) use case > for Parsec, and I imagine that most chess engines will have something > like that (assuming they operate on STDIN/OUT) -- even if the syntax > may be different. This will give you the "backend" to plug it onto > anyway. > > Anywho, good luck with your project, it looks nice! > > /Joe > > PS, Just noticed the little function you use to display the board (and > stuff). You may want to poke around the 2d Pretty printers on hackage, > they may make it easier/more extensible to render the board. Also, > `cout`? Someone's got a bit o' the ++ in 'em... :) > > > > [1] Okay, we mostly make it up, but it's _consistently_ arbitrary... > > On Oct 28, 2009, at 1:56 AM, i?fai wrote: > >> >> I have just recently finished a 'ChessBoard' module that is meant to >> represent a chess board. I could use some opinions and/or suggestions >> on the module. >> >> To give an example of how this can be used right now, and was my >> immediate goal, you can do this: >> >> *ChessBoard> putStr $ cout defaultBoard >> +----+----+----+----+----+----+----+----+ >> | RB | NB | BB | QB | KB | BB | NB | RB | >> +----+----+----+----+----+----+----+----+ >> | PB | PB | PB | PB | PB | PB | PB | PB | >> +----+----+----+----+----+----+----+----+ >> | | | | | | | | | >> +----+----+----+----+----+----+----+----+ >> | | | | | | | | | >> +----+----+----+----+----+----+----+----+ >> | | | | | | | | | >> +----+----+----+----+----+----+----+----+ >> | | | | | | | | | >> +----+----+----+----+----+----+----+----+ >> | PW | PW | PW | PW | PW | PW | PW | PW | >> +----+----+----+----+----+----+----+----+ >> | RW | NW | BW | QW | KW | BW | NW | RW | >> +----+----+----+----+----+----+----+----+ >> >> I have not determined exactly how I will be making moves, but the >> logic will not be in my program. I am going to be using a chess >> engine in another process (I haven't chosen a chess engine yet that >> works on both windows and mac through stdin/stdout). >> >> The module itself follows, I appreciate any thoughts you might have. >> >> >> module ChessBoard where >> >> import Data.Sequence >> import Data.Foldable >> import Data.Maybe >> import Data.List as List >> >> class NiceLook a where >> cout :: a -> String >> >> >> data Piece = Bishop | Rook | Knight | King | Queen | Pawn | NoPiece >> deriving (Show, Eq) >> >> instance NiceLook Piece where >> cout Bishop = "B" >> cout Rook = "R" >> cout Knight = "N" >> cout Queen = "Q" >> cout Pawn = "P" >> cout King = "K" >> cout _ = " " >> >> data Colour = Black | White | NoColour >> deriving (Show, Eq) >> >> instance NiceLook Colour where >> cout Black = "B" >> cout White = "W" >> cout NoColour = " " >> >> -- error "..." might be useful >> >> data Square = Square Piece Colour >> deriving (Show, Eq) >> >> instance NiceLook (Square) where >> cout (Square p c) = (cout p) ++ (cout c) >> >> data Row = Row (Seq Square) >> deriving (Show, Eq) >> >> instance NiceLook (Row) where >> cout (Row s) = "|" ++ foldMap (\x -> " " ++ cout x ++ " |") >> s -- thnx Saizan >> >> makeRow n = case (List.length n) of >> 8 -> Row (fromList n) >> _ -> error "Row is not 8 squares" >> >> makeColouredSquares n c = makeRow $ map makeSquare (zip n (replicate >> 8 c)) >> >> makeSquare (n,c) = Square n c >> >> pawns = [Pawn, Pawn, Pawn, Pawn, Pawn, Pawn, Pawn, Pawn] >> back = [Rook, Knight, Bishop, Queen, King, Bishop, Knight, Rook] >> blank = [NoPiece, NoPiece, NoPiece, NoPiece, NoPiece, NoPiece, >> NoPiece, NoPiece] >> >> data Board = Board (Seq Row) >> deriving (Show, Eq) >> >> instance NiceLook (Board) where >> cout (Board c) = borderOutput ++ "\n" ++ (foldMap (\x -> cout x ++ >> "\n" ++ borderOutput ++ "\n") c) >> >> defaultBoard = Board (makeColouredSquares back Black <| >> makeColouredSquares pawns Black <| >> makeColouredSquares blank NoColour <| >> makeColouredSquares blank NoColour <| >> makeColouredSquares blank NoColour <| >> makeColouredSquares blank NoColour <| >> makeColouredSquares pawns White <| >> makeColouredSquares back White <| empty) >> >> >> borderOutput = "+" ++ (List.foldr1 (++) $ replicate 8 "----+") >> >> _______________________________________________ >> Beginners mailing list >> Beginners@haskell.org >> http://www.haskell.org/mailman/listinfo/beginners > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > -- Tony Morris http://tmorris.net/ From andy.elvey at paradise.net.nz Wed Oct 28 04:23:49 2009 From: andy.elvey at paradise.net.nz (Andy Elvey) Date: Wed Oct 28 04:00:21 2009 Subject: =?windows-1252?Q?Re=3A_=5BHaskell-beginners=5D_Chessboard_?= =?windows-1252?Q?Module=2C_opinions_on=85?= In-Reply-To: References: Message-ID: <4AE7FF95.6090800@paradise.net.nz> i?fai wrote: > > I have just recently finished a 'ChessBoard' module that is meant to > represent a chess board. I could use some opinions and/or suggestions > on the module. > > To give an example of how this can be used right now, and was my > immediate goal, you can do this: > > *ChessBoard> putStr $ cout defaultBoard > +----+----+----+----+----+----+----+----+ > | RB | NB | BB | QB | KB | BB | NB | RB | > +----+----+----+----+----+----+----+----+ > | PB | PB | PB | PB | PB | PB | PB | PB | > +----+----+----+----+----+----+----+----+ > | | | | | | | | | > +----+----+----+----+----+----+----+----+ > | | | | | | | | | > +----+----+----+----+----+----+----+----+ > | | | | | | | | | > +----+----+----+----+----+----+----+----+ > | | | | | | | | | > +----+----+----+----+----+----+----+----+ > | PW | PW | PW | PW | PW | PW | PW | PW | > +----+----+----+----+----+----+----+----+ > | RW | NW | BW | QW | KW | BW | NW | RW | > +----+----+----+----+----+----+----+----+ > > I have not determined exactly how I will be making moves, but the > logic will not be in my program. I am going to be using a chess engine > in another process (I haven't chosen a chess engine yet that works on > both windows and mac through stdin/stdout). > > The module itself follows, I appreciate any thoughts you might have. > > > module ChessBoard where > > import Data.Sequence > import Data.Foldable > import Data.Maybe > import Data.List as List > > class NiceLook a where > cout :: a -> String > > > data Piece = Bishop | Rook | Knight | King | Queen | Pawn | NoPiece > deriving (Show, Eq) > > instance NiceLook Piece where > cout Bishop = "B" > cout Rook = "R" > cout Knight = "N" > cout Queen = "Q" > cout Pawn = "P" > cout King = "K" > cout _ = " " > > data Colour = Black | White | NoColour > deriving (Show, Eq) > > instance NiceLook Colour where > cout Black = "B" > cout White = "W" > cout NoColour = " " > > -- error "..." might be useful > > data Square = Square Piece Colour > deriving (Show, Eq) > > instance NiceLook (Square) where > cout (Square p c) = (cout p) ++ (cout c) > > data Row = Row (Seq Square) > deriving (Show, Eq) > > instance NiceLook (Row) where > cout (Row s) = "|" ++ foldMap (\x -> " " ++ cout x ++ " |") > s -- thnx Saizan > > makeRow n = case (List.length n) of > 8 -> Row (fromList n) > _ -> error "Row is not 8 squares" > > makeColouredSquares n c = makeRow $ map makeSquare (zip n (replicate 8 > c)) > > makeSquare (n,c) = Square n c > > pawns = [Pawn, Pawn, Pawn, Pawn, Pawn, Pawn, Pawn, Pawn] > back = [Rook, Knight, Bishop, Queen, King, Bishop, Knight, Rook] > blank = [NoPiece, NoPiece, NoPiece, NoPiece, NoPiece, NoPiece, > NoPiece, NoPiece] > > data Board = Board (Seq Row) > deriving (Show, Eq) > > instance NiceLook (Board) where > cout (Board c) = borderOutput ++ "\n" ++ (foldMap (\x -> cout x ++ > "\n" ++ borderOutput ++ "\n") c) > > defaultBoard = Board (makeColouredSquares back Black <| > makeColouredSquares pawns Black <| > makeColouredSquares blank NoColour <| > makeColouredSquares blank NoColour <| > makeColouredSquares blank NoColour <| > makeColouredSquares blank NoColour <| > makeColouredSquares pawns White <| > makeColouredSquares back White <| empty) > > > borderOutput = "+" ++ (List.foldr1 (++) $ replicate 8 "----+") > > Hi i?fai! This is great! Very nicely done! I was just wondering - I potter around with crosstab code in several programming languages, and this (the table-creation code in particular) could be quite handy in that area. So, would you mind if I used this? I'll give credit of course! Many thanks for posting this neat bit of code! - Andy From Christian.Maeder at dfki.de Wed Oct 28 07:52:41 2009 From: Christian.Maeder at dfki.de (Christian Maeder) Date: Wed Oct 28 07:29:10 2009 Subject: [Haskell-beginners] Re: Chessboard Module, opinions on... In-Reply-To: References: Message-ID: <4AE83089.8090803@dfki.de> i?fai schrieb: > > I have just recently finished a 'ChessBoard' module that is meant to > represent a chess board. I could use some opinions and/or suggestions on > the module. > > To give an example of how this can be used right now, and was my > immediate goal, you can do this: > > *ChessBoard> putStr $ cout defaultBoard > +----+----+----+----+----+----+----+----+ > | RB | NB | BB | QB | KB | BB | NB | RB | > +----+----+----+----+----+----+----+----+ > | PB | PB | PB | PB | PB | PB | PB | PB | > +----+----+----+----+----+----+----+----+ > | | | | | | | | | > +----+----+----+----+----+----+----+----+ > | | | | | | | | | > +----+----+----+----+----+----+----+----+ > | | | | | | | | | > +----+----+----+----+----+----+----+----+ > | | | | | | | | | > +----+----+----+----+----+----+----+----+ > | PW | PW | PW | PW | PW | PW | PW | PW | > +----+----+----+----+----+----+----+----+ > | RW | NW | BW | QW | KW | BW | NW | RW | > +----+----+----+----+----+----+----+----+ nice! > data Piece = Bishop | Rook | Knight | King | Queen | Pawn | NoPiece > deriving (Show, Eq) > > instance NiceLook Piece where > cout Bishop = "B" > cout Rook = "R" > cout Knight = "N" > cout Queen = "Q" > cout Pawn = "P" > cout King = "K" > cout _ = " " > > data Colour = Black | White | NoColour > deriving (Show, Eq) > > instance NiceLook Colour where > cout Black = "B" > cout White = "W" > cout NoColour = " " > > -- error "..." might be useful > > data Square = Square Piece Colour > deriving (Show, Eq) > > instance NiceLook (Square) where > cout (Square p c) = (cout p) ++ (cout c) I'd omit NoPiece and NoColour in Piece and Colour and add an "Empty" to Square! "cout" (of "nice") for Piece and Colour could then be simply "take 1 . show", if you change Knight to Nknight. nice s = case s of Square p c -> take 1 (show p) ++ take 1 (show c) Empty -> " " Maybe an extra class NiceLook is not even necessary. Cheers Christian From Christian.Maeder at dfki.de Wed Oct 28 07:58:30 2009 From: Christian.Maeder at dfki.de (Christian Maeder) Date: Wed Oct 28 07:35:00 2009 Subject: =?windows-1252?Q?Re=3A_=5BHaskell-beginners=5D_Chessboard_?= =?windows-1252?Q?Module=2C_opinions_on=85?= In-Reply-To: <4AE7FF95.6090800@paradise.net.nz> References: <4AE7FF95.6090800@paradise.net.nz> Message-ID: <4AE831E6.2070309@dfki.de> You may want to look at the tabular package for table-creation code: http://hackage.haskell.org/package/tabular C. Andy Elvey schrieb: > Hi i?fai! This is great! Very nicely done! > I was just wondering - I potter around with crosstab code in several > programming languages, and this (the table-creation code in particular) > could be quite handy in that area. So, would you mind if I used this? > I'll give credit of course! Many thanks for posting this neat bit of > code! - Andy > From wagner.andrew at gmail.com Wed Oct 28 08:00:00 2009 From: wagner.andrew at gmail.com (Andrew Wagner) Date: Wed Oct 28 07:36:29 2009 Subject: =?windows-1252?Q?Re=3A_=5BHaskell=2Dbeginners=5D_Chessboard_Module=2C_opinions_?= =?windows-1252?Q?on=85?= In-Reply-To: <383522F2-1A56-4F9E-9C65-426CE2AFBBF8@gmail.com> References: <383522F2-1A56-4F9E-9C65-426CE2AFBBF8@gmail.com> Message-ID: Just a note to let you know: it's virtually impossible to use a linked-list-of-linked-list or two-dimensional-array board representation as the basis of a serious AI. It's just too inefficient (see http://www.cis.uab.edu/hyatt/boardrep.html for some other options). That said, if you want to use this as the basis of being able to play through games or something, it's great. On Wed, Oct 28, 2009 at 2:11 AM, Joe Fredette wrote: > Awesome, have you cabal-ized it? If not, it's pretty simple (look up > 'cabal' on the haskellwiki). Then you can upload it to hackage to be toyed > with. > > One thing that might be a cool direction to go w/ your project (sounds like > you intend to make a chess playing program, this is somewhat orthogonal to > that goal) is to build a "playback" machine. For instance, I play chess with > people by email on a fairly regular basis. Specifically, we submit moves to > one another in semi-standard[1] algebraic chess notation. So I might see a > game like: > > > 1. Kb3 e5 > 2. d3 d6 > ... > n. a4->a5 e6->d7 > > Where the first move is White, moving his knight to B-3, then black moves > his pawn from e7 to e5. etc. > a move followed by a * is a check, followed by two stars is a mate. etc. > You can poke at the wiki page for ACN for the appropriate syntax. My > suggestion is that- often times we go many days in between moves, and so I > don't keep track (in my head) of the last few moves he made, which can > sometimes indicate weak points/general strategies. It would be _really_ nice > to be able to replay old board positions at will, given this ACN notation of > the game. Might be a nice (simple) use case for Parsec, and I imagine that > most chess engines will have something like that (assuming they operate on > STDIN/OUT) -- even if the syntax may be different. This will give you the > "backend" to plug it onto anyway. > > Anywho, good luck with your project, it looks nice! > > /Joe > > PS, Just noticed the little function you use to display the board (and > stuff). You may want to poke around the 2d Pretty printers on hackage, they > may make it easier/more extensible to render the board. Also, `cout`? > Someone's got a bit o' the ++ in 'em... :) > > > > [1] Okay, we mostly make it up, but it's _consistently_ arbitrary... > > > On Oct 28, 2009, at 1:56 AM, i?fai wrote: > > >> I have just recently finished a 'ChessBoard' module that is meant to >> represent a chess board. I could use some opinions and/or suggestions on the >> module. >> >> To give an example of how this can be used right now, and was my immediate >> goal, you can do this: >> >> *ChessBoard> putStr $ cout defaultBoard >> +----+----+----+----+----+----+----+----+ >> | RB | NB | BB | QB | KB | BB | NB | RB | >> +----+----+----+----+----+----+----+----+ >> | PB | PB | PB | PB | PB | PB | PB | PB | >> +----+----+----+----+----+----+----+----+ >> | | | | | | | | | >> +----+----+----+----+----+----+----+----+ >> | | | | | | | | | >> +----+----+----+----+----+----+----+----+ >> | | | | | | | | | >> +----+----+----+----+----+----+----+----+ >> | | | | | | | | | >> +----+----+----+----+----+----+----+----+ >> | PW | PW | PW | PW | PW | PW | PW | PW | >> +----+----+----+----+----+----+----+----+ >> | RW | NW | BW | QW | KW | BW | NW | RW | >> +----+----+----+----+----+----+----+----+ >> >> I have not determined exactly how I will be making moves, but the logic >> will not be in my program. I am going to be using a chess engine in another >> process (I haven't chosen a chess engine yet that works on both windows and >> mac through stdin/stdout). >> >> The module itself follows, I appreciate any thoughts you might have. >> >> >> module ChessBoard where >> >> import Data.Sequence >> import Data.Foldable >> import Data.Maybe >> import Data.List as List >> >> class NiceLook a where >> cout :: a -> String >> >> >> data Piece = Bishop | Rook | Knight | King | Queen | Pawn | NoPiece >> deriving (Show, Eq) >> >> instance NiceLook Piece where >> cout Bishop = "B" >> cout Rook = "R" >> cout Knight = "N" >> cout Queen = "Q" >> cout Pawn = "P" >> cout King = "K" >> cout _ = " " >> >> data Colour = Black | White | NoColour >> deriving (Show, Eq) >> >> instance NiceLook Colour where >> cout Black = "B" >> cout White = "W" >> cout NoColour = " " >> >> -- error "..." might be useful >> >> data Square = Square Piece Colour >> deriving (Show, Eq) >> >> instance NiceLook (Square) where >> cout (Square p c) = (cout p) ++ (cout c) >> >> data Row = Row (Seq Square) >> deriving (Show, Eq) >> >> instance NiceLook (Row) where >> cout (Row s) = "|" ++ foldMap (\x -> " " ++ cout x ++ " |") s >> -- thnx Saizan >> >> makeRow n = case (List.length n) of >> 8 -> Row (fromList n) >> _ -> error "Row is not 8 squares" >> >> makeColouredSquares n c = makeRow $ map makeSquare (zip n (replicate 8 c)) >> >> makeSquare (n,c) = Square n c >> >> pawns = [Pawn, Pawn, Pawn, Pawn, Pawn, Pawn, Pawn, Pawn] >> back = [Rook, Knight, Bishop, Queen, King, Bishop, Knight, Rook] >> blank = [NoPiece, NoPiece, NoPiece, NoPiece, NoPiece, NoPiece, NoPiece, >> NoPiece] >> >> data Board = Board (Seq Row) >> deriving (Show, Eq) >> >> instance NiceLook (Board) where >> cout (Board c) = borderOutput ++ "\n" ++ (foldMap (\x -> cout x ++ "\n" >> ++ borderOutput ++ "\n") c) >> >> defaultBoard = Board (makeColouredSquares back Black <| >> makeColouredSquares pawns Black <| >> makeColouredSquares blank NoColour <| >> makeColouredSquares blank NoColour <| >> makeColouredSquares blank NoColour <| >> makeColouredSquares blank NoColour <| >> makeColouredSquares pawns White <| >> makeColouredSquares back White <| empty) >> >> >> borderOutput = "+" ++ (List.foldr1 (++) $ replicate 8 "----+") >> >> _______________________________________________ >> Beginners mailing list >> Beginners@haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> > > _______________________________________________ > 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/20091028/64914f23/attachment.html From jfredett at gmail.com Wed Oct 28 08:44:05 2009 From: jfredett at gmail.com (Joe Fredette) Date: Wed Oct 28 09:03:30 2009 Subject: [Haskell-beginners] Chessboard Module, opinions on... In-Reply-To: <4AE7FA2F.3030809@gmail.com> References: <383522F2-1A56-4F9E-9C65-426CE2AFBBF8@gmail.com> <4AE7FA2F.3030809@gmail.com> Message-ID: Poor typo on my part... Though the `K` bit is just a quirk of my nonstandard notation, `G` is the symbol for king in my notation. It's just how it's always been for me, not sure why I never picked up the proper symbology... But yes, one should use _correct_ notation in a parser... :) On Oct 28, 2009, at 4:00 AM, Tony Morris wrote: > Nitpicking, a white knight cannot move to b3 on its first move. Kb3 > denotes King to b3 which is not a possible first move. Nb3 is the > correct notation for Knight to b3. Possible first moves for a white > knight are Na3 Nc3 Nf3 and Nh3. > > Players in chess tournaments are required to notate their games using > algebraic notation and also as a result of training/reading, the > notation rolls off one's tongue. > > Joe Fredette wrote: >> Awesome, have you cabal-ized it? If not, it's pretty simple (look up >> 'cabal' on the haskellwiki). Then you can upload it to hackage to be >> toyed with. >> >> One thing that might be a cool direction to go w/ your project >> (sounds >> like you intend to make a chess playing program, this is somewhat >> orthogonal to that goal) is to build a "playback" machine. For >> instance, I play chess with people by email on a fairly regular >> basis. >> Specifically, we submit moves to one another in semi-standard[1] >> algebraic chess notation. So I might see a game like: >> >> >> 1. Kb3 e5 >> 2. d3 d6 >> ... >> n. a4->a5 e6->d7 >> >> Where the first move is White, moving his knight to B-3, then black >> moves his pawn from e7 to e5. etc. >> a move followed by a * is a check, followed by two stars is a mate. >> etc. You can poke at the wiki page for ACN for the appropriate >> syntax. >> My suggestion is that- often times we go many days in between moves, >> and so I don't keep track (in my head) of the last few moves he made, >> which can sometimes indicate weak points/general strategies. It would >> be _really_ nice to be able to replay old board positions at will, >> given this ACN notation of the game. Might be a nice (simple) use >> case >> for Parsec, and I imagine that most chess engines will have something >> like that (assuming they operate on STDIN/OUT) -- even if the syntax >> may be different. This will give you the "backend" to plug it onto >> anyway. >> >> Anywho, good luck with your project, it looks nice! >> >> /Joe >> >> PS, Just noticed the little function you use to display the board >> (and >> stuff). You may want to poke around the 2d Pretty printers on >> hackage, >> they may make it easier/more extensible to render the board. Also, >> `cout`? Someone's got a bit o' the ++ in 'em... :) >> >> >> >> [1] Okay, we mostly make it up, but it's _consistently_ arbitrary... >> >> On Oct 28, 2009, at 1:56 AM, i?fai wrote: >> >>> >>> I have just recently finished a 'ChessBoard' module that is meant to >>> represent a chess board. I could use some opinions and/or >>> suggestions >>> on the module. >>> >>> To give an example of how this can be used right now, and was my >>> immediate goal, you can do this: >>> >>> *ChessBoard> putStr $ cout defaultBoard >>> +----+----+----+----+----+----+----+----+ >>> | RB | NB | BB | QB | KB | BB | NB | RB | >>> +----+----+----+----+----+----+----+----+ >>> | PB | PB | PB | PB | PB | PB | PB | PB | >>> +----+----+----+----+----+----+----+----+ >>> | | | | | | | | | >>> +----+----+----+----+----+----+----+----+ >>> | | | | | | | | | >>> +----+----+----+----+----+----+----+----+ >>> | | | | | | | | | >>> +----+----+----+----+----+----+----+----+ >>> | | | | | | | | | >>> +----+----+----+----+----+----+----+----+ >>> | PW | PW | PW | PW | PW | PW | PW | PW | >>> +----+----+----+----+----+----+----+----+ >>> | RW | NW | BW | QW | KW | BW | NW | RW | >>> +----+----+----+----+----+----+----+----+ >>> >>> I have not determined exactly how I will be making moves, but the >>> logic will not be in my program. I am going to be using a chess >>> engine in another process (I haven't chosen a chess engine yet that >>> works on both windows and mac through stdin/stdout). >>> >>> The module itself follows, I appreciate any thoughts you might have. >>> >>> >>> module ChessBoard where >>> >>> import Data.Sequence >>> import Data.Foldable >>> import Data.Maybe >>> import Data.List as List >>> >>> class NiceLook a where >>> cout :: a -> String >>> >>> >>> data Piece = Bishop | Rook | Knight | King | Queen | Pawn | NoPiece >>> deriving (Show, Eq) >>> >>> instance NiceLook Piece where >>> cout Bishop = "B" >>> cout Rook = "R" >>> cout Knight = "N" >>> cout Queen = "Q" >>> cout Pawn = "P" >>> cout King = "K" >>> cout _ = " " >>> >>> data Colour = Black | White | NoColour >>> deriving (Show, Eq) >>> >>> instance NiceLook Colour where >>> cout Black = "B" >>> cout White = "W" >>> cout NoColour = " " >>> >>> -- error "..." might be useful >>> >>> data Square = Square Piece Colour >>> deriving (Show, Eq) >>> >>> instance NiceLook (Square) where >>> cout (Square p c) = (cout p) ++ (cout c) >>> >>> data Row = Row (Seq Square) >>> deriving (Show, Eq) >>> >>> instance NiceLook (Row) where >>> cout (Row s) = "|" ++ foldMap (\x -> " " ++ cout x ++ " |") >>> s -- thnx Saizan >>> >>> makeRow n = case (List.length n) of >>> 8 -> Row (fromList n) >>> _ -> error "Row is not 8 squares" >>> >>> makeColouredSquares n c = makeRow $ map makeSquare (zip n (replicate >>> 8 c)) >>> >>> makeSquare (n,c) = Square n c >>> >>> pawns = [Pawn, Pawn, Pawn, Pawn, Pawn, Pawn, Pawn, Pawn] >>> back = [Rook, Knight, Bishop, Queen, King, Bishop, Knight, Rook] >>> blank = [NoPiece, NoPiece, NoPiece, NoPiece, NoPiece, NoPiece, >>> NoPiece, NoPiece] >>> >>> data Board = Board (Seq Row) >>> deriving (Show, Eq) >>> >>> instance NiceLook (Board) where >>> cout (Board c) = borderOutput ++ "\n" ++ (foldMap (\x -> cout x ++ >>> "\n" ++ borderOutput ++ "\n") c) >>> >>> defaultBoard = Board (makeColouredSquares back Black <| >>> makeColouredSquares pawns Black <| >>> makeColouredSquares blank NoColour <| >>> makeColouredSquares blank NoColour <| >>> makeColouredSquares blank NoColour <| >>> makeColouredSquares blank NoColour <| >>> makeColouredSquares pawns White <| >>> makeColouredSquares back White <| empty) >>> >>> >>> borderOutput = "+" ++ (List.foldr1 (++) $ replicate 8 "----+") >>> >>> _______________________________________________ >>> Beginners mailing list >>> Beginners@haskell.org >>> http://www.haskell.org/mailman/listinfo/beginners >> >> _______________________________________________ >> Beginners mailing list >> Beginners@haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> > > -- > Tony Morris > http://tmorris.net/ > > From jfredett at gmail.com Wed Oct 28 09:30:06 2009 From: jfredett at gmail.com (Joe Fredette) Date: Wed Oct 28 09:06:37 2009 Subject: =?WINDOWS-1252?Q?Re:_[Haskell-beginners]_Chessboard_Module,_opin?= =?WINDOWS-1252?Q?ions_on=85?= In-Reply-To: References: <383522F2-1A56-4F9E-9C65-426CE2AFBBF8@gmail.com> Message-ID: <0397C5CF-8EB3-4563-AF1D-9045474099E9@gmail.com> Holy. Crap. Awesome... I don't even know what to say, a $50k chess playing robot? Awesome... On Oct 28, 2009, at 2:33 AM, i?fai wrote: > If I may be so bold, this project is much more interesting than you > might suspect. > > This is of course only the first part, but the next step is to have > an opengl display (I hope to get something running on mac and > windows) going and it must support opengl 1.4 due to some > limitations I have. > > The chess AI process is something I still have to hunt for mind you, > but the part that is the most interesting is that I am going to be > controlling a $50,000 robot with this in class :P. > > This robot is a CRS-3000 I believe, it looks something like this: http://www.phym.sdu.edu.cn/rolf/image/arm_overview.jpg > and it is picking up real chess pieces at the direction of the user > on screen. The communication with the robot is going to be over the > serial port. > > An interesting problem related to this is communication, luckily I > have tested a serial port library that does work on windows with > ghc. I will probably implement a program on the robot's 486 > controller to instruct the robot on what to do specifically. > > My next step that could definitely use some direction would be the > display part. I am limited to using power of 2 textures due to some > unfortunate limitations on the machines I have available. I am > thinking about this from a layered display approach. So I would be > able to have a layer that would be the chess board with some > interaction. Another layer would help calibrate the robot positions > (luckily I am using only 4 and interpolating the rest - I figured > out how to do that with some effort on Friday). > > I would probably start using glut for this, and hack together > something, but I would imagine what I am speaking of would benefit > very much from some of what haskell can do. There might even be a > library that already exists that I might not have found yet. > > - i?fai > > On 2009-10-28, at 2:11 AM, Joe Fredette wrote: > >> Awesome, have you cabal-ized it? If not, it's pretty simple (look >> up 'cabal' on the haskellwiki). Then you can upload it to hackage >> to be toyed with. >> >> One thing that might be a cool direction to go w/ your project >> (sounds like you intend to make a chess playing program, this is >> somewhat orthogonal to that goal) is to build a "playback" machine. >> For instance, I play chess with people by email on a fairly regular >> basis. Specifically, we submit moves to one another in semi- >> standard[1] algebraic chess notation. So I might see a game like: >> >> >> 1. Kb3 e5 >> 2. d3 d6 >> ... >> n. a4->a5 e6->d7 >> >> Where the first move is White, moving his knight to B-3, then black >> moves his pawn from e7 to e5. etc. >> a move followed by a * is a check, followed by two stars is a mate. >> etc. You can poke at the wiki page for ACN for the appropriate >> syntax. My suggestion is that- often times we go many days in >> between moves, and so I don't keep track (in my head) of the last >> few moves he made, which can sometimes indicate weak points/general >> strategies. It would be _really_ nice to be able to replay old >> board positions at will, given this ACN notation of the game. Might >> be a nice (simple) use case for Parsec, and I imagine that most >> chess engines will have something like that (assuming they operate >> on STDIN/OUT) -- even if the syntax may be different. This will >> give you the "backend" to plug it onto anyway. >> >> Anywho, good luck with your project, it looks nice! >> >> /Joe >> >> PS, Just noticed the little function you use to display the board >> (and stuff). You may want to poke around the 2d Pretty printers on >> hackage, they may make it easier/more extensible to render the >> board. Also, `cout`? Someone's got a bit o' the ++ in 'em... :) >> >> >> >> [1] Okay, we mostly make it up, but it's _consistently_ arbitrary... >> >> On Oct 28, 2009, at 1:56 AM, i?fai wrote: >> >>> >>> I have just recently finished a 'ChessBoard' module that is meant >>> to represent a chess board. I could use some opinions and/or >>> suggestions on the module. >>> >>> To give an example of how this can be used right now, and was my >>> immediate goal, you can do this: >>> >>> *ChessBoard> putStr $ cout defaultBoard >>> +----+----+----+----+----+----+----+----+ >>> | RB | NB | BB | QB | KB | BB | NB | RB | >>> +----+----+----+----+----+----+----+----+ >>> | PB | PB | PB | PB | PB | PB | PB | PB | >>> +----+----+----+----+----+----+----+----+ >>> | | | | | | | | | >>> +----+----+----+----+----+----+----+----+ >>> | | | | | | | | | >>> +----+----+----+----+----+----+----+----+ >>> | | | | | | | | | >>> +----+----+----+----+----+----+----+----+ >>> | | | | | | | | | >>> +----+----+----+----+----+----+----+----+ >>> | PW | PW | PW | PW | PW | PW | PW | PW | >>> +----+----+----+----+----+----+----+----+ >>> | RW | NW | BW | QW | KW | BW | NW | RW | >>> +----+----+----+----+----+----+----+----+ >>> >>> I have not determined exactly how I will be making moves, but the >>> logic will not be in my program. I am going to be using a chess >>> engine in another process (I haven't chosen a chess engine yet >>> that works on both windows and mac through stdin/stdout). >>> >>> The module itself follows, I appreciate any thoughts you might have. >>> >>> >>> module ChessBoard where >>> >>> import Data.Sequence >>> import Data.Foldable >>> import Data.Maybe >>> import Data.List as List >>> >>> class NiceLook a where >>> cout :: a -> String >>> >>> >>> data Piece = Bishop | Rook | Knight | King | Queen | Pawn | NoPiece >>> deriving (Show, Eq) >>> >>> instance NiceLook Piece where >>> cout Bishop = "B" >>> cout Rook = "R" >>> cout Knight = "N" >>> cout Queen = "Q" >>> cout Pawn = "P" >>> cout King = "K" >>> cout _ = " " >>> >>> data Colour = Black | White | NoColour >>> deriving (Show, Eq) >>> >>> instance NiceLook Colour where >>> cout Black = "B" >>> cout White = "W" >>> cout NoColour = " " >>> >>> -- error "..." might be useful >>> >>> data Square = Square Piece Colour >>> deriving (Show, Eq) >>> >>> instance NiceLook (Square) where >>> cout (Square p c) = (cout p) ++ (cout c) >>> >>> data Row = Row (Seq Square) >>> deriving (Show, Eq) >>> >>> instance NiceLook (Row) where >>> cout (Row s) = "|" ++ foldMap (\x -> " " ++ cout x ++ " |") >>> s -- thnx Saizan >>> >>> makeRow n = case (List.length n) of >>> 8 -> Row (fromList n) >>> _ -> error "Row is not 8 squares" >>> >>> makeColouredSquares n c = makeRow $ map makeSquare (zip n >>> (replicate 8 c)) >>> >>> makeSquare (n,c) = Square n c >>> >>> pawns = [Pawn, Pawn, Pawn, Pawn, Pawn, Pawn, Pawn, Pawn] >>> back = [Rook, Knight, Bishop, Queen, King, Bishop, Knight, Rook] >>> blank = [NoPiece, NoPiece, NoPiece, NoPiece, NoPiece, NoPiece, >>> NoPiece, NoPiece] >>> >>> data Board = Board (Seq Row) >>> deriving (Show, Eq) >>> >>> instance NiceLook (Board) where >>> cout (Board c) = borderOutput ++ "\n" ++ (foldMap (\x -> cout x + >>> + "\n" ++ borderOutput ++ "\n") c) >>> >>> defaultBoard = Board (makeColouredSquares back Black <| >>> makeColouredSquares pawns Black <| >>> makeColouredSquares blank NoColour <| >>> makeColouredSquares blank NoColour <| >>> makeColouredSquares blank NoColour <| >>> makeColouredSquares blank NoColour <| >>> makeColouredSquares pawns White <| >>> makeColouredSquares back White <| empty) >>> >>> >>> borderOutput = "+" ++ (List.foldr1 (++) $ replicate 8 "----+") >>> >>> _______________________________________________ >>> Beginners mailing list >>> Beginners@haskell.org >>> http://www.haskell.org/mailman/listinfo/beginners >> > From iaefai at me.com Wed Oct 28 11:05:16 2009 From: iaefai at me.com (=?windows-1252?Q?i=E6fai?=) Date: Wed Oct 28 10:42:10 2009 Subject: =?windows-1252?Q?Re:_[Haskell-beginners]_Chessboard_Module,_opin?= =?windows-1252?Q?ions_on=85?= In-Reply-To: <4AE7FF95.6090800@paradise.net.nz> References: <4AE7FF95.6090800@paradise.net.nz> Message-ID: <1AAE69C0-C936-4507-B150-B45237EB9146@me.com> Andy, feel free. I should note that I am going to update this code to use Text.PrettyPrint.HughesPJ shortly. In addition, it will be cabalizing it and go up on hackage once I figure it out. I will keep you informed of this. - i?fai On 2009-10-28, at 4:23 AM, Andy Elvey wrote: > i?fai wrote: >> >> I have just recently finished a 'ChessBoard' module that is meant >> to represent a chess board. I could use some opinions and/or >> suggestions on the module. >> >> To give an example of how this can be used right now, and was my >> immediate goal, you can do this: >> >> *ChessBoard> putStr $ cout defaultBoard >> +----+----+----+----+----+----+----+----+ >> | RB | NB | BB | QB | KB | BB | NB | RB | >> +----+----+----+----+----+----+----+----+ >> | PB | PB | PB | PB | PB | PB | PB | PB | >> +----+----+----+----+----+----+----+----+ >> | | | | | | | | | >> +----+----+----+----+----+----+----+----+ >> | | | | | | | | | >> +----+----+----+----+----+----+----+----+ >> | | | | | | | | | >> +----+----+----+----+----+----+----+----+ >> | | | | | | | | | >> +----+----+----+----+----+----+----+----+ >> | PW | PW | PW | PW | PW | PW | PW | PW | >> +----+----+----+----+----+----+----+----+ >> | RW | NW | BW | QW | KW | BW | NW | RW | >> +----+----+----+----+----+----+----+----+ >> >> I have not determined exactly how I will be making moves, but the >> logic will not be in my program. I am going to be using a chess >> engine in another process (I haven't chosen a chess engine yet that >> works on both windows and mac through stdin/stdout). >> >> The module itself follows, I appreciate any thoughts you might have. >> >> >> module ChessBoard where >> >> import Data.Sequence >> import Data.Foldable >> import Data.Maybe >> import Data.List as List >> >> class NiceLook a where >> cout :: a -> String >> >> >> data Piece = Bishop | Rook | Knight | King | Queen | Pawn | NoPiece >> deriving (Show, Eq) >> >> instance NiceLook Piece where >> cout Bishop = "B" >> cout Rook = "R" >> cout Knight = "N" >> cout Queen = "Q" >> cout Pawn = "P" >> cout King = "K" >> cout _ = " " >> >> data Colour = Black | White | NoColour >> deriving (Show, Eq) >> >> instance NiceLook Colour where >> cout Black = "B" >> cout White = "W" >> cout NoColour = " " >> >> -- error "..." might be useful >> >> data Square = Square Piece Colour >> deriving (Show, Eq) >> >> instance NiceLook (Square) where >> cout (Square p c) = (cout p) ++ (cout c) >> >> data Row = Row (Seq Square) >> deriving (Show, Eq) >> >> instance NiceLook (Row) where >> cout (Row s) = "|" ++ foldMap (\x -> " " ++ cout x ++ " |") >> s -- thnx Saizan >> >> makeRow n = case (List.length n) of >> 8 -> Row (fromList n) >> _ -> error "Row is not 8 squares" >> >> makeColouredSquares n c = makeRow $ map makeSquare (zip n >> (replicate 8 c)) >> >> makeSquare (n,c) = Square n c >> >> pawns = [Pawn, Pawn, Pawn, Pawn, Pawn, Pawn, Pawn, Pawn] >> back = [Rook, Knight, Bishop, Queen, King, Bishop, Knight, Rook] >> blank = [NoPiece, NoPiece, NoPiece, NoPiece, NoPiece, NoPiece, >> NoPiece, NoPiece] >> >> data Board = Board (Seq Row) >> deriving (Show, Eq) >> >> instance NiceLook (Board) where >> cout (Board c) = borderOutput ++ "\n" ++ (foldMap (\x -> cout x + >> + "\n" ++ borderOutput ++ "\n") c) >> >> defaultBoard = Board (makeColouredSquares back Black <| >> makeColouredSquares pawns Black <| >> makeColouredSquares blank NoColour <| >> makeColouredSquares blank NoColour <| >> makeColouredSquares blank NoColour <| >> makeColouredSquares blank NoColour <| >> makeColouredSquares pawns White <| >> makeColouredSquares back White <| empty) >> >> >> borderOutput = "+" ++ (List.foldr1 (++) $ replicate 8 "----+") >> >> > Hi i?fai! This is great! Very nicely done! > I was just wondering - I potter around with crosstab code in several > programming languages, and this (the table-creation code in > particular) could be quite handy in that area. So, would you mind > if I used this? I'll give credit of course! Many thanks for posting > this neat bit of code! - Andy > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners From iaefai at me.com Wed Oct 28 11:07:00 2009 From: iaefai at me.com (=?iso-8859-1?Q?i=E6fai?=) Date: Wed Oct 28 10:43:41 2009 Subject: =?windows-1252?Q?Re:_[Haskell-beginners]_Chessboard_Module,_opin?= =?windows-1252?Q?ions_on=85?= In-Reply-To: References: <383522F2-1A56-4F9E-9C65-426CE2AFBBF8@gmail.com> Message-ID: <5712FA16-27E2-4A92-9775-9203A73655E9@me.com> I am not making an AI, I am using an existing AI, so how I store it internally will not be of consequence to it. - i?fai On 2009-10-28, at 8:00 AM, Andrew Wagner wrote: > Just a note to let you know: it's virtually impossible to use a > linked-list-of-linked-list or two-dimensional-array board > representation as the basis of a serious AI. It's just too > inefficient (see http://www.cis.uab.edu/hyatt/boardrep.html for some > other options). > > That said, if you want to use this as the basis of being able to > play through games or something, it's great. > > On Wed, Oct 28, 2009 at 2:11 AM, Joe Fredette > wrote: > Awesome, have you cabal-ized it? If not, it's pretty simple (look up > 'cabal' on the haskellwiki). Then you can upload it to hackage to be > toyed with. > > One thing that might be a cool direction to go w/ your project > (sounds like you intend to make a chess playing program, this is > somewhat orthogonal to that goal) is to build a "playback" machine. > For instance, I play chess with people by email on a fairly regular > basis. Specifically, we submit moves to one another in semi-standard > [1] algebraic chess notation. So I might see a game like: > > > 1. Kb3 e5 > 2. d3 d6 > ... > n. a4->a5 e6->d7 > > Where the first move is White, moving his knight to B-3, then black > moves his pawn from e7 to e5. etc. > a move followed by a * is a check, followed by two stars is a mate. > etc. You can poke at the wiki page for ACN for the appropriate > syntax. My suggestion is that- often times we go many days in > between moves, and so I don't keep track (in my head) of the last > few moves he made, which can sometimes indicate weak points/general > strategies. It would be _really_ nice to be able to replay old board > positions at will, given this ACN notation of the game. Might be a > nice (simple) use case for Parsec, and I imagine that most chess > engines will have something like that (assuming they operate on > STDIN/OUT) -- even if the syntax may be different. This will give > you the "backend" to plug it onto anyway. > > Anywho, good luck with your project, it looks nice! > > /Joe > > PS, Just noticed the little function you use to display the board > (and stuff). You may want to poke around the 2d Pretty printers on > hackage, they may make it easier/more extensible to render the > board. Also, `cout`? Someone's got a bit o' the ++ in 'em... :) > > > > [1] Okay, we mostly make it up, but it's _consistently_ arbitrary... > > > On Oct 28, 2009, at 1:56 AM, i?fai wrote: > > > I have just recently finished a 'ChessBoard' module that is meant to > represent a chess board. I could use some opinions and/or > suggestions on the module. > > To give an example of how this can be used right now, and was my > immediate goal, you can do this: > > *ChessBoard> putStr $ cout defaultBoard > +----+----+----+----+----+----+----+----+ > | RB | NB | BB | QB | KB | BB | NB | RB | > +----+----+----+----+----+----+----+----+ > | PB | PB | PB | PB | PB | PB | PB | PB | > +----+----+----+----+----+----+----+----+ > | | | | | | | | | > +----+----+----+----+----+----+----+----+ > | | | | | | | | | > +----+----+----+----+----+----+----+----+ > | | | | | | | | | > +----+----+----+----+----+----+----+----+ > | | | | | | | | | > +----+----+----+----+----+----+----+----+ > | PW | PW | PW | PW | PW | PW | PW | PW | > +----+----+----+----+----+----+----+----+ > | RW | NW | BW | QW | KW | BW | NW | RW | > +----+----+----+----+----+----+----+----+ > > I have not determined exactly how I will be making moves, but the > logic will not be in my program. I am going to be using a chess > engine in another process (I haven't chosen a chess engine yet that > works on both windows and mac through stdin/stdout). > > The module itself follows, I appreciate any thoughts you might have. > > > module ChessBoard where > > import Data.Sequence > import Data.Foldable > import Data.Maybe > import Data.List as List > > class NiceLook a where > cout :: a -> String > > > data Piece = Bishop | Rook | Knight | King | Queen | Pawn | NoPiece > deriving (Show, Eq) > > instance NiceLook Piece where > cout Bishop = "B" > cout Rook = "R" > cout Knight = "N" > cout Queen = "Q" > cout Pawn = "P" > cout King = "K" > cout _ = " " > > data Colour = Black | White | NoColour > deriving (Show, Eq) > > instance NiceLook Colour where > cout Black = "B" > cout White = "W" > cout NoColour = " " > > -- error "..." might be useful > > data Square = Square Piece Colour > deriving (Show, Eq) > > instance NiceLook (Square) where > cout (Square p c) = (cout p) ++ (cout c) > > data Row = Row (Seq Square) > deriving (Show, Eq) > > instance NiceLook (Row) where > cout (Row s) = "|" ++ foldMap (\x -> " " ++ cout x ++ " |") > s -- thnx Saizan > > makeRow n = case (List.length n) of > 8 -> Row (fromList n) > _ -> error "Row is not 8 squares" > > makeColouredSquares n c = makeRow $ map makeSquare (zip n (replicate > 8 c)) > > makeSquare (n,c) = Square n c > > pawns = [Pawn, Pawn, Pawn, Pawn, Pawn, Pawn, Pawn, Pawn] > back = [Rook, Knight, Bishop, Queen, King, Bishop, Knight, Rook] > blank = [NoPiece, NoPiece, NoPiece, NoPiece, NoPiece, NoPiece, > NoPiece, NoPiece] > > data Board = Board (Seq Row) > deriving (Show, Eq) > > instance NiceLook (Board) where > cout (Board c) = borderOutput ++ "\n" ++ (foldMap (\x -> cout x ++ > "\n" ++ borderOutput ++ "\n") c) > > defaultBoard = Board (makeColouredSquares back Black <| > makeColouredSquares pawns Black <| > makeColouredSquares blank NoColour <| > makeColouredSquares blank NoColour <| > makeColouredSquares blank NoColour <| > makeColouredSquares blank NoColour <| > makeColouredSquares pawns White <| > makeColouredSquares back White <| empty) > > > borderOutput = "+" ++ (List.foldr1 (++) $ replicate 8 "----+") > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > From wagner.andrew at gmail.com Wed Oct 28 11:20:48 2009 From: wagner.andrew at gmail.com (Andrew Wagner) Date: Wed Oct 28 10:57:17 2009 Subject: =?windows-1252?Q?Fwd=3A_=5BHaskell=2Dbeginners=5D_Chessboard_Module=2C_opinions?= =?windows-1252?Q?_on=85?= In-Reply-To: References: <383522F2-1A56-4F9E-9C65-426CE2AFBBF8@gmail.com> <5712FA16-27E2-4A92-9775-9203A73655E9@me.com> Message-ID: ---------- Forwarded message ---------- From: Andrew Wagner Date: Wed, Oct 28, 2009 at 11:19 AM Subject: Re: [Haskell-beginners] Chessboard Module, opinions on? To: i?fai Ah, ok. You may also be interested in this protocol then: http://www.gnu.org/software/xboard/engine-intf.html . It's a widely-used standard for communication between chess AIs and UIs. On Wed, Oct 28, 2009 at 11:07 AM, i?fai wrote: > I am not making an AI, I am using an existing AI, so how I store it > internally will not be of consequence to it. > > - i?fai > > On 2009-10-28, at 8:00 AM, Andrew Wagner wrote: > > Just a note to let you know: it's virtually impossible to use a >> linked-list-of-linked-list or two-dimensional-array board representation as >> the basis of a serious AI. It's just too inefficient (see >> http://www.cis.uab.edu/hyatt/boardrep.html for some other options). >> >> That said, if you want to use this as the basis of being able to play >> through games or something, it's great. >> >> On Wed, Oct 28, 2009 at 2:11 AM, Joe Fredette wrote: >> Awesome, have you cabal-ized it? If not, it's pretty simple (look up >> 'cabal' on the haskellwiki). Then you can upload it to hackage to be toyed >> with. >> >> One thing that might be a cool direction to go w/ your project (sounds >> like you intend to make a chess playing program, this is somewhat orthogonal >> to that goal) is to build a "playback" machine. For instance, I play chess >> with people by email on a fairly regular basis. Specifically, we submit >> moves to one another in semi-standard[1] algebraic chess notation. So I >> might see a game like: >> >> >> 1. Kb3 e5 >> 2. d3 d6 >> ... >> n. a4->a5 e6->d7 >> >> Where the first move is White, moving his knight to B-3, then black moves >> his pawn from e7 to e5. etc. >> a move followed by a * is a check, followed by two stars is a mate. etc. >> You can poke at the wiki page for ACN for the appropriate syntax. My >> suggestion is that- often times we go many days in between moves, and so I >> don't keep track (in my head) of the last few moves he made, which can >> sometimes indicate weak points/general strategies. It would be _really_ nice >> to be able to replay old board positions at will, given this ACN notation of >> the game. Might be a nice (simple) use case for Parsec, and I imagine that >> most chess engines will have something like that (assuming they operate on >> STDIN/OUT) -- even if the syntax may be different. This will give you the >> "backend" to plug it onto anyway. >> >> Anywho, good luck with your project, it looks nice! >> >> /Joe >> >> PS, Just noticed the little function you use to display the board (and >> stuff). You may want to poke around the 2d Pretty printers on hackage, they >> may make it easier/more extensible to render the board. Also, `cout`? >> Someone's got a bit o' the ++ in 'em... :) >> >> >> >> [1] Okay, we mostly make it up, but it's _consistently_ arbitrary... >> >> >> >> On Oct 28, 2009, at 1:56 AM, i?fai wrote: >> >> >> I have just recently finished a 'ChessBoard' module that is meant to >> represent a chess board. I could use some opinions and/or suggestions on the >> module. >> >> To give an example of how this can be used right now, and was my immediate >> goal, you can do this: >> >> *ChessBoard> putStr $ cout defaultBoard >> +----+----+----+----+----+----+----+----+ >> | RB | NB | BB | QB | KB | BB | NB | RB | >> +----+----+----+----+----+----+----+----+ >> | PB | PB | PB | PB | PB | PB | PB | PB | >> +----+----+----+----+----+----+----+----+ >> | | | | | | | | | >> +----+----+----+----+----+----+----+----+ >> | | | | | | | | | >> +----+----+----+----+----+----+----+----+ >> | | | | | | | | | >> +----+----+----+----+----+----+----+----+ >> | | | | | | | | | >> +----+----+----+----+----+----+----+----+ >> | PW | PW | PW | PW | PW | PW | PW | PW | >> +----+----+----+----+----+----+----+----+ >> | RW | NW | BW | QW | KW | BW | NW | RW | >> +----+----+----+----+----+----+----+----+ >> >> I have not determined exactly how I will be making moves, but the logic >> will not be in my program. I am going to be using a chess engine in another >> process (I haven't chosen a chess engine yet that works on both windows and >> mac through stdin/stdout). >> >> The module itself follows, I appreciate any thoughts you might have. >> >> >> module ChessBoard where >> >> import Data.Sequence >> import Data.Foldable >> import Data.Maybe >> import Data.List as List >> >> class NiceLook a where >> cout :: a -> String >> >> >> data Piece = Bishop | Rook | Knight | King | Queen | Pawn | NoPiece >> deriving (Show, Eq) >> >> instance NiceLook Piece where >> cout Bishop = "B" >> cout Rook = "R" >> cout Knight = "N" >> cout Queen = "Q" >> cout Pawn = "P" >> cout King = "K" >> cout _ = " " >> >> data Colour = Black | White | NoColour >> deriving (Show, Eq) >> >> instance NiceLook Colour where >> cout Black = "B" >> cout White = "W" >> cout NoColour = " " >> >> -- error "..." might be useful >> >> data Square = Square Piece Colour >> deriving (Show, Eq) >> >> instance NiceLook (Square) where >> cout (Square p c) = (cout p) ++ (cout c) >> >> data Row = Row (Seq Square) >> deriving (Show, Eq) >> >> instance NiceLook (Row) where >> cout (Row s) = "|" ++ foldMap (\x -> " " ++ cout x ++ " |") s >> -- thnx Saizan >> >> makeRow n = case (List.length n) of >> 8 -> Row (fromList n) >> _ -> error "Row is not 8 squares" >> >> makeColouredSquares n c = makeRow $ map makeSquare (zip n (replicate 8 c)) >> >> makeSquare (n,c) = Square n c >> >> pawns = [Pawn, Pawn, Pawn, Pawn, Pawn, Pawn, Pawn, Pawn] >> back = [Rook, Knight, Bishop, Queen, King, Bishop, Knight, Rook] >> blank = [NoPiece, NoPiece, NoPiece, NoPiece, NoPiece, NoPiece, NoPiece, >> NoPiece] >> >> data Board = Board (Seq Row) >> deriving (Show, Eq) >> >> instance NiceLook (Board) where >> cout (Board c) = borderOutput ++ "\n" ++ (foldMap (\x -> cout x ++ "\n" >> ++ borderOutput ++ "\n") c) >> >> defaultBoard = Board (makeColouredSquares back Black <| >> makeColouredSquares pawns Black <| >> makeColouredSquares blank NoColour <| >> makeColouredSquares blank NoColour <| >> makeColouredSquares blank NoColour <| >> makeColouredSquares blank NoColour <| >> makeColouredSquares pawns White <| >> makeColouredSquares back White <| empty) >> >> >> borderOutput = "+" ++ (List.foldr1 (++) $ replicate 8 "----+") >> >> _______________________________________________ >> Beginners mailing list >> Beginners@haskell.org >> http://www.haskell.org/mailman/listinfo/beginners >> >> _______________________________________________ >> 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/20091028/8b49784c/attachment-0001.html From iaefai at me.com Wed Oct 28 11:23:31 2009 From: iaefai at me.com (=?iso-8859-1?Q?i=E6fai?=) Date: Wed Oct 28 11:00:20 2009 Subject: =?windows-1252?Q?Re:_[Haskell-beginners]_Chessboard_Module,_opin?= =?windows-1252?Q?ions_on=85?= In-Reply-To: References: <383522F2-1A56-4F9E-9C65-426CE2AFBBF8@gmail.com> Message-ID: <10BDE766-A51E-44FD-903B-BC6C9085F476@me.com> If I had a clock, I would consider it :P On 2009-10-28, at 11:20 AM, Darrin Thompson wrote: > On Wed, Oct 28, 2009 at 2:33 AM, i?fai wrote: >> The chess AI process is something I still have to hunt for mind >> you, but the >> part that is the most interesting is that I am going to be >> controlling a >> $50,000 robot with this in class :P. >> > > For $50k do you get a robot that can play a serious intimidating > blitz? Can it smack the clock with Class A player _authority_? :-) > > -- > Darrin From Christian.Maeder at dfki.de Wed Oct 28 12:01:29 2009 From: Christian.Maeder at dfki.de (Christian Maeder) Date: Wed Oct 28 11:37:59 2009 Subject: [Haskell-beginners] Re: Chessboard Module, opinions on... In-Reply-To: <1AAE69C0-C936-4507-B150-B45237EB9146@me.com> References: <4AE7FF95.6090800@paradise.net.nz> <1AAE69C0-C936-4507-B150-B45237EB9146@me.com> Message-ID: <4AE86AD9.6000200@dfki.de> i?fai schrieb: > Andy, feel free. I should note that I am going to update this code to > use Text.PrettyPrint.HughesPJ shortly. In addition, it will be Using a pretty printer library for this fixed format seems unnecessary to me. C. From shawn-haskell at willden.org Wed Oct 28 21:45:35 2009 From: shawn-haskell at willden.org (Shawn Willden) Date: Wed Oct 28 21:22:05 2009 Subject: [Haskell-beginners] Memoization Message-ID: <200910281945.35769.shawn-haskell@willden.org> Hi, I've just begun experimenting with Haskell, and I'm having a lot of fun, but my first semi-serious program is in need of some optimization, and based on the results of profiling I think a really good start is to memoize one particular function which is called many, many times (and currently consumes over 80% of the program run time). The function takes a two-dimensional range and a location within that range and returns a list of the locations vertically and horizontally adjacent to that location and within the bounds type Bounds = ((Int,Int),(Int,Int)) type Location = (Int,Int) adjacentCells :: Bounds -> Location -> [Location] adjacentCells bounds (col, row) = filter valid cells where valid loc = inRange bounds loc neighborLoc' = [(col-1,row),(col+1,row),(col,row-1),(col,row+1)] The ranges are fairly small -- certainly no more than 10x10, and each location has an associated list of at most 4 neighbors (edge locations have less). During a given run of the program, the bounds are fixed. So, any tips on how I can memoize this function? I have read the Memoization page on the wiki, and I understand (I think) the recursive memoization section, but it's not clear to me how to apply that approach. Section one on non-recursive memoization seems like what I want, but I don't understand the section and the links provided haven't shed any light for me. The section uses the following notation to describe how to construct a map to be used to hold the keys and values: Map () b := b Map (Either a a') b := (Map a b, Map a' b) Map (a,a') b := Map a (Map a' b) but I'm not sure what that notation is. It's not Haskell code, I don't think. Any and all suggestions appreciated. Thanks, Shawn. From daniel.is.fischer at web.de Wed Oct 28 22:20:43 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Wed Oct 28 21:58:35 2009 Subject: [Haskell-beginners] Memoization In-Reply-To: <200910281945.35769.shawn-haskell@willden.org> References: <200910281945.35769.shawn-haskell@willden.org> Message-ID: <200910290320.44286.daniel.is.fischer@web.de> Am Donnerstag 29 Oktober 2009 02:45:35 schrieb Shawn Willden: > Hi, > > I've just begun experimenting with Haskell, and I'm having a lot of fun, > but my first semi-serious program is in need of some optimization, and > based on the results of profiling I think a really good start is to memoize > one particular function which is called many, many times (and currently > consumes over 80% of the program run time). > > The function takes a two-dimensional range and a location within that range > and returns a list of the locations vertically and horizontally adjacent to > that location and within the bounds > > type Bounds = ((Int,Int),(Int,Int)) > type Location = (Int,Int) > adjacentCells :: Bounds -> Location -> [Location] > adjacentCells bounds (col, row) = filter valid cells > where > valid loc = inRange bounds loc > neighborLoc' = [(col-1,row),(col+1,row),(col,row-1),(col,row+1)] > > The ranges are fairly small -- certainly no more than 10x10, and each > location has an associated list of at most 4 neighbors (edge locations have > less). During a given run of the program, the bounds are fixed. Probably the simplest thing to do is use an array. At the start of your programme let neighbourArray = array bounds [(loc, adjacentCells bounds loc) | loc <- range bounds] and replace calls "adjacentCells bounds loc" with "neighbourArray!loc" > > Thanks, > > Shawn. From andy.elvey at paradise.net.nz Thu Oct 29 04:03:05 2009 From: andy.elvey at paradise.net.nz (Andy Elvey) Date: Thu Oct 29 03:39:53 2009 Subject: =?windows-1252?Q?Re=3A_=5BHaskell-beginners=5D_Chessboard_?= =?windows-1252?Q?Module=2C_opinions_on=85?= In-Reply-To: <1AAE69C0-C936-4507-B150-B45237EB9146@me.com> References: <4AE7FF95.6090800@paradise.net.nz> <1AAE69C0-C936-4507-B150-B45237EB9146@me.com> Message-ID: <4AE94C39.8060808@paradise.net.nz> Great! Many thanks.... :) - Andy i?fai wrote: > Andy, feel free. I should note that I am going to update this code to > use Text.PrettyPrint.HughesPJ shortly. In addition, it will be > cabalizing it and go up on hackage once I figure it out. > > I will keep you informed of this. > > - i?fai > > On 2009-10-28, at 4:23 AM, Andy Elvey wrote: > >> i?fai wrote: >>> >>> I have just recently finished a 'ChessBoard' module that is meant to >>> represent a chess board. I could use some opinions and/or >>> suggestions on the module. >>> >>> To give an example of how this can be used right now, and was my >>> immediate goal, you can do this: >>> >>> *ChessBoard> putStr $ cout defaultBoard >>> +----+----+----+----+----+----+----+----+ >>> | RB | NB | BB | QB | KB | BB | NB | RB | >>> +----+----+----+----+----+----+----+----+ >>> | PB | PB | PB | PB | PB | PB | PB | PB | >>> +----+----+----+----+----+----+----+----+ >>> | | | | | | | | | >>> +----+----+----+----+----+----+----+----+ >>> | | | | | | | | | >>> +----+----+----+----+----+----+----+----+ >>> | | | | | | | | | >>> +----+----+----+----+----+----+----+----+ >>> | | | | | | | | | >>> +----+----+----+----+----+----+----+----+ >>> | PW | PW | PW | PW | PW | PW | PW | PW | >>> +----+----+----+----+----+----+----+----+ >>> | RW | NW | BW | QW | KW | BW | NW | RW | >>> +----+----+----+----+----+----+----+----+ >>> >>> I have not determined exactly how I will be making moves, but the >>> logic will not be in my program. I am going to be using a chess >>> engine in another process (I haven't chosen a chess engine yet that >>> works on both windows and mac through stdin/stdout). >>> >>> The module itself follows, I appreciate any thoughts you might have. >>> >>> >>> module ChessBoard where >>> >>> import Data.Sequence >>> import Data.Foldable >>> import Data.Maybe >>> import Data.List as List >>> >>> class NiceLook a where >>> cout :: a -> String >>> >>> >>> data Piece = Bishop | Rook | Knight | King | Queen | Pawn | NoPiece >>> deriving (Show, Eq) >>> >>> instance NiceLook Piece where >>> cout Bishop = "B" >>> cout Rook = "R" >>> cout Knight = "N" >>> cout Queen = "Q" >>> cout Pawn = "P" >>> cout King = "K" >>> cout _ = " " >>> >>> data Colour = Black | White | NoColour >>> deriving (Show, Eq) >>> >>> instance NiceLook Colour where >>> cout Black = "B" >>> cout White = "W" >>> cout NoColour = " " >>> >>> -- error "..." might be useful >>> >>> data Square = Square Piece Colour >>> deriving (Show, Eq) >>> >>> instance NiceLook (Square) where >>> cout (Square p c) = (cout p) ++ (cout c) >>> >>> data Row = Row (Seq Square) >>> deriving (Show, Eq) >>> >>> instance NiceLook (Row) where >>> cout (Row s) = "|" ++ foldMap (\x -> " " ++ cout x ++ " |") >>> s -- thnx Saizan >>> >>> makeRow n = case (List.length n) of >>> 8 -> Row (fromList n) >>> _ -> error "Row is not 8 squares" >>> >>> makeColouredSquares n c = makeRow $ map makeSquare (zip n (replicate >>> 8 c)) >>> >>> makeSquare (n,c) = Square n c >>> >>> pawns = [Pawn, Pawn, Pawn, Pawn, Pawn, Pawn, Pawn, Pawn] >>> back = [Rook, Knight, Bishop, Queen, King, Bishop, Knight, Rook] >>> blank = [NoPiece, NoPiece, NoPiece, NoPiece, NoPiece, NoPiece, >>> NoPiece, NoPiece] >>> >>> data Board = Board (Seq Row) >>> deriving (Show, Eq) >>> >>> instance NiceLook (Board) where >>> cout (Board c) = borderOutput ++ "\n" ++ (foldMap (\x -> cout x >>> ++ "\n" ++ borderOutput ++ "\n") c) >>> >>> defaultBoard = Board (makeColouredSquares back Black <| >>> makeColouredSquares pawns Black <| >>> makeColouredSquares blank NoColour <| >>> makeColouredSquares blank NoColour <| >>> makeColouredSquares blank NoColour <| >>> makeColouredSquares blank NoColour <| >>> makeColouredSquares pawns White <| >>> makeColouredSquares back White <| empty) >>> >>> >>> borderOutput = "+" ++ (List.foldr1 (++) $ replicate 8 "----+") >>> >>> >> Hi i?fai! This is great! Very nicely done! >> I was just wondering - I potter around with crosstab code in several >> programming languages, and this (the table-creation code in >> particular) could be quite handy in that area. So, would you mind if >> I used this? I'll give credit of course! Many thanks for posting >> this neat bit of code! - Andy >> >> _______________________________________________ >> Beginners mailing list >> Beginners@haskell.org >> http://www.haskell.org/mailman/listinfo/beginners > > From andy.elvey at paradise.net.nz Thu Oct 29 04:04:12 2009 From: andy.elvey at paradise.net.nz (Andy Elvey) Date: Thu Oct 29 03:40:37 2009 Subject: =?windows-1252?Q?Re=3A_=5BHaskell-beginners=5D_Chessboard_?= =?windows-1252?Q?Module=2C_opinions_on=85?= In-Reply-To: <4AE831E6.2070309@dfki.de> References: <4AE7FF95.6090800@paradise.net.nz> <4AE831E6.2070309@dfki.de> Message-ID: <4AE94C7C.8000000@paradise.net.nz> Hi Christian - Thanks for that! I'll check that out.... :) Bye for now - - Andy Christian Maeder wrote: > You may want to look at the tabular package for table-creation code: > http://hackage.haskell.org/package/tabular > > C. > > Andy Elvey schrieb: > >> Hi i?fai! This is great! Very nicely done! >> I was just wondering - I potter around with crosstab code in several >> programming languages, and this (the table-creation code in particular) >> could be quite handy in that area. So, would you mind if I used this? >> I'll give credit of course! Many thanks for posting this neat bit of >> code! - Andy >> >> > > From matthias.guedemann at ovgu.de Fri Oct 30 07:44:41 2009 From: matthias.guedemann at ovgu.de (Matthias Guedemann) Date: Fri Oct 30 07:22:26 2009 Subject: [Haskell-beginners] list monad question Message-ID: <1256902955-sup-336@pc44es141.cs.uni-magdeburg.de> Hi, a friend of mine wanted to write function (in Perl) that creates all tuples of length 3 of the elements of a given list, e.g. [(0,0,0),(0,0,1),(0,0,2),...,(5,5,5)] for the list [0..5]. Trying to get better at Haskell, I wrote a small function using the list monad for this (tuples replaced with lists) all3 ls = do a <- ls b <- ls c <- ls return [a,b,c] Now I want to make it capable to create all combinations of length n instead of fixed length 3 (that's why list instead of tuple), but I don't really see how. As the do notation translates to ls >>= \a -> etc. I thought it should be possible to have some sort of "foldr (>>=)" over a list of length n, but I can't figure out how to collect the variable number of results in a list for the "return". Any hints for that? best regards Matthias -- __________________________________________________________ ___ __ __ Dipl. Inf. Matthias Guedemann / __\/ _\ /__\ Computer Systems in Engineering / / \ \ /_\ Otto-von-Guericke Universitaet Magdeburg / /___ _\ \//__ Tel.: 0391 / 67-19359 \____/ \__/\__/ __________________________________________________________ From dav.vire+haskell at gmail.com Fri Oct 30 08:09:50 2009 From: dav.vire+haskell at gmail.com (David Virebayre) Date: Fri Oct 30 07:46:15 2009 Subject: [Haskell-beginners] list monad question In-Reply-To: <1256902955-sup-336@pc44es141.cs.uni-magdeburg.de> References: <1256902955-sup-336@pc44es141.cs.uni-magdeburg.de> Message-ID: <4c88418c0910300509w4e949ce9y73f784a248960544@mail.gmail.com> On Fri, Oct 30, 2009 at 12:44 PM, Matthias Guedemann wrote: > a friend of mine wanted to write function (in Perl) that creates all tuples of > length 3 of the elements of a given list, > e.g. [(0,0,0),(0,0,1),(0,0,2),...,(5,5,5)] for the list [0..5]. Trying to get > better at Haskell, I wrote a small function using the list monad for this (tuples > replaced with lists) > > all3 ls = do > ?a <- ls > ?b <- ls > ?c <- ls > ?return [a,b,c] Almost there : all3 ls = do a <- ls b <- ls c <- ls return (a,b,c) For each element a of list ls , for each element b of the same list ls, and for each element c of the same list ls, make a tuple of them. return the list of tall the tuples. You could also write it with a list comprehension : all3 ls = [ (a,b,c) | a <- ls, b <- ls, c <- ls ] David. From jakubuv at gmail.com Fri Oct 30 08:23:27 2009 From: jakubuv at gmail.com (Jan Jakubuv) Date: Fri Oct 30 08:02:23 2009 Subject: [Haskell-beginners] list monad question In-Reply-To: <1256902955-sup-336@pc44es141.cs.uni-magdeburg.de> References: <1256902955-sup-336@pc44es141.cs.uni-magdeburg.de> Message-ID: <20091030122327.GA11848@lxultra2.macs.hw.ac.uk> Hi Matthias, On Fri, Oct 30, 2009 at 12:44:41PM +0100, Matthias Guedemann wrote: > > Hi, > > a friend of mine wanted to write function (in Perl) that creates all tuples of > length 3 of the elements of a given list... > > all3 ls = do > a <- ls > b <- ls > c <- ls > return [a,b,c] > > Now I want to make it capable to create all combinations of length n instead of > fixed length 3 (that's why list instead of tuple), but I don't really see how. How about a recursive function like this: alln 1 ls = map (:[]) ls alln n ls = do a <- ls as <- alln (n-1) ls return (a:as) Note that `ls :: [t]` and `all (n-1) ls :: [[t]]` has different types but it's okay because both are in the list monad. Also, it can be done with list comprehensions: alln' 1 ls = [[a] | a<-ls] alln' n ls = [a:as | a<-ls, as<-alln' (n-1) ls] Sincerely, jan. -- Heriot-Watt University is a Scottish charity registered under charity number SC000278. From dav.vire+haskell at gmail.com Fri Oct 30 08:29:20 2009 From: dav.vire+haskell at gmail.com (David Virebayre) Date: Fri Oct 30 08:05:45 2009 Subject: [Haskell-beginners] list monad question In-Reply-To: <4c88418c0910300509w4e949ce9y73f784a248960544@mail.gmail.com> References: <1256902955-sup-336@pc44es141.cs.uni-magdeburg.de> <4c88418c0910300509w4e949ce9y73f784a248960544@mail.gmail.com> Message-ID: <4c88418c0910300529h2ef4cd1eib5e2c13e0b3f8a9b@mail.gmail.com> On Fri, Oct 30, 2009 at 1:09 PM, David Virebayre wrote: > Almost there : Nevermind, I didn't read the question carefully Sorry about that :( From dav.vire+haskell at gmail.com Fri Oct 30 08:36:21 2009 From: dav.vire+haskell at gmail.com (David Virebayre) Date: Fri Oct 30 08:12:45 2009 Subject: [Haskell-beginners] list monad question In-Reply-To: <1256902955-sup-336@pc44es141.cs.uni-magdeburg.de> References: <1256902955-sup-336@pc44es141.cs.uni-magdeburg.de> Message-ID: <4c88418c0910300536p75b38a50k629b60aef643e635@mail.gmail.com> On Fri, Oct 30, 2009 at 12:44 PM, Matthias Guedemann wrote: > Now I want to make it capable to create all combinations of length n instead of > fixed length 3 (that's why list instead of tuple), but I don't really see how. If I understood what you ask this time, there's a function in Control.Monad that does it : allN = replicateM replicateM 4 [ 1,2,3 ] = [ [ 1,1,1,1],[1,1,1,2], .... when you write a <- ls b <- ls c <- ls You perform the monad "action" 3 times, collecting the result in a then b, then c. now replicateM performs a monad action n times, collecting the result in a list. turns out that making a list of the result is exactly what you want. David. From matthias.guedemann at ovgu.de Fri Oct 30 08:37:55 2009 From: matthias.guedemann at ovgu.de (Matthias Guedemann) Date: Fri Oct 30 08:15:43 2009 Subject: [Haskell-beginners] list monad question In-Reply-To: <4c88418c0910300509w4e949ce9y73f784a248960544@mail.gmail.com> References: <1256902955-sup-336@pc44es141.cs.uni-magdeburg.de> <4c88418c0910300509w4e949ce9y73f784a248960544@mail.gmail.com> Message-ID: <1256906197-sup-9834@pc44es141.cs.uni-magdeburg.de> Thanks David, > all3 ls = do > a <- ls > b <- ls > c <- ls > return (a,b,c) > > For each element a of list ls , for each element b of the same list > ls, and for each element c of the same list ls, make a tuple of them. > return the list of tall the tuples. But it is a bit more complicated. I changed the result to [a,b,c] in order to have variable length results. I am now trying to get a "allN ls n" function that returns the result for the original problem with "allN [0..5] 3" and all combinations of the form [a,b] with "allN [0..5] 2". So basically I want a variable number of name bindings in the list comprehension. Is this possible in a (simple) way using the list monad? Maybe like allN ls n = foldr (>>=) [] (replicate n ls) >>= return regards, Matthias -- __________________________________________________________ ___ __ __ Dipl. Inf. Matthias Guedemann / __\/ _\ /__\ Computer Systems in Engineering / / \ \ /_\ Otto-von-Guericke Universitaet Magdeburg / /___ _\ \//__ Tel.: 0391 / 67-19359 \____/ \__/\__/ __________________________________________________________ From daniel.is.fischer at web.de Fri Oct 30 08:56:34 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Fri Oct 30 08:34:21 2009 Subject: [Haskell-beginners] list monad question In-Reply-To: <1256902955-sup-336@pc44es141.cs.uni-magdeburg.de> References: <1256902955-sup-336@pc44es141.cs.uni-magdeburg.de> Message-ID: <200910301356.34688.daniel.is.fischer@web.de> Am Freitag 30 Oktober 2009 12:44:41 schrieb Matthias Guedemann: > Hi, > > a friend of mine wanted to write function (in Perl) that creates all tuples > of length 3 of the elements of a given list, > e.g. [(0,0,0),(0,0,1),(0,0,2),...,(5,5,5)] for the list [0..5]. Trying to > get better at Haskell, I wrote a small function using the list monad for > this (tuples replaced with lists) > > all3 ls = do > a <- ls > b <- ls > c <- ls > return [a,b,c] > > Now I want to make it capable to create all combinations of length n > instead of fixed length 3 (that's why list instead of tuple), but I don't > really see how. As the do notation translates to > > ls >>= \a -> etc. > > I thought it should be possible to have some sort of "foldr (>>=)" over a > list of length n, but I can't figure out how to collect the variable number > of results in a list for the "return". Recursive version first: combinations 0 _ = [[]] -- whatever the list, there's one way to pick 0 elements combinations n xs = do h <- xs t <- combinations (n-1) xs return (h:t) (check for n < 0 omitted) So, what are we doing? We have one list of a (xs) and one list of [a] (ys = combinations (n-1) xs) and cons each element of xs to each element of ys: f xs ys = [h:t | h <- xs, t <- ys] = concat [[h:t | t <- ys] | h <- xs] = concat [map (h:) ys | h <- xs] = concat (map (\h -> map (h:) ys) xs) = xs >>= \h -> map (h:) ys That gives combinations n xs = foldr f [[]] (replicate n xs) pointfree, for extra goodness: -- pointfree f inline combinations n xs = foldr ((. (. (:)) . flip map) . (>>=)) [[]] (replicate n xs) -- eliminate xs combinations n = foldr ((. (. (:)) . flip map) . (>>=)) [[]] . replicate n -- completely pointfree combinations = (foldr ((. (. (:)) . flip map) . (>>=)) [[]] .) . replicate > > Any hints for that? > > best regards > Matthias From matthias.guedemann at ovgu.de Fri Oct 30 08:56:40 2009 From: matthias.guedemann at ovgu.de (Matthias Guedemann) Date: Fri Oct 30 08:34:27 2009 Subject: [Haskell-beginners] list monad question In-Reply-To: <20091030122327.GA11848@lxultra2.macs.hw.ac.uk> References: <1256902955-sup-336@pc44es141.cs.uni-magdeburg.de> <20091030122327.GA11848@lxultra2.macs.hw.ac.uk> Message-ID: <1256907306-sup-6590@pc44es141.cs.uni-magdeburg.de> > How about a recursive function like this: > > alln 1 ls = map (:[]) ls > alln n ls = do > a <- ls > as <- alln (n-1) ls > return (a:as) > > Note that `ls :: [t]` and `all (n-1) ls :: [[t]]` has different types but > it's okay because both are in the list monad. > > Also, it can be done with list comprehensions: > > alln' 1 ls = [[a] | a<-ls] > alln' n ls = [a:as | a<-ls, as<-alln' (n-1) ls] Works great, thanks a lot Matthias -- __________________________________________________________ ___ __ __ Dipl. Inf. Matthias Guedemann / __\/ _\ /__\ Computer Systems in Engineering / / \ \ /_\ Otto-von-Guericke Universitaet Magdeburg / /___ _\ \//__ Tel.: 0391 / 67-19359 \____/ \__/\__/ __________________________________________________________ From matthias.guedemann at ovgu.de Fri Oct 30 09:01:20 2009 From: matthias.guedemann at ovgu.de (Matthias Guedemann) Date: Fri Oct 30 08:39:05 2009 Subject: [Haskell-beginners] list monad question In-Reply-To: <4c88418c0910300536p75b38a50k629b60aef643e635@mail.gmail.com> References: <1256902955-sup-336@pc44es141.cs.uni-magdeburg.de> <4c88418c0910300536p75b38a50k629b60aef643e635@mail.gmail.com> Message-ID: <1256907417-sup-736@pc44es141.cs.uni-magdeburg.de> Hallo David, > If I understood what you ask this time, there's a function in > Control.Monad that does it : > allN = replicateM > > replicateM 4 [ 1,2,3 ] = [ [ 1,1,1,1],[1,1,1,2], .... that is exactly what I wanted, thanks a lot. Next time I will state the question more clearly :-) > when you write > > a <- ls > b <- ls > c <- ls > > You perform the monad "action" 3 times, collecting the result in a > then b, then c. > now replicateM performs a monad action n times, collecting the result in a list. > turns out that making a list of the result is exactly what you want. Maybe I should have a closer look at Control.Monad, a lot seems to be there already. best regards, Matthias -- __________________________________________________________ ___ __ __ Dipl. Inf. Matthias Guedemann / __\/ _\ /__\ Computer Systems in Engineering / / \ \ /_\ Otto-von-Guericke Universitaet Magdeburg / /___ _\ \//__ Tel.: 0391 / 67-19359 \____/ \__/\__/ __________________________________________________________ From matthias.guedemann at ovgu.de Fri Oct 30 09:32:35 2009 From: matthias.guedemann at ovgu.de (Matthias Guedemann) Date: Fri Oct 30 09:10:19 2009 Subject: [Haskell-beginners] list monad question In-Reply-To: <200910301356.34688.daniel.is.fischer@web.de> References: <1256902955-sup-336@pc44es141.cs.uni-magdeburg.de> <200910301356.34688.daniel.is.fischer@web.de> Message-ID: <1256909341-sup-3304@pc44es141.cs.uni-magdeburg.de> Hi Daniel, > That gives > > combinations n xs = foldr f [[]] (replicate n xs) > > pointfree, for extra goodness: > > -- pointfree f inline > combinations n xs = foldr ((. (. (:)) . flip map) . (>>=)) [[]] (replicate n xs) > -- eliminate xs > combinations n = foldr ((. (. (:)) . flip map) . (>>=)) [[]] . replicate n > -- completely pointfree > combinations = (foldr ((. (. (:)) . flip map) . (>>=)) [[]] .) . replicate thank you, looks rather strange to me but works well. regards -- __________________________________________________________ ___ __ __ Dipl. Inf. Matthias Guedemann / __\/ _\ /__\ Computer Systems in Engineering / / \ \ /_\ Otto-von-Guericke Universitaet Magdeburg / /___ _\ \//__ Tel.: 0391 / 67-19359 \____/ \__/\__/ __________________________________________________________ From luca_ciciriello at hotmail.com Fri Oct 30 09:40:13 2009 From: luca_ciciriello at hotmail.com (Luca Ciciriello) Date: Fri Oct 30 09:16:37 2009 Subject: [Haskell-beginners] beginner question Message-ID: Hi all. Just a very basic question. I need to write a function str2lsts :: String -> [[String]] in order to transorm a string like: "\"1\",\"cat\",\"dog\"?\"2\",\"duck\",\"goose\"" in the list of lists: [["1","cat","dog"],["2","duck","goose"]] I've tried to mix recursion, pattern matching and list comprehension, but the obtained result was embarrassing complex (> 20 lines of awful code). I think that a more simple solution certainly exists. Thanks in advance for any idea. Luca _________________________________________________________________ Download Messenger onto your mobile for free http://clk.atdmt.com/UKM/go/174426567/direct/01/ -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20091030/4a0586e3/attachment.html From es at ertes.de Fri Oct 30 10:01:29 2009 From: es at ertes.de (Ertugrul Soeylemez) Date: Fri Oct 30 09:38:15 2009 Subject: [Haskell-beginners] Re: list monad question References: <1256902955-sup-336@pc44es141.cs.uni-magdeburg.de> Message-ID: <20091030150129.1f7583d8@tritium.xx> Hello Matthias, you may want to have a look at section 11 of my monads tutorial [1], which contains monadic library functions like replicateM together with examples and detailed explanations. [1] http://ertes.de/articles/monads.html#section-11 Greets, Ertugrul. Matthias Guedemann wrote: > > Hi, > > a friend of mine wanted to write function (in Perl) that creates all tuples of > length 3 of the elements of a given list, > e.g. [(0,0,0),(0,0,1),(0,0,2),...,(5,5,5)] for the list [0..5]. Trying to get > better at Haskell, I wrote a small function using the list monad for this (tuples > replaced with lists) > > all3 ls = do > a <- ls > b <- ls > c <- ls > return [a,b,c] > > Now I want to make it capable to create all combinations of length n instead of > fixed length 3 (that's why list instead of tuple), but I don't really see how. > As the do notation translates to > > ls >>= \a -> etc. > > I thought it should be possible to have some sort of "foldr (>>=)" over a list > of length n, but I can't figure out how to collect the variable number of > results in a list for the "return". > > Any hints for that? > > best regards > Matthias > > -- nightmare = unsafePerformIO (getWrongWife >>= sex) http://blog.ertes.de/ From matthias.guedemann at ovgu.de Fri Oct 30 10:39:25 2009 From: matthias.guedemann at ovgu.de (Matthias Guedemann) Date: Fri Oct 30 10:17:18 2009 Subject: [Haskell-beginners] Re: list monad question In-Reply-To: <20091030150129.1f7583d8@tritium.xx> References: <1256902955-sup-336@pc44es141.cs.uni-magdeburg.de> <20091030150129.1f7583d8@tritium.xx> Message-ID: <1256912868-sup-565@pc44es141.cs.uni-magdeburg.de> Hello Ertugrul, this looks interesting. I read Brent Yorgeys Typeclassopedia and also the work you cite in your tutorial. So I am trying to learn about Applicative, Monoids, Monads etc. by using them. Your description of the library functions comes handy, as reading the source directly does not really help if you're still struggling with understanding the concepts. And on the other hand, due to the abstract nature of monads, a lot of functionality should be "hidden" in the library (just like my rewrite of replicateM) thank you very much, will read it on the weekend regards Matthias Excerpts from Ertugrul Soeylemez's message of Fr Okt 30 15:01:29 +0100 2009: > Hello Matthias, > > you may want to have a look at section 11 of my monads tutorial [1], > which contains monadic library functions like replicateM together with > examples and detailed explanations. > > [1] http://ertes.de/articles/monads.html#section-11 > > > Greets, > Ertugrul. > > Matthias Guedemann wrote: > > > > > Hi, > > > > a friend of mine wanted to write function (in Perl) that creates all tuples of > > length 3 of the elements of a given list, > > e.g. [(0,0,0),(0,0,1),(0,0,2),...,(5,5,5)] for the list [0..5]. Trying to get > > better at Haskell, I wrote a small function using the list monad for this (tuples > > replaced with lists) > > > > all3 ls = do > > a <- ls > > b <- ls > > c <- ls > > return [a,b,c] > > > > Now I want to make it capable to create all combinations of length n instead of > > fixed length 3 (that's why list instead of tuple), but I don't really see how. > > As the do notation translates to > > > > ls >>= \a -> etc. > > > > I thought it should be possible to have some sort of "foldr (>>=)" over a list > > of length n, but I can't figure out how to collect the variable number of > > results in a list for the "return". > > > > Any hints for that? > > > > best regards > > Matthias > > > > > -- __________________________________________________________ ___ __ __ Dipl. Inf. Matthias Guedemann / __\/ _\ /__\ Computer Systems in Engineering / / \ \ /_\ Otto-von-Guericke Universitaet Magdeburg / /___ _\ \//__ Tel.: 0391 / 67-19359 \____/ \__/\__/ __________________________________________________________ From daniel.is.fischer at web.de Fri Oct 30 10:46:33 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Fri Oct 30 10:29:21 2009 Subject: [Haskell-beginners] beginner question In-Reply-To: References: Message-ID: <200910301546.34301.daniel.is.fischer@web.de> Am Freitag 30 Oktober 2009 14:40:13 schrieb Luca Ciciriello: > Hi all. > > Just a very basic question. > > > > I need to write a function str2lsts :: String -> [[String]] in order to > transorm a string like: > > > > "\"1\",\"cat\",\"dog\"?\"2\",\"duck\",\"goose\"" > > > > in the list of lists: > > > > [["1","cat","dog"],["2","duck","goose"]] > > > > I've tried to mix recursion, pattern matching and list comprehension, but > the obtained result was embarrassing complex (> 20 lines of awful code). I > think that a more simple solution certainly exists. > splitOnToken :: Eq a => a -> [a] -> [[a]] splitOnToken t xs = case break (== t) xs of (hd,tl) -> hd:case tl of (_:r@(_:_)) -> splitOnToken t r _ -> [] str2lsts = map (map read . splitOnToken ',') . splitOnToken '?' if things weren't enclosed in quotation marks inside the string, it would be the nicer map (splitOnToken ',') . splitOnToken '?' , provided of course, neither ',' nor '?' are valid characters for the target strings. import Text.ParserCombinators.Parsec simple = between (char '"') (char '"') (many (staisfy (/= '"'))) -- alternative: simple = char '"' >> manyTill anyChar (char '"') multiple = sepBy simple (char ',') total = sepBy multiple (char '?') str2lsts str = case parse total "" str of Left err -> error (show err) Right lsts -> lsts > > > > Thanks in advance for any idea. > > > > Luca From daniel.is.fischer at web.de Fri Oct 30 11:03:37 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Fri Oct 30 10:41:25 2009 Subject: [Haskell-beginners] list monad question In-Reply-To: <1256909341-sup-3304@pc44es141.cs.uni-magdeburg.de> References: <1256902955-sup-336@pc44es141.cs.uni-magdeburg.de> <200910301356.34688.daniel.is.fischer@web.de> <1256909341-sup-3304@pc44es141.cs.uni-magdeburg.de> Message-ID: <200910301603.37963.daniel.is.fischer@web.de> Am Freitag 30 Oktober 2009 14:32:35 schrieb Matthias Guedemann: > Hi Daniel, > > > That gives > > > > combinations n xs = foldr f [[]] (replicate n xs) > > > > pointfree, for extra goodness: > > > > -- pointfree f inline > > combinations n xs = foldr ((. (. (:)) . flip map) . (>>=)) [[]] > > (replicate n xs) -- eliminate xs > > combinations n = foldr ((. (. (:)) . flip map) . (>>=)) [[]] . replicate > > n -- completely pointfree > > combinations = (foldr ((. (. (:)) . flip map) . (>>=)) [[]] .) . > > replicate > > thank you, looks rather strange to me but works well. Yes :D The pointfree f is nicely obfuscated. But if your friend is a perl coder, he should be able to appreciate that. The standard way to express f, however, is liftM2 (:), so combinations = (foldr (liftM2 (:)) [[]] .) . replicate -- isn't that boring? But earnestly, replicateM is the thing to use. > > regards From shawn-haskell at willden.org Fri Oct 30 11:11:48 2009 From: shawn-haskell at willden.org (Shawn Willden) Date: Fri Oct 30 10:53:13 2009 Subject: [Haskell-beginners] beginner question In-Reply-To: References: Message-ID: <200910300911.48668.shawn-haskell@willden.org> On Friday 30 October 2009 07:40:13 am Luca Ciciriello wrote: > I need to write a function str2lsts :: String -> [[String]] in order to > transorm a string like: > > "\"1\",\"cat\",\"dog\"?\"2\",\"duck\",\"goose\"" > > in the list of lists: > > [["1","cat","dog"],["2","duck","goose"]] A variety of solutions on these blog posts, and the comments: http://gimbo.org.uk/blog/2007/04/20/splitting-a-string-in-haskell/ http://julipedia.blogspot.com/2006/08/split-function-in-haskell.html Shawn. From matthias.guedemann at ovgu.de Fri Oct 30 11:34:13 2009 From: matthias.guedemann at ovgu.de (Matthias Guedemann) Date: Fri Oct 30 11:11:58 2009 Subject: [Haskell-beginners] list monad question In-Reply-To: <200910301603.37963.daniel.is.fischer@web.de> References: <1256902955-sup-336@pc44es141.cs.uni-magdeburg.de> <200910301356.34688.daniel.is.fischer@web.de> <1256909341-sup-3304@pc44es141.cs.uni-magdeburg.de> <200910301603.37963.daniel.is.fischer@web.de> Message-ID: <1256916789-sup-138@pc44es141.cs.uni-magdeburg.de> > Yes :D The pointfree f is nicely obfuscated. But if your friend is a perl > coder, he should > be able to appreciate that. Honestly, he just wanted a "one-loop-using solution" and was not too interested in anything using Haskell :-) > The standard way to express f, however, is liftM2 (:), so > > combinations = (foldr (liftM2 (:)) [[]] .) . replicate > -- isn't that boring? true, it's almost readable At the moment trying to use pointfree is basically pointless for me, more practice is needed. regards Matthias -- __________________________________________________________ ___ __ __ Dipl. Inf. Matthias Guedemann / __\/ _\ /__\ Computer Systems in Engineering / / \ \ /_\ Otto-von-Guericke Universitaet Magdeburg / /___ _\ \//__ Tel.: 0391 / 67-19359 \____/ \__/\__/ __________________________________________________________ From luca_ciciriello at hotmail.com Fri Oct 30 11:51:46 2009 From: luca_ciciriello at hotmail.com (Luca Ciciriello) Date: Fri Oct 30 11:28:11 2009 Subject: [Haskell-beginners] beginner question In-Reply-To: <200910301546.34301.daniel.is.fischer@web.de> References: Message-ID: Thanks. import Text.ParserCombinators.Parsec simple = between (char '"') (char '"') (many (satisfy (/= '"'))) multiple = sepBy simple (char ',') total = sepBy multiple (char '@') str2lsts :: String -> [[String]] str2lsts str= case parse total "" str of Left err -> error (show err) Right lsts -> lsts solves my problem. As you can see I've replaced '?' with '@' and now all works fine. Luca. > From: daniel.is.fischer@web.de > To: beginners@haskell.org > Subject: Re: [Haskell-beginners] beginner question > Date: Fri, 30 Oct 2009 15:46:33 +0100 > > Am Freitag 30 Oktober 2009 14:40:13 schrieb Luca Ciciriello: > > Hi all. > > > > Just a very basic question. > > > > > > > > I need to write a function str2lsts :: String -> [[String]] in order to > > transorm a string like: > > > > > > > > "\"1\",\"cat\",\"dog\"?\"2\",\"duck\",\"goose\"" > > > > > > > > in the list of lists: > > > > > > > > [["1","cat","dog"],["2","duck","goose"]] > > > > > > > > I've tried to mix recursion, pattern matching and list comprehension, but > > the obtained result was embarrassing complex (> 20 lines of awful code). I > > think that a more simple solution certainly exists. > > > > splitOnToken :: Eq a => a -> [a] -> [[a]] > splitOnToken t xs > = case break (== t) xs of > (hd,tl) -> hd:case tl of > (_:r@(_:_)) -> splitOnToken t r > _ -> [] > > str2lsts = map (map read . splitOnToken ',') . splitOnToken '?' > > if things weren't enclosed in quotation marks inside the string, it would be the nicer > > map (splitOnToken ',') . splitOnToken '?' > > , provided of course, neither ',' nor '?' are valid characters for the target strings. > > import Text.ParserCombinators.Parsec > > simple = between (char '"') (char '"') (many (staisfy (/= '"'))) > -- alternative: simple = char '"' >> manyTill anyChar (char '"') > > multiple = sepBy simple (char ',') > > total = sepBy multiple (char '?') > > str2lsts str > = case parse total "" str of > Left err -> error (show err) > Right lsts -> lsts > > > > > > > > > Thanks in advance for any idea. > > > > > > > > Luca > > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners _________________________________________________________________ Download Messenger onto your mobile for free http://clk.atdmt.com/UKM/go/174426567/direct/01/ -------------- next part -------------- An HTML attachment was scrubbed... URL: http://www.haskell.org/pipermail/beginners/attachments/20091030/18dff5cb/attachment.html From colin at colina.demon.co.uk Fri Oct 30 11:55:01 2009 From: colin at colina.demon.co.uk (Colin Paul Adams) Date: Fri Oct 30 11:31:24 2009 Subject: [Haskell-beginners] list monad question In-Reply-To: <1256916789-sup-138@pc44es141.cs.uni-magdeburg.de> (Matthias Guedemann's message of "Fri\, 30 Oct 2009 16\:34\:13 +0100") References: <1256902955-sup-336@pc44es141.cs.uni-magdeburg.de> <200910301356.34688.daniel.is.fischer@web.de> <1256909341-sup-3304@pc44es141.cs.uni-magdeburg.de> <200910301603.37963.daniel.is.fischer@web.de> <1256916789-sup-138@pc44es141.cs.uni-magdeburg.de> Message-ID: >>>>> "Matthias" == Matthias Guedemann writes: Matthias> true, it's almost readable At the moment trying to use Matthias> pointfree is basically pointless for me, more practice Matthias> is needed. Actually the pointless style does have a point - like all jargon, it shows that you're a member of the club, and can look down on those who don't understand it. In support of this, it deliberately confuses beginners by omitting the arguments that should be there according to the signature (this is a principle purpose of currying, to confuse beginners :-) ). I cheat - I use hlint, and write the pointless versions that it tells me I should write instead of the pointed version that I write. Is there a "Bluffer's guide to Haskell"? -- Colin Adams Preston Lancashire From stephen.tetley at gmail.com Fri Oct 30 12:27:12 2009 From: stephen.tetley at gmail.com (Stephen Tetley) Date: Fri Oct 30 12:03:34 2009 Subject: [Haskell-beginners] list monad question In-Reply-To: References: <1256902955-sup-336@pc44es141.cs.uni-magdeburg.de> <200910301356.34688.daniel.is.fischer@web.de> <1256909341-sup-3304@pc44es141.cs.uni-magdeburg.de> <200910301603.37963.daniel.is.fischer@web.de> <1256916789-sup-138@pc44es141.cs.uni-magdeburg.de> Message-ID: <5fdc56d70910300927u53aa011leeb4884052903514@mail.gmail.com> 2009/10/30 Colin Paul Adams : > Is there a "Bluffer's guide to Haskell"? Whilst not a bluffers guide, this one contains several dozen flavours of 'obscurantism' (** add your less pejorative term here **) http://www.willamette.edu/~fruehr/haskell/evolution.html Methinks your being a bit hard on the pointfree style, but it does often diminish an _operation reading_ of the code (you can tell what the code does from looking at it). So you either have to trust it, or work it out to some expanded form you are happy with. For what its worth I came up with this bit of golf which saves a few keystrokes if you're prepare not to count the helper functions (I consider them generally useful): combinations :: Int -> [a] -> [[a]] combinations = foldr (<:>) [[]] `oo` replicate -- Helpers that I like but are not in the libraries -- | Applicative 'cons'. Equivalent to - liftA2 (:) - but I like having it around. -- The monadic version is attributable to a parser library in Clean. (<:>) :: Applicative f => f a -> f [a] -> f [a] (<:>) a b = (:) <$> a <*> b -- | Compose an arity 1 function and an arity 2 function. -- -- I call this combinator 'specs' (aka glasses) due to its infix -- appearance `oo` - I believe fans of Raymond Smullyan's -- 'To Mock a Mockingbird' call it blackbird... oo :: (c -> d) -> (a -> b -> c) -> a -> b -> d oo f g = (f .) . g Best wishes Stephen From byorgey at seas.upenn.edu Fri Oct 30 13:47:23 2009 From: byorgey at seas.upenn.edu (Brent Yorgey) Date: Fri Oct 30 13:23:45 2009 Subject: [Haskell-beginners] beginner question In-Reply-To: References: Message-ID: <20091030174722.GA10482@seas.upenn.edu> On Fri, Oct 30, 2009 at 01:40:13PM +0000, Luca Ciciriello wrote: > > Hi all. > > Just a very basic question. > > I need to write a function str2lsts :: String -> [[String]] in order to transorm a string like: > > "\"1\",\"cat\",\"dog\"?\"2\",\"duck\",\"goose\"" > > in the list of lists: > > [["1","cat","dog"],["2","duck","goose"]] Another possibility: import Data.List.Split -- from the 'split' package on Hackage str2lsts = map (splitOn ",") . splitOn "@" . filter (/='"') -Brent From daniel.is.fischer at web.de Fri Oct 30 17:03:50 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Fri Oct 30 16:41:40 2009 Subject: [Haskell-beginners] list monad question In-Reply-To: <5fdc56d70910300927u53aa011leeb4884052903514@mail.gmail.com> References: <1256902955-sup-336@pc44es141.cs.uni-magdeburg.de> <5fdc56d70910300927u53aa011leeb4884052903514@mail.gmail.com> Message-ID: <200910302203.50864.daniel.is.fischer@web.de> Am Freitag 30 Oktober 2009 17:27:12 schrieb Stephen Tetley: > 2009/10/30 Colin Paul Adams : > > Is there a "Bluffer's guide to Haskell"? > > Whilst not a bluffers guide, this one contains several dozen flavours > of 'obscurantism' (** add your less pejorative term here **) > > http://www.willamette.edu/~fruehr/haskell/evolution.html > > Methinks your being a bit hard on the pointfree style, but it does > often diminish an _operation reading_ of the code (you can tell what > the code does from looking at it). So you either have to trust it, or > work it out to some expanded form you are happy with. Completely pointfree style tends to be unreadable except for the selected few. It's fun to create pointfree versions of your functions, and you learn something by doing that, but it should appear rarely in your code. Completely pointful style tends to be not unreadable, but often cluttered. The most readable is usually a "partially pointfree" style. Which degree of pointfreeness is most readable depends of course on the reader, but there's a range which most can agree is good. Function combinators and pipelines should be partially pointfree: foo = bar . baz . hum is better than foo x = bar (baz (hum x)) or foo x = bar $ baz $ hum x or foo x = bar . baz . hum $ x flurb f g = f . g . f is better than flurb f g = f (g (f x)) wibble f g = f &&& g >>> g *** f is better than wibble f g x = f &&& g >>> g *** f $ x - but worse than wibble f g = (f &&& g) >>> (g *** f) because the latter doesn't require knowledge of the fixities to parse. However, wibble f g = (g . f) &&& (f . g) is at least as good if you want it only for the Category (->). Whether that is better than wibble f g x = (g (f x), f (g x)) depends on how familiar one is with Control.Arrow. Writing flurb or wibble completely pointfree is a nightmare :) Which is best: a) incrementAll n xs = map (\x -> x+n) xs b) incrementAll n xs = map (+n) xs c) incrementAll n = map (+n) d) incrementAll = map . (+) ? None of them is unreadable - though d) is confusing in the first few weeks of Haskell - but b) and c) are clearly better than the other two and c) is a bit better than b) in my opinion. Would you prefer: a) comb = flip (.) (flip (.)) (flip (.) (flip (.))) b) comb = (. flip (.)) . flip (.) c) comb f = (. f) . flip (.) d) comb f g = (. g) . f e) comb f g x = f x . g f) comb f g x y = f x (g y) ? a) is the prettiest, but honestly, I'd rather not meet it in code. e) is best, f) is okay, d) acceptable. > > For what its worth I came up with this bit of golf which saves a few > keystrokes if you're prepare not to count the helper functions That's cheating (until they are in a library). > (I consider them generally useful): Yup. > > > combinations :: Int -> [a] -> [[a]] > combinations = foldr (<:>) [[]] `oo` replicate > > -- Helpers that I like but are not in the libraries > > -- | Applicative 'cons'. Equivalent to - liftA2 (:) - but I like > having it around. > -- The monadic version is attributable to a parser library in Clean. > > (<:>) :: Applicative f => f a -> f [a] -> f [a] > (<:>) a b = (:) <$> a <*> b I'd prefer either (<:>) = liftA2 (:) or a <:> b = (:) <$> a <*> b > > -- | Compose an arity 1 function and an arity 2 function. > -- > -- I call this combinator 'specs' (aka glasses) due to its infix > -- appearance `oo` - I believe fans of Raymond Smullyan's > -- 'To Mock a Mockingbird' call it blackbird... > > oo :: (c -> d) -> (a -> b -> c) -> a -> b -> d > oo f g = (f .) . g This is sometimes also denoted by (.:), it has the pretty definition (.:) = (.) . (.) > > Best wishes > > Stephen From mpm at alumni.caltech.edu Sat Oct 31 08:32:51 2009 From: mpm at alumni.caltech.edu (Michael Mossey) Date: Sat Oct 31 08:09:16 2009 Subject: [Haskell-beginners] hierarchy of modules Message-ID: <4AEC2E73.6000107@alumni.caltech.edu> I want to have a hierarchy of modules for a local project. Not submitting it to Hackage yet. I just want to refer to my local modules as Basics.CSound Basics.Node Algo.Fux etc. how does one set this up? Thanks, Mike From daniel.is.fischer at web.de Sat Oct 31 08:54:31 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Sat Oct 31 08:32:13 2009 Subject: [Haskell-beginners] hierarchy of modules In-Reply-To: <4AEC2E73.6000107@alumni.caltech.edu> References: <4AEC2E73.6000107@alumni.caltech.edu> Message-ID: <200910311354.31475.daniel.is.fischer@web.de> Am Samstag 31 Oktober 2009 13:32:51 schrieb Michael Mossey: > I want to have a hierarchy of modules for a local project. Not submitting > it to Hackage yet. I just want to refer to my local modules as > > Basics.CSound > Basics.Node > Algo.Fux > > etc. > > how does one set this up? Top directory: project.cabal, Setup.hs (module Main where main = defaultMain) Subdirectry Basics: File CSound.hs (module Basics.CSound (exports) where...) File Node.hs (module Basics.Node (exports where...) Subdirectory Algo: File Fux.hs (module Algo.Fux (exports) where...) cd Top directory cabal install > > Thanks, > Mike From shawn-haskell at willden.org Sat Oct 31 12:27:10 2009 From: shawn-haskell at willden.org (Shawn Willden) Date: Sat Oct 31 12:03:33 2009 Subject: [Haskell-beginners] Class definition syntax Message-ID: <200910311027.10559.shawn-haskell@willden.org> I have a program that makes use of various data types built on top of Arrays. In some cases, they're data types that contain an Array plus some additonal information, in others, they're just "newtype" Arrays, so that I can use typechecking to make sure that I'm not using the wrong kind of object. I'd really like to define an "ArrayOps" class with all of the operations I need, and define instances for all of the specific types. I also use some "raw" Array objects, so it would be even better if I could make an instance of my class for Array. And, ideally, I'd like to use the Array operations for my class operations. So, I want something like: class ArrayOps a where (!) :: a -> i -> e (//) :: a -> (i,e) -> a bounds :: a -> (i,i) range :: a -> [i] 'i' and 'e' are the index and element types, respectively. Obviously, the signatures above reference type variables that aren't declared, and really must be constrained to be the 'i' and 'e' that were used in building the type 'a' (which is an Array i e). Something like the following (though this obviously doesn't work): class ((Array.Array i e) a) => ArrayOps a where ... I'm sure there must be a way to do this, but I can't figure out what the syntax would look like. Thanks, Shawn. From jfredett at gmail.com Sat Oct 31 12:33:10 2009 From: jfredett at gmail.com (Joe Fredette) Date: Sat Oct 31 12:09:32 2009 Subject: [Haskell-beginners] Class definition syntax In-Reply-To: <200910311027.10559.shawn-haskell@willden.org> References: <200910311027.10559.shawn-haskell@willden.org> Message-ID: <17D619C7-3D98-4190-BC9D-73575C75FE01@gmail.com> You'll probably need to look at associated types/functional dependencies. The former is the new hotness, the latter is the old and not-so-busted. A quick search of the wiki ought to reveal much more than I can possibly explain, there is an example on the page for Assoc. Types about generic Map implementation, which is similar to what you're trying to do. On Oct 31, 2009, at 12:27 PM, Shawn Willden wrote: > I have a program that makes use of various data types built on top > of Arrays. > In some cases, they're data types that contain an Array plus some > additonal > information, in others, they're just "newtype" Arrays, so that I can > use > typechecking to make sure that I'm not using the wrong kind of object. > > I'd really like to define an "ArrayOps" class with all of the > operations I > need, and define instances for all of the specific types. I also use > some "raw" Array objects, so it would be even better if I could make > an > instance of my class for Array. And, ideally, I'd like to use the > Array > operations for my class operations. > > So, I want something like: > > class ArrayOps a where > (!) :: a -> i -> e > (//) :: a -> (i,e) -> a > bounds :: a -> (i,i) > range :: a -> [i] > > 'i' and 'e' are the index and element types, respectively. > > Obviously, the signatures above reference type variables that aren't > declared, > and really must be constrained to be the 'i' and 'e' that were used in > building the type 'a' (which is an Array i e). Something like the > following > (though this obviously doesn't work): > > class ((Array.Array i e) a) => ArrayOps a where ... > > I'm sure there must be a way to do this, but I can't figure out what > the > syntax would look like. > > Thanks, > > Shawn. > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners From estebistec at gmail.com Sat Oct 31 12:51:29 2009 From: estebistec at gmail.com (Steven Cummings) Date: Sat Oct 31 12:27:49 2009 Subject: [Haskell-beginners] hierarchy of modules In-Reply-To: <200910311354.31475.daniel.is.fischer@web.de> References: <4AEC2E73.6000107@alumni.caltech.edu> <200910311354.31475.daniel.is.fischer@web.de> Message-ID: What if he wanted to separately have test code? e.g. Sources and Tests each containing identical hierarchies described above. I know cabal has an option for source directory. It appears something like [1] can be use to run all tests, though I haven't yet looked into the details of how each file needs to be structured so that all tests are detected (if, e.g., your using quickcheck to implement your tests). Ideally, from the root directory I would want to be able to do cabal install or cabal test. [1] http://hackage.haskell.org/package/cabal-test -- Steven On Sat, Oct 31, 2009 at 7:54 AM, Daniel Fischer wrote: > Am Samstag 31 Oktober 2009 13:32:51 schrieb Michael Mossey: > > I want to have a hierarchy of modules for a local project. Not submitting > > it to Hackage yet. I just want to refer to my local modules as > > > > Basics.CSound > > Basics.Node > > Algo.Fux > > > > etc. > > > > how does one set this up? > > Top directory: project.cabal, Setup.hs (module Main where main = > defaultMain) > Subdirectry Basics: > File CSound.hs (module Basics.CSound (exports) where...) > File Node.hs (module Basics.Node (exports where...) > Subdirectory Algo: > File Fux.hs (module Algo.Fux (exports) where...) > > cd Top directory > cabal install > > > > > Thanks, > > Mike > > > _______________________________________________ > 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/20091031/3f31796e/attachment.html From daniel.is.fischer at web.de Sat Oct 31 12:50:10 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Sat Oct 31 12:27:54 2009 Subject: [Haskell-beginners] Class definition syntax In-Reply-To: <17D619C7-3D98-4190-BC9D-73575C75FE01@gmail.com> References: <200910311027.10559.shawn-haskell@willden.org> <17D619C7-3D98-4190-BC9D-73575C75FE01@gmail.com> Message-ID: <200910311750.10712.daniel.is.fischer@web.de> Am Samstag 31 Oktober 2009 17:33:10 schrieb Joe Fredette: > You'll probably need to look at associated types/functional > dependencies. The former is the new hotness, the latter is the old and > not-so-busted. A quick search of the wiki ought to reveal much more > than I can possibly explain, there is an example on the page for > Assoc. Types about generic Map implementation, which is similar to > what you're trying to do. Or perhaps he should look at the class IArray from Data.Array.IArray, maybe he can just declare instances of IArray for his datatypes. Without more information, I can't tell which way to go. > > On Oct 31, 2009, at 12:27 PM, Shawn Willden wrote: > > I have a program that makes use of various data types built on top > > of Arrays. > > In some cases, they're data types that contain an Array plus some > > additonal > > information, in others, they're just "newtype" Arrays, so that I can > > use > > typechecking to make sure that I'm not using the wrong kind of object. > > > > I'd really like to define an "ArrayOps" class with all of the > > operations I > > need, and define instances for all of the specific types. I also use > > some "raw" Array objects, so it would be even better if I could make > > an > > instance of my class for Array. And, ideally, I'd like to use the > > Array > > operations for my class operations. > > > > So, I want something like: > > > > class ArrayOps a where > > (!) :: a -> i -> e > > (//) :: a -> (i,e) -> a > > bounds :: a -> (i,i) > > range :: a -> [i] > > > > 'i' and 'e' are the index and element types, respectively. > > > > Obviously, the signatures above reference type variables that aren't > > declared, > > and really must be constrained to be the 'i' and 'e' that were used in > > building the type 'a' (which is an Array i e). Something like the > > following > > (though this obviously doesn't work): > > > > class ((Array.Array i e) a) => ArrayOps a where ... > > > > I'm sure there must be a way to do this, but I can't figure out what > > the > > syntax would look like. > > > > Thanks, > > > > Shawn. From mpm at alumni.caltech.edu Sat Oct 31 13:19:24 2009 From: mpm at alumni.caltech.edu (Michael Mossey) Date: Sat Oct 31 12:55:51 2009 Subject: [Haskell-beginners] hierarchy of modules In-Reply-To: <200910311354.31475.daniel.is.fischer@web.de> References: <4AEC2E73.6000107@alumni.caltech.edu> <200910311354.31475.daniel.is.fischer@web.de> Message-ID: <4AEC719C.3020105@alumni.caltech.edu> Hi Daniel, Thanks for the information. However, does this work for a test-compile-debug cycle, or for a modify-interpret cycle? These modules would all be in development. I don't like the idea that I have to 'cabal install' after any change to any module. Is that necessary? Thanks, Mike Daniel Fischer wrote: > Am Samstag 31 Oktober 2009 13:32:51 schrieb Michael Mossey: >> I want to have a hierarchy of modules for a local project. Not submitting >> it to Hackage yet. I just want to refer to my local modules as >> >> Basics.CSound >> Basics.Node >> Algo.Fux >> >> etc. >> >> how does one set this up? > > Top directory: project.cabal, Setup.hs (module Main where main = defaultMain) > Subdirectry Basics: > File CSound.hs (module Basics.CSound (exports) where...) > File Node.hs (module Basics.Node (exports where...) > Subdirectory Algo: > File Fux.hs (module Algo.Fux (exports) where...) > > cd Top directory > cabal install > >> Thanks, >> Mike > > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners From daniel.is.fischer at web.de Sat Oct 31 14:43:20 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Sat Oct 31 14:21:04 2009 Subject: [Haskell-beginners] hierarchy of modules In-Reply-To: <4AEC719C.3020105@alumni.caltech.edu> References: <4AEC2E73.6000107@alumni.caltech.edu> <200910311354.31475.daniel.is.fischer@web.de> <4AEC719C.3020105@alumni.caltech.edu> Message-ID: <200910311943.20529.daniel.is.fischer@web.de> Am Samstag 31 Oktober 2009 18:19:24 schrieb Michael Mossey: > Hi Daniel, > > Thanks for the information. However, does this work for a > test-compile-debug cycle, or for a modify-interpret cycle? These modules > would all be in development. I don't like the idea that I have to 'cabal > install' after any change to any module. Is that necessary? No, that would be the step when develpment is done (for the time being). While developing, have the same source-tree (you can add the .cabal file and Setup.hs later) and just work in ghci (compile what's not currently being worked on, so the code runs faster). I can't guarantee that it works everywhere and with all versions of GHC, but it works here. > > Thanks, > Mike > > Daniel Fischer wrote: > > Am Samstag 31 Oktober 2009 13:32:51 schrieb Michael Mossey: > >> I want to have a hierarchy of modules for a local project. Not > >> submitting it to Hackage yet. I just want to refer to my local modules > >> as > >> > >> Basics.CSound > >> Basics.Node > >> Algo.Fux > >> > >> etc. > >> > >> how does one set this up? > > > > Top directory: project.cabal, Setup.hs (module Main where main = > > defaultMain) Subdirectry Basics: > > File CSound.hs (module Basics.CSound (exports) where...) > > File Node.hs (module Basics.Node (exports where...) > > Subdirectory Algo: > > File Fux.hs (module Algo.Fux (exports) where...) > > > > cd Top directory > > cabal install > > > >> Thanks, > >> Mike From stephen.tetley at gmail.com Sat Oct 31 15:33:53 2009 From: stephen.tetley at gmail.com (Stephen Tetley) Date: Sat Oct 31 15:10:14 2009 Subject: [Haskell-beginners] hierarchy of modules In-Reply-To: <4AEC719C.3020105@alumni.caltech.edu> References: <4AEC2E73.6000107@alumni.caltech.edu> <200910311354.31475.daniel.is.fischer@web.de> <4AEC719C.3020105@alumni.caltech.edu> Message-ID: <5fdc56d70910311233w2cbfd9f6v9f80c8f7adc5ab71@mail.gmail.com> Hello Mike An option I like is to put the main project files in a top level directory called src, so your organization would look like this: ./src/Algo/Fux.hs ./src/Basics/CSound.hs ./src/Basics/Node.hs I usually have a running example which has imports for all the files in the project inside a top level folder called demo... ./demo/ImportAll.hs For a GHCi session, I cd to $PROJECT/demo then > ghci Prelude> set -i../src This is the useful bit of having all the project files under the src hierarchy - the search path is very simple. During development adding the files needed for cabal (Setup.hs, project.cabal...) is still worthwhile - you can then generate Haddock docs very easily. If you used Haddock in standalone mode you would have to supply a few flags to tell which files to document and where to put the output. Each time you add or remove a module in the src tree, you should re-run configure... > runhaskell Setup.hs configure > runhaskell Setup.hs haddock but as your GHCi session is always using the interpreted project source you don't need to run 'runhaskell Setup.hs build' and 'runhaskell Setup.hs install'. Best wishes Stephen From keithshep at gmail.com Sat Oct 31 15:39:57 2009 From: keithshep at gmail.com (Keith Sheppard) Date: Sat Oct 31 15:16:16 2009 Subject: [Haskell-beginners] Ord [] instance Message-ID: <92e42b740910311239pf614067w9eebcd51c7db20d2@mail.gmail.com> the compare function for lists seems to be work like (leaving off class details): > compare [] [] = EQ > compare [] _ = LT > compare _ [] = GT > compare (x:xt) (y:yt) = case x `compare` y of > EQ -> xt `compare` yt > _ -> x `compare` y I poked around to see if I could find where this was defined in the spec or if it was an implementation specific thing (I need to be able to rely on this definition across implementations). I found this text in the report: -- Lists data [a] = [] | a : [a] deriving (Eq, Ord) -- Not legal Haskell; for illustration only Is this basically saying the same thing as my compare definition? Thanks Keith -- keithsheppard.name From daniel.is.fischer at web.de Sat Oct 31 16:01:46 2009 From: daniel.is.fischer at web.de (Daniel Fischer) Date: Sat Oct 31 15:39:29 2009 Subject: [Haskell-beginners] Ord [] instance In-Reply-To: <92e42b740910311239pf614067w9eebcd51c7db20d2@mail.gmail.com> References: <92e42b740910311239pf614067w9eebcd51c7db20d2@mail.gmail.com> Message-ID: <200910312101.46296.daniel.is.fischer@web.de> Am Samstag 31 Oktober 2009 20:39:57 schrieb Keith Sheppard: > the compare function for lists seems to be work like (leaving off > > class details): > > compare [] [] = EQ > > compare [] _ = LT > > compare _ [] = GT > > compare (x:xt) (y:yt) = case x `compare` y of > > EQ -> xt `compare` yt > > _ -> x `compare` y > > I poked around to see if I could find where this was defined in the > spec or if it was an implementation specific thing (I need to be able > to rely on this definition across implementations). You can rely on lexicographic ordering for lists. Haskell Report, section 10.1 ( http://haskell.org/onlinereport/derived.html#derived- appendix ): 10.1 Derived instances of Eq and Ord The class methods automatically introduced by derived instances of Eq and Ord are (==), (/=), compare, (<), (<=), (>), (>=), max, and min. The latter seven operators are defined so as to compare their arguments lexicographically with respect to the constructor set given, with earlier constructors in the datatype declaration counting as smaller than later ones. > I found this text in the report: > > -- Lists > > > data [a] = [] | a : [a] deriving (Eq, Ord) > -- Not legal Haskell; for illustration only > > Is this basically saying the same thing as my compare definition? Yes, see above. > > Thanks > Keith From keithshep at gmail.com Sat Oct 31 16:16:55 2009 From: keithshep at gmail.com (Keith Sheppard) Date: Sat Oct 31 15:53:13 2009 Subject: [Haskell-beginners] Ord [] instance In-Reply-To: <200910312101.46296.daniel.is.fischer@web.de> References: <92e42b740910311239pf614067w9eebcd51c7db20d2@mail.gmail.com> <200910312101.46296.daniel.is.fischer@web.de> Message-ID: <92e42b740910311316y2061ed23q7f0630a0415e2f8c@mail.gmail.com> Thank you -Keith From shawn-haskell at willden.org Sat Oct 31 21:47:15 2009 From: shawn-haskell at willden.org (Shawn Willden) Date: Sat Oct 31 21:23:37 2009 Subject: [Haskell-beginners] Class definition syntax In-Reply-To: <17D619C7-3D98-4190-BC9D-73575C75FE01@gmail.com> References: <200910311027.10559.shawn-haskell@willden.org> <17D619C7-3D98-4190-BC9D-73575C75FE01@gmail.com> Message-ID: <200910311947.16094.shawn-haskell@willden.org> On Saturday 31 October 2009 10:33:10 am Joe Fredette wrote: > You'll probably need to look at associated types/functional > dependencies. The former is the new hotness, the latter is the old and > not-so-busted. A quick search of the wiki ought to reveal much more > than I can possibly explain, there is an example on the page for > Assoc. Types about generic Map implementation, which is similar to > what you're trying to do. Hmm. That looks like it will require a deeper dive into the theory than I want to make right now. I don't yet understand what a kind is yet, much less how to write an appropriate kind signature. It appears to be a somewhat disconcerting fact that learning Haskell requires reading original research papers on type theory and lambda calculus. I suppose I need to get over my reluctance and dive into some of that -- but I lack the big slabs of time needed to immerse myself in it enough to make useful progress. Learning Haskell appears to be something one should do as a college student :-) Shawn. From shawn-haskell at willden.org Sat Oct 31 21:55:02 2009 From: shawn-haskell at willden.org (Shawn Willden) Date: Sat Oct 31 21:31:28 2009 Subject: [Haskell-beginners] Class definition syntax In-Reply-To: <200910311750.10712.daniel.is.fischer@web.de> References: <200910311027.10559.shawn-haskell@willden.org> <17D619C7-3D98-4190-BC9D-73575C75FE01@gmail.com> <200910311750.10712.daniel.is.fischer@web.de> Message-ID: <200910311955.02397.shawn-haskell@willden.org> On Saturday 31 October 2009 10:50:10 am Daniel Fischer wrote: > Am Samstag 31 Oktober 2009 17:33:10 schrieb Joe Fredette: > > You'll probably need to look at associated types/functional > > dependencies. The former is the new hotness, the latter is the old and > > not-so-busted. A quick search of the wiki ought to reveal much more > > than I can possibly explain, there is an example on the page for > > Assoc. Types about generic Map implementation, which is similar to > > what you're trying to do. > > Or perhaps he should look at the class IArray from Data.Array.IArray, maybe > he can just declare instances of IArray for his datatypes. > Without more information, I can't tell which way to go. This gives me some ideas, whether to use IArray directly, or just to use it as a model for my own class. Thanks, Shawn. From shawn-haskell at willden.org Sat Oct 31 22:36:26 2009 From: shawn-haskell at willden.org (Shawn Willden) Date: Sat Oct 31 22:12:47 2009 Subject: [Haskell-beginners] Class definition syntax In-Reply-To: <200910311750.10712.daniel.is.fischer@web.de> References: <200910311027.10559.shawn-haskell@willden.org> <17D619C7-3D98-4190-BC9D-73575C75FE01@gmail.com> <200910311750.10712.daniel.is.fischer@web.de> Message-ID: <200910312036.26455.shawn-haskell@willden.org> On Saturday 31 October 2009 10:50:10 am Daniel Fischer wrote: > Or perhaps he should look at the class IArray from Data.Array.IArray, maybe > he can just declare instances of IArray for his datatypes. > Without more information, I can't tell which way to go. Looking into the idea of declaring my types as IArray instances, there's one immediate problem: IArray's only method is "bounds". All of the functions that I want as methods of my class are functions in the IArray module (if I'm reading it correctly). So, it seems like what I want to do is to subclass IArray and add the additional methods. Then I can declare instances for my various types and define the methods appropriately. So, I wrote this: ------------------------------------ import Data.Ix (Ix, inRange) import qualified Data.Array.IArray (IArray, Array, array, listArray, range, bounds, (!)) listArray = Data.Array.IArray.listArray array = Data.Array.IArray.array class (Data.Array.IArray.IArray a e) => MyArray a e where bounds :: Ix i => a i e -> (i,i) range :: Ix i => a i e -> [i] (!) :: Ix i => a i e -> i -> e (//) :: Ix i => a i e -> [(i,e)] type Location = (Int, Int) newtype Board = Board (Data.Array.IArray.Array Location Int) instance MyArray Board where bounds = Data.Array.IArray.bounds (!) = (Data.Array.IArray.!) -------------------------------------- However, the instance declaration gives me a "kind mis-match" error. It says that it expects kind '* -> * -> *', but Board has kind '*'. So, I tried: instance MyArray (Board Data.Array.IArray.Array Location Int) where and other variations on that, but they all give me "Board is applied to too many type arguments". How should this be written? Thanks, Shawn. From jfredett at gmail.com Sat Oct 31 22:55:56 2009 From: jfredett at gmail.com (Joe Fredette) Date: Sat Oct 31 22:32:16 2009 Subject: [Haskell-beginners] Class definition syntax In-Reply-To: <200910312036.26455.shawn-haskell@willden.org> References: <200910311027.10559.shawn-haskell@willden.org> <17D619C7-3D98-4190-BC9D-73575C75FE01@gmail.com> <200910311750.10712.daniel.is.fischer@web.de> <200910312036.26455.shawn-haskell@willden.org> Message-ID: Well, I think the issue is you're thinking too OOPy... But let me answer the actual problem first, type classes are (basically) functions on types. So a type of "kind" `* -> * -> *` means it is a type which accepts two type variables. So: newtype Foo a b = Foo (a, b) is a type of "kind" * -> * -> *, and if I wanted to implement the IArray class, I would write: instance IArray Foo where ... because IArray is a type-function of type: "(* -> * -> *) - > ..." (this is a little stretched, I think, but you get the idea. tl;dr is that "Board" doesn't have enough type arguments to be an IArray. However, I think this is part of a bigger problem. By way of analogy, consider the Ord class, it implements things like `sort` as derived functions, not as parts of the class. Classes (at least in my estimation) are more like sets of axioms from math than like interfaces from OOP. So one doesn't so much "subclass" something as "add more assumptions" to it. So for instance, I can say, "assume a variable of type `a` which implements the Eq class", then if I want, I can say, "such a variable implements the Ord class if and only if it provides a `compare` or `<=` function". So, while I'm not sure of the specifics of your application and the abilities of IArray. Perhaps it is better to think about how to implement your functions in terms of the `bounds` function. In fact, this is what you do, but I think you're getting caught up in the type-classyness. Saying newtype Board = Board IArray ... means that _you can just use the IArray types_! Well, almost, really what you want is a type-synonym: type Board = IArray Location ... Now you can write functions like foo :: Board -> Int foo = Board !! (1,2) and it will "just work" because Board _is_ an "IArray". Hope that makes sense... On Oct 31, 2009, at 10:36 PM, Shawn Willden wrote: > On Saturday 31 October 2009 10:50:10 am Daniel Fischer wrote: >> Or perhaps he should look at the class IArray from >> Data.Array.IArray, maybe >> he can just declare instances of IArray for his datatypes. >> Without more information, I can't tell which way to go. > > Looking into the idea of declaring my types as IArray instances, > there's one > immediate problem: IArray's only method is "bounds". All of the > functions > that I want as methods of my class are functions in the IArray > module (if I'm > reading it correctly). > > So, it seems like what I want to do is to subclass IArray and add the > additional methods. Then I can declare instances for my various > types and > define the methods appropriately. > > So, I wrote this: > > ------------------------------------ > import Data.Ix (Ix, inRange) > import qualified Data.Array.IArray (IArray, > Array, > array, > listArray, > range, > bounds, > (!)) > > listArray = Data.Array.IArray.listArray > array = Data.Array.IArray.array > > class (Data.Array.IArray.IArray a e) => MyArray a e where > bounds :: Ix i => a i e -> (i,i) > range :: Ix i => a i e -> [i] > (!) :: Ix i => a i e -> i -> e > (//) :: Ix i => a i e -> [(i,e)] > > type Location = (Int, Int) > newtype Board = Board (Data.Array.IArray.Array Location Int) > > instance MyArray Board where > bounds = Data.Array.IArray.bounds > (!) = (Data.Array.IArray.!) > -------------------------------------- > > However, the instance declaration gives me a "kind mis-match" > error. It says > that it expects kind '* -> * -> *', but Board has kind '*'. > > So, I tried: > > instance MyArray (Board Data.Array.IArray.Array Location Int) where > > and other variations on that, but they all give me "Board is applied > to too > many type arguments". > > How should this be written? > > Thanks, > > Shawn. > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners From shawn-haskell at willden.org Sat Oct 31 23:42:24 2009 From: shawn-haskell at willden.org (Shawn Willden) Date: Sat Oct 31 23:18:51 2009 Subject: [Haskell-beginners] Class definition syntax In-Reply-To: References: <200910311027.10559.shawn-haskell@willden.org> <200910312036.26455.shawn-haskell@willden.org> Message-ID: <200910312142.24975.shawn-haskell@willden.org> On Saturday 31 October 2009 08:55:56 pm Joe Fredette wrote: > Well, I think the issue is you're thinking too OOPy... I understand what you're saying, but I don't think I am. > But let me answer the actual problem first, type classes are > (basically) functions on types. So a type of "kind" `* -> * -> *` > means it is a type which accepts two type variables. So: > > newtype Foo a b = Foo (a, b) Okay, that makes sense. What I'd read about kinds was considerably less clear. Thanks. > newtype Board = Board IArray ... > > means that _you can just use the IArray types_! Well, almost, really > what you want is a type-synonym: > > type Board = IArray Location ... > > Now you can write functions like > > foo :: Board -> Int > foo = Board !! (1,2) > > and it will "just work" because Board _is_ an "IArray". > > Hope that makes sense... It does make sense, but it doesn't solve my problem. See, Board isn't the only type I have (and, also, Board has to be a newtype rather than a type synonym because it's also an instance of another class -- well, unless I want to turn on the extension that allows instances of synonyms, and I'm not sure what the etiquette is there), and some of the others aren't just IArrays with an aliased name, they have other data elements as well. For example: data ScoredBoard = ScoredBoard { arry :: (IArray Location String) score :: Int maxScore :: Int } I would like to be able to use (!), (//), bound, range, etc., on those as well, and without having to say "range (arry sb)", or having to define a bunch of fooRange, barRange, bazRange, etc., functions. Basically I want to take this set of common array operations and overload them for a bunch of different types. As I understand it, classes are effectively the only way to overload in Haskell. Perhaps it just isn't possible to do what I want? If kind signatures must match, then that's a problem, because different types will have different numbers of construction parameters. Thanks for the help, Shawn. From mpm at alumni.caltech.edu Sat Oct 31 23:48:48 2009 From: mpm at alumni.caltech.edu (Michael Mossey) Date: Sat Oct 31 23:25:14 2009 Subject: [Haskell-beginners] ErrorT Identity Message-ID: <4AED0520.3060404@alumni.caltech.edu> Does using ErrorT Identity have advantages over Either? -Mike From jfredett at gmail.com Sat Oct 31 23:53:16 2009 From: jfredett at gmail.com (Joe Fredette) Date: Sat Oct 31 23:29:36 2009 Subject: [Haskell-beginners] Class definition syntax In-Reply-To: <200910312142.24975.shawn-haskell@willden.org> References: <200910311027.10559.shawn-haskell@willden.org> <200910312036.26455.shawn-haskell@willden.org> <200910312142.24975.shawn-haskell@willden.org> Message-ID: <992836AC-4C2B-45B1-8AEA-8809989C273D@gmail.com> Ahh, I see what you need, you want to "lift" the IArray functions into your type. Well, Rather than trying to instance the type- you could define your type like this: newtype Board = Board IArray.IArray ... whatever (!) :: Board -> Location -> Int (!) = IArray.(!) That is, create synonyms manually for each function you _absolutely need, assuming they don't conflict elsewhere. You would have to manually import each -- I feel like there is probably a better way to do this, but this will definitely work. Though, I'm not sure why you'd need to be instancing another class with a type like this, it's a _very_ specific type, I imagine one or the other set of functions ought to be easy enough to define simply about the type (dodging the typeclass entirely). I imagine extensibility comes to play here. One thing you might be able to do is class IArray a Location Int , OtherClass a ... => MyClass a ... where Which would force you to have a type which is an IArray of Location -> Ints, and an OtherClass, etc. I don't know all the details of your implementation, so I don't know how well this would work, but I imagine thats probably the "better" solution I'm thinking of... /Joe On Oct 31, 2009, at 11:42 PM, Shawn Willden wrote: > On Saturday 31 October 2009 08:55:56 pm Joe Fredette wrote: >> Well, I think the issue is you're thinking too OOPy... > > I understand what you're saying, but I don't think I am. > >> But let me answer the actual problem first, type classes are >> (basically) functions on types. So a type of "kind" `* -> * -> *` >> means it is a type which accepts two type variables. So: >> >> newtype Foo a b = Foo (a, b) > > Okay, that makes sense. What I'd read about kinds was considerably > less > clear. Thanks. > >> newtype Board = Board IArray ... >> >> means that _you can just use the IArray types_! Well, almost, really >> what you want is a type-synonym: >> >> type Board = IArray Location ... >> >> Now you can write functions like >> >> foo :: Board -> Int >> foo = Board !! (1,2) >> >> and it will "just work" because Board _is_ an "IArray". >> >> Hope that makes sense... > > It does make sense, but it doesn't solve my problem. See, Board > isn't the > only type I have (and, also, Board has to be a newtype rather than a > type > synonym because it's also an instance of another class -- well, > unless I want > to turn on the extension that allows instances of synonyms, and I'm > not sure > what the etiquette is there), and some of the others aren't just > IArrays with > an aliased name, they have other data elements as well. For example: > > data ScoredBoard = ScoredBoard { > arry :: (IArray Location String) > score :: Int > maxScore :: Int > } > > I would like to be able to use (!), (//), bound, range, etc., on > those as > well, and without having to say "range (arry sb)", or having to > define a > bunch of fooRange, barRange, bazRange, etc., functions. > > Basically I want to take this set of common array operations and > overload them > for a bunch of different types. As I understand it, classes are > effectively > the only way to overload in Haskell. > > Perhaps it just isn't possible to do what I want? If kind > signatures must > match, then that's a problem, because different types will have > different > numbers of construction parameters. > > Thanks for the help, > > Shawn.