[Haskell-cafe] lazily-loaded structures

Jeff Davis pgsql at j-davis.com
Fri Jan 1 17:17:09 EST 2010


I'm relatively new to haskell.

Let's say I have a simple program like this:

  import Data.Map

  type Tree = String
  type StringMap = Map String String

  f :: StringMap -> Tree -> Tree
  f _ y = y ++ "!"

  getStringMap :: IO StringMap
  getStringMap = return (Data.Map.fromList [("k1","v1")])

  main :: IO ()
  main = do
    m <- getStringMap
    s <- getLine
    putStr $ show (f m s) ++ "\n"

Ignore the implementation of f and getStringMap for now. Assume f is a
fairly complex function that uses the Map during the process of
transforming the Tree argument into the result Tree (here I defined Tree
as a string just so that it would compile).

getStringMap reads the whole Map into memory at once. However, now let's
say that the whole Map is too large, so I decide to store it over a
network in a remote database system instead. And then I define another
function like:

  type Connection = String
  getStringMapValue :: Connection -> String -> IO String

that takes a connection string and a key and returns the value.

My question is this: how do I change my program to make StringMap a
lazily-loaded structure based on getStringMapValue without changing f?

Regards,
	Jeff Davis



More information about the Haskell-Cafe mailing list