[Haskell-cafe] Newbie vs. laziness

Udo Stenzel u.stenzel at web.de
Wed Mar 21 16:30:16 EDT 2007


Alex Queiroz wrote:
> 
>     I don't quite get how ($!) works. I have this function:
> 
>   ids <- liftM (map fromSql . concat ) $! quickQuery con query []

There's a difference between an IO action and the result of said action,
and similarly there's a difference between making sure an action is
evaluated and making sure the result of executing the action is
evalulated.  You did the former.

If you really wanted to evaluate the result to WHNF (only) before
finishing dbCreateIndices, this would work:

>   ids <- liftM (map fromSql . concat ) $ quickQuery con query []
>   ids `seq` return $ IntMap.fromList $ zip ids [0..]

But you probably need to evaluate the complete list, so you need more:

>   ids <- liftM (map fromSql . concat ) $ quickQuery con query []
>   foldr seq () ids `seq` return $ IntMap.fromList $ zip ids [0..]

Of course, if you insist on using ($!), that's also possible:

>   ids <- liftM (map fromSql . concat ) $ quickQuery con query []
>   return $ IntMap.fromList $ flip zip [0..] $! foldr seq ids ids


HTH.

-Udo.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org/pipermail/haskell-cafe/attachments/20070321/c1cb0365/attachment-0001.bin


More information about the Haskell-Cafe mailing list