
On Sun, 7 Aug 2011 18:55:37 +0200
Ertugrul Soeylemez
This is possible in Haskell using the mapM_ function, but it doesn't really solve your problem. What you need is a foldable data structure, which gives its entries names. As suggested earlier, Data.Map gives you such a structure. You can define a custom index type like this:
data AppDir = ConfigAppDir | DataAppDir | OtherAppDir deriving Ord
Then you can create a map from AppDir to a string:
import Data.Map (Map)
type AppDirs = Map AppDir FilePath
This is a foldable data structure. Instead of mapM_ from the Prelude you can now use mapM_ or forM_ from Data.Foldable. To access individual directories you can either use safe lookups or unsafe lookups:
import Data.Map (Map, (!), lookup)
lookup ConfigAppDir myDirs myDirs ! ConfigAppDir
This is the solution I like. I have to accept that here I cannot reach the conciseness of which might be due to Haskell being strongly typed.
The former is the safe variant giving you a Maybe String, while the latter is the unsafe variant, which throws an exception, if the directory in question is not present.
Is the latter one really unsafe? I'm not quite sure how to code something that the compiler accepts and crashes at runtime because mydirs :: Map AppDir FilePath and I would believe that the compiler would detect if the values after the ! is not from AppDir. -- Manfred