[Haskell-cafe] Takusen and strictness

Paul Moore p.f.moore at gmail.com
Fri Mar 2 06:55:37 EST 2007


I'm still fiddling with simple database queries using Takusen. One
question I have is regarding strictness. I've just been reading
"Haskell IO for Imperative Programmers" and loved the idea that
laziness (rather than monads) is what makes IO in Haskell so simple.
What I'm not sure about, is whether Takusen offers a way of lazily
getting data from a database (and a meta-question - how do I work this
out for mysefl?!)

With the code

{-# OPTIONS -fglasgow-exts #-}

import Database.Oracle.Enumerator
import Database.Enumerator

runSql :: String -> IO [String]
runSql s = withSession (connect "USER" "PASSWD" "DB") ( do
       let iter (s::String) accum = result (s : accum)
       r <- doQuery (sql s) iter []
       return r
    )

main :: IO ()
main = do
   r <- runSql "select username from all_users"
   putStrLn (unlines r)

I can do a simple database query, process the results, and produce
output. The sreucture of main is much like an interact loop, but with
the input from a database rather than stdin.

But, will this read the database lazily, or will it get all the rows
into memory at once? How will using result' instead of result (in
runSql) affect this? And as I said above, how can I learn to work this
out for myself? I know I could just ignore the issue until performance
becomes a problem and then profile - but that ignores the question of
whether I've got the idiom right.

Thanks for any help,
Paul.


More information about the Haskell-Cafe mailing list