Thank you all for kind replies.
But I'm not sure whether I grasp the gist.
Actual code is as follows.
I want to scan the query argument of HTTP get method and
build response object according to the query arguments.
I use fold and the scanQuery function should return IO object
for some reason.
Since parseHeader function is fold function and uses query and request
variable at the same time, I cannot write as a separate function.
scanQuery :: Request -> IO Object
scanQuery request = do
let (p, query) = parseUrl (path request)
foldM parseHeader defObject query
where parseHeader obj (name, Just value) =
case name of
"len" -> return obj { contentLength = read value }
"type" -> return obj { contentType = parseContentType value }
"rate" -> return obj { rate = Just value }
"status" -> return obj { httpStatus = read value }
('x':'x':name2) -> return obj { clientReqHdr = ((name2, vv) : prev) }
where prev = clientReqHdr obj
vv = case value of
"client_ip_addr" -> clientIp request
"now" -> do utcTime <- currentUTCTime
return $ formatRFC1123 utcTime
_ -> value
_ -> return obj
However, above code does not compile also :-(
*Main> :l test
[1 of 1] Compiling Main ( test.hs, interpreted )
test.hs:101:64:
Couldn't match expected type `[t0]' with actual type `IO UTCTime'
In a stmt of a 'do' block: utcTime <- currentUTCTime
In the expression:
do { utcTime <- currentUTCTime;
return $ formatRFC1123 utcTime }
In a case alternative:
"now"
-> do { utcTime <- currentUTCTime;
return $ formatRFC1123 utcTime }
Failed, modules loaded: none.
Obviously outer do-block is inside IO monad, as the type of scanQuery is
Request -> IO Object. But GHC puts inner do-block (in "now" case) inside list monad,
doesn't it? Why does ghc look for List monad?
Lost in monad,
Chul-Woong