Send Beginners mailing list submissions to beginners@haskell.org To subscribe or unsubscribe via the World Wide Web, visit http://www.haskell.org/mailman/listinfo/beginners or, via email, send a message with subject or body 'help' to beginners-requ...@haskell.org
You can reach the person managing the list at beginners-ow...@haskell.org When replying, please edit your Subject line so it is more specific than "Re: Contents of Beginners digest..." Today's Topics: 1. Re: minimal Haskell concepts subset (David F. Place) 2. Re: code critique (Gary Klindt) 3. [Code Review] csv file to sqlite3 (Shakthi Kannan) 4. Re: [Code Review] csv file to sqlite3 (aditya siram) 5. Re: [Code Review] csv file to sqlite3 (Shakthi Kannan) ---------------------------------------------------------------------- Message: 1 Date: Wed, 27 Jul 2011 16:01:18 +0000 (UTC) From: David F. Place <d...@vidplace.com> Subject: Re: [Haskell-beginners] minimal Haskell concepts subset To: beginners@haskell.org Message-ID: <loom.20110727t175455-...@post.gmane.org> Content-Type: text/plain; charset=us-ascii Davi Santos <dps.abc <at> gmail.com> writes: > > If somebody knows another minihaskell, please let me know. > > Davi > For me, the most important ideas about Haskell are contained in the Untyped Lambda Calculus and the Simply-Typed Lambda Calculus. Yes, it's math, but a little goes a long way. http://en.wikipedia.org/wiki/Lambda_calculus ------------------------------ Message: 2 Date: Thu, 28 Jul 2011 00:59:50 +0200 From: Gary Klindt <gary.kli...@uni-konstanz.de> Subject: Re: [Haskell-beginners] code critique To: aditya siram <aditya.si...@gmail.com> Cc: beginners@haskell.org Message-ID: <4e309866.9020...@uni-konstanz.de> Content-Type: text/plain; charset=ISO-8859-1; format=flowed I repeated the profiling: print $ sumCheck 500 [1..1000] [1..1000] sumCheck1: 58,648 sumCheck2: 58,484 sumCheck3: 70,016 print $ sumCheck 5000 [1..10000] [1..10000] sumCheck1: 238,668 sumCheck2: 238,504 sumCheck3: 358,016 (unit: byte) This time I used the same compiler flags as Aditya (all but -rtsopts, I got:ghc: unrecognised flags: -rtsopts ). I'm using GHC version 6.12.1. Ok, in this numerical example, the scaling of sumCheck3 is not worse than the other's. In my first run I used quasi random numbers (fingers on my keyboard). Maybe you got puzzled with your '.prof' files? They seem identical (there always exists a function sum) On 07/27/2011 07:34 AM, aditya siram wrote: > I just profiled all three and have not found them to be different. > Maybe I'm reading my profiler output wrong so I've pasted it below. > The compilation command I used was: > ghc -fforce-recomp -XBangPatterns -prof -rtsopts -auto-all -o > ListCrossProductSum ListCrossProductSum.hs > > They were all invoked using: > main = print $ sumCheck 5000 [1..10000] [1..10000] > > I am running GHC 7.0.1 > > -deech > > sumcheck1: > ========== > Wed Jul 27 00:26 2011 Time and Allocation Profiling Report (Final) > > ListCrossProductSum +RTS -p -RTS > > total time = 0.00 secs (0 ticks @ 20 ms) > total alloc = 344,084 bytes (excludes profiling overheads) > > COST CENTRE MODULE %time %alloc > > sums Main 0.0 40.7 > main Main 0.0 58.3 > > > > individual inherited > COST CENTRE MODULE > no. entries %time %alloc %time %alloc > > MAIN MAIN > 1 0 0.0 0.0 0.0 100.0 > CAF Main > 240 2 0.0 0.2 0.0 99.2 > main Main > 246 1 0.0 58.3 0.0 99.0 > sumCheck Main > 247 1 0.0 0.0 0.0 40.7 > sums Main > 248 1 0.0 40.7 0.0 40.7 > CAF GHC.Show > 236 1 0.0 0.0 0.0 0.0 > CAF GHC.IO.Handle.FD > 176 2 0.0 0.4 0.0 0.4 > CAF System.Posix.Internals > 175 1 0.0 0.0 0.0 0.0 > CAF GHC.IO.Handle.Internals > 140 1 0.0 0.0 0.0 0.0 > CAF GHC.IO.Encoding.Iconv > 134 2 0.0 0.3 0.0 0.3 > CAF GHC.Conc.Signal > 131 1 0.0 0.1 0.0 0.1 > > sumcheck2 > ========= > [1 of 1] Compiling Main ( ListCrossProductSum.hs, > ListCrossProductSum.o ) > True > Wed Jul 27 00:27 2011 Time and Allocation Profiling Report (Final) > > ListCrossProductSum +RTS -p -RTS > > total time = 0.00 secs (0 ticks @ 20 ms) > total alloc = 344,084 bytes (excludes profiling overheads) > > COST CENTRE MODULE %time %alloc > > sums Main 0.0 40.7 > main Main 0.0 58.3 > > > > individual inherited > COST CENTRE MODULE > no. entries %time %alloc %time %alloc > > MAIN MAIN > 1 0 0.0 0.0 0.0 100.0 > CAF Main > 240 2 0.0 0.2 0.0 99.2 > main Main > 246 1 0.0 58.3 0.0 99.0 > sumCheck Main > 247 1 0.0 0.0 0.0 40.7 > sums Main > 248 1 0.0 40.7 0.0 40.7 > CAF GHC.Show > 236 1 0.0 0.0 0.0 0.0 > CAF GHC.IO.Handle.FD > 176 2 0.0 0.4 0.0 0.4 > CAF System.Posix.Internals > 175 1 0.0 0.0 0.0 0.0 > CAF GHC.IO.Handle.Internals > 140 1 0.0 0.0 0.0 0.0 > CAF GHC.IO.Encoding.Iconv > 134 2 0.0 0.3 0.0 0.3 > CAF GHC.Conc.Signal > 131 1 0.0 0.1 0.0 0.1 > > sumcheck3 > ========= > ListCrossProductSum +RTS -p -RTS > > total time = 0.00 secs (0 ticks @ 20 ms) > total alloc = 344,084 bytes (excludes profiling overheads) > > COST CENTRE MODULE %time %alloc > > sums Main 0.0 40.7 > main Main 0.0 58.3 > > > > individual inherited > COST CENTRE MODULE > no. entries %time %alloc %time %alloc > > MAIN MAIN > 1 0 0.0 0.0 0.0 100.0 > CAF Main > 240 2 0.0 0.2 0.0 99.2 > main Main > 246 1 0.0 58.3 0.0 99.0 > sumCheck Main > 247 1 0.0 0.0 0.0 40.7 > sums Main > 248 1 0.0 40.7 0.0 40.7 > CAF GHC.Show > 236 1 0.0 0.0 0.0 0.0 > CAF GHC.IO.Handle.FD > 176 2 0.0 0.4 0.0 0.4 > CAF System.Posix.Internals > 175 1 0.0 0.0 0.0 0.0 > CAF GHC.IO.Handle.Internals > 140 1 0.0 0.0 0.0 0.0 > CAF GHC.IO.Encoding.Iconv > 134 2 0.0 0.3 0.0 0.3 > CAF GHC.Conc.Signal > 131 1 0.0 0.1 0.0 0.1 ------------------------------ Message: 3 Date: Thu, 28 Jul 2011 07:42:11 +0530 From: Shakthi Kannan <shakthim...@gmail.com> Subject: [Haskell-beginners] [Code Review] csv file to sqlite3 To: beginners@haskell.org Message-ID: <CABG-yt1wb0P729Wh1H1M9Z1m_VKj1+Bdv+JXVvzL=2qknfb...@mail.gmail.com> Content-Type: text/plain; charset=UTF-8 Hi, I am trying to write a simple Haskell program to read from a .csv file and write to a .sqlite3 database. Can anyone please review the following code? === BEGIN === import Text.ParserCombinators.Parsec import Database.HDBC import Database.HDBC.Sqlite3 import Control.Monad(when) -- | Initialize DB and return database Connection connect :: FilePath -> IO Connection connect fp = do dbh <- connectSqlite3 fp prepDB dbh return dbh prepDB :: IConnection conn => conn -> IO () prepDB dbh = do tables <- getTables dbh when (not ("entries" `elem` tables)) $ do run dbh "CREATE TABLE entries ( \ \id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, \ \first TEXT, \ \second TEXT, \ \third TEXT)" [] return () commit dbh csvFile = endBy line eol line = sepBy cell (char ';') cell = quotedCell <|> many (noneOf ";\n\r") quotedCell = do char '"' content <- many quotedChar char '"' <?> "quote at end of cell" return content quotedChar = noneOf "\"" <|> try (string "\"\"" >> return '"') eol = try (string "\n\r") <|> try (string "\r\n") <|> string "\n" <|> string "\r" <?> "end of line" process :: [String] -> IO () process r = do conn <- connectSqlite3 "simple1.db" run conn "INSERT INTO entries (first, second, third) VALUES (?, ?, ?)" (map toSql r) commit conn disconnect conn main = do dbh <- connect "simple1.db" do c <- getContents case parse csvFile "(stdin)" c of Left e -> do putStrLn "Error parsing input:" print e Right r -> do mapM_ process r === END === Appreciate any suggestions, improvements in this regard. Thanks! SK -- Shakthi Kannan http://www.shakthimaan.com ------------------------------ Message: 4 Date: Wed, 27 Jul 2011 21:19:09 -0500 From: aditya siram <aditya.si...@gmail.com> Subject: Re: [Haskell-beginners] [Code Review] csv file to sqlite3 To: Shakthi Kannan <shakthim...@gmail.com> Cc: beginners@haskell.org Message-ID: <CAJrReyjOSBW6exjKuJesEpW+095_rOc=qo5yv_uremu9r3d...@mail.gmail.com> Content-Type: text/plain; charset=ISO-8859-1 Hi Shakthi, Are you aware that there is a csv parsing package on Hackage? If you're writing the parser as a exercise, Real World Haskell [1] has a chapter on Parsec that covers csv parsing. -deech [1] http://book.realworldhaskell.org/read/using-parsec.html On Wed, Jul 27, 2011 at 9:12 PM, Shakthi Kannan <shakthim...@gmail.com> wrote: > Hi, > > I am trying to write a simple Haskell program to read from a .csv file > and write to a .sqlite3 database. Can anyone please review the > following code? > > === BEGIN === > > import Text.ParserCombinators.Parsec > > import Database.HDBC > import Database.HDBC.Sqlite3 > import Control.Monad(when) > > -- | Initialize DB and return database Connection > connect :: FilePath -> IO Connection > connect fp = > ? ?do dbh <- connectSqlite3 fp > ? ? ? prepDB dbh > ? ? ? return dbh > > prepDB :: IConnection conn => conn -> IO () > prepDB dbh = > ? ? ? do tables <- getTables dbh > ? ? ? ? ?when (not ("entries" `elem` tables)) $ > ? ? ? ? ? ? ? do run dbh "CREATE TABLE entries ( \ > ? ? ? ? ? ? ? ? ?\id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, \ > ? ? ? ? ? ? ? ? ?\first TEXT, \ > ? ? ? ? ? ? ? ? ?\second TEXT, \ > ? ? ? ? ? ? ? ? ?\third TEXT)" [] > ? ? ? ? ? ? ? ? ?return () > ? ? ? ? ?commit dbh > > csvFile = endBy line eol > line = sepBy cell (char ';') > cell = quotedCell <|> many (noneOf ";\n\r") > > quotedCell = > ? ?do char '"' > ? ? ? content <- many quotedChar > ? ? ? char '"' <?> "quote at end of cell" > ? ? ? return content > > quotedChar = > ? ? ? ? ? noneOf "\"" > ? ?<|> try (string "\"\"" >> return '"') > > eol = ? try (string "\n\r") > ? ?<|> try (string "\r\n") > ? ?<|> string "\n" > ? ?<|> string "\r" > ? ?<?> "end of line" > > > process :: [String] -> IO () > process r = do > ? ? ? ?conn <- connectSqlite3 "simple1.db" > ? ? ? ?run conn "INSERT INTO entries (first, second, third) VALUES > (?, ?, ?)" (map toSql r) > ? ? ? ?commit conn > ? ? ? ?disconnect conn > > main = do > ? ? dbh <- connect "simple1.db" > ? ? do c <- getContents > ? ? ? ?case parse csvFile "(stdin)" c of > ? ? ? ? ? ? Left e -> do putStrLn "Error parsing input:" > ? ? ? ? ? ? ? ? ? ? ? ? ?print e > ? ? ? ? ? ? Right r -> do > ? ? ? ? ? ? ? ? ? mapM_ process r > > === END === > > Appreciate any suggestions, improvements in this regard. > > Thanks! > > SK > > -- > Shakthi Kannan > http://www.shakthimaan.com > > _______________________________________________ > Beginners mailing list > Beginners@haskell.org > http://www.haskell.org/mailman/listinfo/beginners > ------------------------------ Message: 5 Date: Thu, 28 Jul 2011 07:57:04 +0530 From: Shakthi Kannan <shakthim...@gmail.com> Subject: Re: [Haskell-beginners] [Code Review] csv file to sqlite3 To: aditya siram <aditya.si...@gmail.com> Cc: beginners@haskell.org Message-ID: <CABG-yt09YeZqcco4DQsbv=LjjezSyQ4vAOjwE=e+k5mqs9s...@mail.gmail.com> Content-Type: text/plain; charset=UTF-8 Hi, --- On Thu, Jul 28, 2011 at 7:49 AM, aditya siram <aditya.si...@gmail.com> wrote: | If | you're writing the parser as a exercise, Real World Haskell [1] has a | chapter on Parsec that covers csv parsing. | -deech | | [1] http://book.realworldhaskell.org/read/using-parsec.html \-- I have used import Text.ParserCombinators.Parsec from RWH in the code. SK -- Shakthi Kannan http://www.shakthimaan.com ------------------------------ _______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners End of Beginners Digest, Vol 37, Issue 62 *****************************************