[Haskell-beginners] Type error in sub-function
양철웅
cwyang at aranetworks.com
Wed Jul 23 14:45:04 UTC 2014
Thank you, Brandon.
I changed the code like belows and it works.
('x':'x':name2) -> do vv <- case value of
"client_ip_addr" ->
return $ clientIp request
"now" -> do utcTime
<- currentUTCTime
return $
formatRFC1123 utcTime
_ -> return value
let prev = clientReqHdr obj
return obj { clientReqHdr =
((name2, vv) : prev) }
2014-07-23 23:22 GMT+09:00 Brandon Allbery <allbery.b at gmail.com>:
>
> On Wed, Jul 23, 2014 at 10:18 AM, 양철웅 <cwyang at aranetworks.com> wrote:
>
>> ('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?
>>
>
> Because you're treating vv as a pure value when you use it, so ghc looks
> for a way to treat it as a monad and concludes that it is a List. If you
> want it to be in IO, you need to use <- on its result, not use it directly.
>
> --
> brandon s allbery kf8nh sine nomine
> associates
> allbery.b at gmail.com
> ballbery at sinenomine.net
> unix, openafs, kerberos, infrastructure, xmonad
> http://sinenomine.net
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20140723/9d0c1a89/attachment.html>
More information about the Beginners
mailing list