[Haskell-beginners] Type error in sub-function

Brandon Allbery allbery.b at gmail.com
Wed Jul 23 14:22:57 UTC 2014


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.haskell.org/pipermail/beginners/attachments/20140723/e4238630/attachment-0001.html>


More information about the Beginners mailing list