[Haskell-cafe] Newbie vs. laziness

Alex Queiroz asandroq at gmail.com
Tue Mar 20 09:34:08 EDT 2007


Hallo list,

     I don't quite get how ($!) works. I have this function:

dbCreateIndices con dtn dof = do
   let dummy = newDbItem
   let query = "SELECT id FROM " ++ (dtn dummy) ++ " ORDER BY " ++ (dof dummy)
   ids <- liftM (map fromSql . concat ) $! quickQuery con query []
   return $ IntMap.fromList $ zip ids [0..]

quickQuery returns a lazy list of results, and I expected ($!) to make
it strict. But my program crashes if I use it this way. But, if I add
a print to the function:

dbCreateIndices con dtn dof = do
   let dummy = newDbItem
   let query = "SELECT id FROM " ++ (dtn dummy) ++ " ORDER BY " ++ (dof dummy)
   ids <- liftM (map fromSql . concat ) $! quickQuery con query []
   print ids
   return $ IntMap.fromList $ zip ids [0..]

print makes the ids list strict and all is well. How is the correct
way to use ($!) ?

Cheers,
-- 
-alex
http://www.ventonegro.org/


More information about the Haskell-Cafe mailing list