Seeking a small bug-fix for previously-posted code

Hi all - I'm trying to run some code posted recently on this list ( the exact URI is as follows - http://comments.gmane.org/gmane.comp.lang.haskell.cafe/7860 ( The reason I'm doing this is because this code is quite close to what I'm trying to do. ) Anyway, I saved the code in a file called andy1.hs . The exact code is as follows - module Main where readVecs :: String -> IO [Vector3 GLfloat] readVecs f = do str <- readFile f let mkVec [x,y,z] = Vector3 x y z return $ map (mkVec . read . words) $ lines str ( I can see what the code seems to be doing : it's reading the file line-by-line, splitting it into "words", and filling the vectors with them. Not too bad a guess for a "Haskellista" of a few minutes standing ..... ;-) I created a small file with whitespace-separated floats so that I could run the above code. This data file looks like this - 23.596 38.201 79.903 32.647 65.559 15.438 21.648 34.978 90.089 55.587 76.763 96.634 When I run the above code (in GHCi , version 6.4) I get the message - Prelude> :load andy1 Compiling Main ( andy1.hs, interpreted ) andy1.hs:4:7: The last statement in a 'do' construct must be an expression Failed, modules loaded: none. So - I'm just wondering if anyone can see how the above code should be fixed in order to run. And just one last question - If the file looked like this (with column headings ) Name Ethnicity Age Mary_Smith NZ_European 27 Joe_Brown NZ_European 34 John_Watson Canadian 45 Mike_Tangaroa NZ_Maori 38 - what change would the above code need to run? Very many thanks in advance ... - Andy

Andy, Just a quick hack: module Main where import List -- ADDED import Graphics.UI.GLUT -- ADDED readVecs :: String -> IO [Vector3 GLfloat] readVecs f = do str <- readFile f -- FIXED LAYOUT: let mkVec [x,y,z] = Vector3 x y z let format line = "[" ++ concat (intersperse "," (words line)) ++ "]" -- ADDED return $ map (mkVec . read . format) $ lines str -- CORRECTED So, the error message you got was layout-related, i.e., you needed to move the last two lines of you programs two positions to the left. Furthermore, you had to import the HOpenGL modules. Even then, your code did not type check, so I replaced the application of words in the last line by an application to a user-defined function format, that not even makes it type check but also makes it actually reformat the input so it can be consumed by read. I used intersperse to do this, so I also needed to import the List module. But, seriously, do you really think this is the way to learn Haskell?
And just one last question - If the file looked like this (with column headings )
Name Ethnicity Age Mary_Smith NZ_European 27 Joe_Brown NZ_European 34 John_Watson Canadian 45 Mike_Tangaroa NZ_Maori 38
- what change would the above code need to run?
I hope you don't mind I leave this to you as an exercise. HTH, Stefan

Hello Stefan, Sunday, August 14, 2005, 2:05:00 PM, you wrote: SH> let format line = "[" ++ concat (intersperse "," (words line)) ++ "]" SH> return $ map (mkVec . read . format) $ lines str -- CORRECTED or just return $ map (mkVec . map read . words) $ lines str -- Best regards, Bulat mailto:bulatz@HotPOP.com

Bulat,
or just
return $ map (mkVec . map read . words) $ lines str
Yeah, I wrote that one down a little bit later, but didn't really find it worth mentioning. ;) As said, I don't really think this kind of programs is suitable for learning Haskell, anyway. Regards, Stefan

Bulat Ziganshin wrote:
Hello Stefan,
Sunday, August 14, 2005, 2:05:00 PM, you wrote:
SH> let format line = "[" ++ concat (intersperse "," (words line)) ++ "]" SH> return $ map (mkVec . read . format) $ lines str -- CORRECTED
or just
return $ map (mkVec . map read . words) $ lines str
Hi Bulat and Stefan - Thanks very much for your help - very much appreciated! In spite of being a newcomer, I'm very much sold on Haskell's elegance and clean syntax. On the subject of whether this is a good way to learn Haskell - granted, it's probably not ideal. However, I've already been through the GHC user guide (and also visited the "PLEAC" site on Sourceforge). I didn't find quite the code that I was looking for in either place, so I scanned the archives of this list as well (where I found the previously-posted code). So that's three places ... If there were a "Haskell cookbook" (as there is a Python one), I would have gone straight there. I regularly use the "Python cookbook" site which is very useful . However, until there's a Haskell equivalent (which then newcomers could be pointed at) , it's pretty much a case of "finding what one can" ... :-) Many thanks again - I'll make this my last post for at least a week or two (to give others some bandwidth) - - Andy

