[Haskell-beginners] feedback on simplifying my Takusen code

Neil Jensen neilcjensen at gmail.com
Wed Mar 2 16:27:33 CET 2011

Hello, beginner here, looking for some ideas on how to simplify how I use
the Takusen database library to query a database and return types.

In the working code below, I'm able to query the database and
through several functions get a list of simple types. I've tried
playing around with the query2Iteratee and getAccounts' functions to
simplify the code and reduce it to a single function, but keep getting hung
up on compiler errors due to type issues.

Is there a better way to write this, or is it best to keep the
various functions quite separate? (in my real project, I have the
database access in a separate module from the list of custom types and the
code to convert to the Account type).

Thanks in advance,

import Database.Sqlite.Enumerator
import Database.Enumerator

data Account = Account {account_id :: Int}
            deriving (Eq, Show)

query2Iteratee :: (Monad m) => Int -> [Int] -> m (IterResult [Int])
--query2Iteratee :: (Monad m) => Int -> IterAct m [Int]
query2Iteratee a accum = result' (a:accum)

getAccounts' :: IO [Int]
getAccounts' = do
           let dbh = connect
           withSession dbh (do
                  r <- doQuery (sql "SELECT account_id FROM
ACCOUNT;") query2Iteratee []
                  return r

getAccounts :: IO [Account]
getAccounts  = do
              accountsList <- getAccounts'
              let r = map conv accountsList
              return r

conv :: Int -> Account
conv a = Account {account_id=a}

main = do
    accounts <- getAccounts'
    putStrLn (show accounts)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110302/399ca4c4/attachment.htm>

More information about the Beginners mailing list