[Haskell-beginners] module "static" data
Dennis Raddle
dennis.raddle at gmail.com
Tue Jan 10 20:17:22 CET 2012
I should clarify. I have many modules A, B, C, D, .. which need to load
configuration from disk--each module its own data type AData, BData,
CData... . One solution is to load the data once at the highest common
level (call it Common) and pass it to each module.
But the way Common calls the modules is like this (this is my original code
which ignores the need for configuration:
fnLookupTable :: [(String, String -> Int)]
fnLookupTable = [ ("a", moduleAFn)
,("b", moduleBFn) ]
commonFunction :: [String] -> [Int]
commonFunction ss = do
let lookupFn functionName = fromJust $ lookup functionName fnLookupTable
result = map (\s -> ((lookupFn s) "some input")) ss
return result
So if I were to pass some configuration to each module I would need to
change the type signature of fnLookupTable:
fnLookupTable :: [(String, ConfigurationData -> String -> Int)]
data ConfigurationData = TypeACons AData
| TypeBCons BData
| TypeCCons CData
What's annoying about this is the need to make this extra type
ConfigurationData and what's worse, I can't hide AData, BData, CData in the
modules A, B, C. This commonFunction has to "know" about them. If I modify
modules or add new modules there's an extra step, modifying
ConfigurationData.
On Tue, Jan 10, 2012 at 10:45 AM, Dennis Raddle <dennis.raddle at gmail.com>wrote:
> I would like to know how to do something in Haskell. I have a module which
> needs to load some data from disk in order to run, and I'd like to load it
> at most once. In C, I would use a "static" variable in the module to store
> the data. How can I do this in Haskell?
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20120110/c3cd3d47/attachment.htm>
More information about the Beginners
mailing list