[Haskell-beginners] Type error in sub-function

양철웅 cwyang at aranetworks.com
Wed Jul 23 14:18:11 UTC 2014


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





2014-07-23 21:33 GMT+09:00 Kim-Ee Yeoh <ky3 at atamo.com>:

>
> On Wed, Jul 23, 2014 at 7:08 PM, 양철웅 <cwyang at aranetworks.com> wrote:
>
>> Since the return type of foo Func is IO String and
>> first case statement has "return timedVal",
>> I think that ghc expects the type of timedVal as String.
>> However, the error message shows that
>> ghc expects timedVal should have type IO b0.
>>
>
> It's not really about timedVal nor the case 'statement'. (The scare quotes
> are because there are only expressions, not statements, in haskell.)
>
> Consider the difference between
>
> do { putStrLn "hello"; True; }
>
> and
>
> do { putStrLn "hello"; return True; }
>
> Which one throws an error and why?
>
>
> -- Kim-Ee
>
> _______________________________________________
> 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/2d02001b/attachment.html>


More information about the Beginners mailing list