Tem*_*ora 4 interpreter haskell hint configuration-files
如何使用Language.Haskell.Interpreter读取给定的配置文件并分配其中给出的值以初始化程序中的变量?
我的配置文件如下:
numRecords = 10
numFields = 3
inputFile = /home/user1/project/indata.file
outputFile = /home/user1/project/outdata.file
datefmt = ddmmyyyy
Run Code Online (Sandbox Code Playgroud)
我想使用配置文件中给出的值初始化与配置文件中给出的标识符相对应的变量.
我如何使用Language.Haskell.Interpreter来完成这件事?我很困惑因为IO Monad和Interpreter Monad.这种小例子也很有用.
为什么不?
data Config = Config {size :: Int, path :: String} deriving (Read, Show)
readConfig :: String -> Config
readConfig = read
main = do
config <- readFile "my.config" >>= return . readConfig
putStrLn $ "Configured size := " ++ show (size config)
putStrLn $ "Configured path := " ++ show (path config)
Run Code Online (Sandbox Code Playgroud)
使用my.config文件
Config {
size = 1024,
path = "/root/passwords.db"
}
Run Code Online (Sandbox Code Playgroud)
并测试使用 ghci
*Main> main
Configured size := 1024
Configured path := "/root/passwords.db"
*Main>
Run Code Online (Sandbox Code Playgroud)
(抱歉以前的错误,我很快)