Hello Andy, Tuesday, August 16, 2005, 2:58:43 AM, you wrote: AE> If there were a "Haskell cookbook" (as there is a Python one), I AE> would have gone straight there. I regularly use the "Python cookbook" AE> site which is very useful . However, until there's a Haskell equivalent AE> (which then newcomers could be pointed at) , it's pretty much a case of AE> "finding what one can" ... :-) i had an idea of creating Haskell cookbook by repeating the Perl's one, but i think, that the some questions complex for Perl would be trivial for Haskell, and vice versa i can recommend you two books for beginners: "Gentle inroduction to Haskell" http://www.haskell.org/tutorial/haskell-98-tutorial-html.tar.gz "Yet Another Haskell Tutorial" by Hal Daume III http://www.isi.edu/~hdaume/htut/tutorial.pdf and book + PowerPoint tutorial by Graham Hutton: http://www.cs.nott.ac.uk/~gmh/preview.pdf http://www.cs.nott.ac.uk/~gmh/mgs-appsem1.ppt http://www.cs.nott.ac.uk/~gmh/mgs-appsem2.ppt http://www.cs.nott.ac.uk/~gmh/mgs-appsem3.ppt http://www.cs.nott.ac.uk/~gmh/mgs-appsem-exam.pdf http://www.cs.nott.ac.uk/~gmh/countdown.pdf btw, in the "another haskell tutorial" we can read: There have been many books and tutorials written about Haskell; for a (nearly) complete list, visit the http://haskell.org/bookshelf (Haskell Bookshelf) at the Haskell homepage. A brief survey of the tutorials available yields: - A Gentle Introduction to Haskell is an introduction to Haskell, given that the reader is familiar with functional programming en large. - Haskell Companion is a short reference of common concepts and definitions. - Online Haskell Course is a short course (in German) for beginning with Haskell. - Two Dozen Short Lessons in Haskell is the draft of an excellent textbook that emphasizes user involvement. - Haskell Tutorial is based on a course given at the 3rd International Summer School on Advanced Functional Programming. - Haskell for Miranda Programmers assumes knowledge of the language Miranda. - PLEAC-Haskell is a tutorial in the style of the Perl Cookbook. AE> Many thanks again - I'll make this my last post for at least a week or AE> two (to give others some bandwidth) - :-) -- Best regards, Bulat mailto:bulatz@HotPOP.com

Andy,
Many thanks again - I'll make this my last post for at least a week or two (to give others some bandwidth) -
Well, if you are having difficulties learning the language, you really shouldn't hold back on asking questions, of course. So, please, do ask those questions. I can recommend visiting the #haskell IRC channel at irc.freenode.net: there are always lots of capable people out there that are willing to help you set those first steps in Haskell. Good luck, Stefan

Stefan Holdermans wrote:
Andy,
Many thanks again - I'll make this my last post for at least a week or two (to give others some bandwidth) -
Well, if you are having difficulties learning the language, you really shouldn't hold back on asking questions, of course. So, please, do ask those questions. I can recommend visiting the #haskell IRC channel at irc.freenode.net: there are always lots of capable people out there that are willing to help you set those first steps in Haskell.
Good luck,
Stefan
Hi Stefan - thanks for this! I'm having a really good time getting into Haskell (and the resources and links that have been mentioned should be very useful too. :-) ( And yep - if I have problems I'll pop in a question or two to the list ;-) The other thing I'm "keeping tabs" on is the "ICFP" programming contest. I've known of that for quite a while (well before I started using Haskell), and even though the code of the winners is often pretty daunting, it's interesting as well - (and great to see the ingenious solutions that people come up with . ) Bye for now - - Andy
participants (3)
-
Andy Elvey
-
Bulat Ziganshin
-
Stefan Holdermans