[Haskell-cafe] FP design

Tim Docker timd at macquarie.com.au
Wed Nov 7 18:36:22 EST 2007


levi.stephen wrote:
> I have similar questions about Haskell abstracting away
> implementations behind interfaces as well. I have become
> used to an approach where I will not worry about
> databases/persistence when beginning. I will create an
> interface to a database layer (e.g., save(object), retrieve(id),
> findByName(name)) etc., and an implementation that uses in
> memory collections to begin with. Later I will replace this with
> database calls.

> How does this type of approach work in Haskell?
> or what is the Haskell way to achieve this?


If OO is a good approach for a problem, it's straightforward to model
it in haskell. If you plan to access an external DB in any case, then
the interface will involve the IO Monad. Something along the lines
of:

data Object
data ID

data ObjectStore = ObjectStore {
    save :: Object -> IO ID,
    retrieve :: IO -> IO (Maybe Object),
    retrieveByName :: String -> IO (Maybe Object)
}

createMemoryStore :: IO ObjectStore
connnectExternalStore :: ConnectionParams -> IO ObjectStore

Tim 


More information about the Haskell-Cafe mailing list