[Haskell-beginners] Get responsecode(Int) from simpleHTTP's Response

Michael Orlitzky michael at orlitzky.com
Wed Oct 17 13:52:47 CEST 2012


On 10/17/2012 02:51 AM, Jacques du Rand wrote:
> This is great works perfectly !
> I'm so new at haskell its scary !
> 
> One last question i dont *understand* one line ( just the right side )
> 
>        let (x,y,z) = rspCode response
> 
> 1) If i look at the documentation: Sorry for HTML. I see the
> constructors of Response and below it rspCode,rspReason,rspHeaders etc
> Are those parters or functions ?

They're functions that you can think of as the names of attributes of a
Response object (if you're coming from object-oriented programming).
There's a little magic going on under the hood, so you might want to
check out e.g.,

  http://learnyouahaskell.com/making-our-own-types-and-typeclasses

In particular the "Record Syntax" section. Papering over the details, in
an OO language, you might do something like,

  response.rspCode

to get the code out of a Response object. In Haskell, we just use a
function to do it. So,

  rspCode response

calls the rspCode function on 'response'. If you check the API docs,
  >
http://hackage.haskell.org/packages/archive/HTTP/4000.0.7/doc/html/Network-HTTP-Base.html#t:Response

you should see that rspCode takes a Response object and returns a
ResponseCode. But ResponseCode is just a synonym for (Int,Int,Int):

  type ResponseCode = (Int, Int, Int)

Therefore, rspCode takes a Response, and gives you back three Ints in an
ordered triplet.


> 2) I see the new version has a getResponseCode functions like
> getResponseBody with the signature:
> getResponseCode :: Result (Response a) -> IO ResponseCode
> getResponseCode (Left err) = fail (show err)
> getResponseCode (Right r)  = return (rspCode r)
> 
> What does this mean in the signature  *Result (Response a)* 

The Result type is really just a wrapper around Either. Either usually
takes two parameters, but Result fixes one of them to be ConnError:

  type Result a = Either ConnError a

So Result still takes one parameter. The parameter in this case is
(Response a).

For a more concrete example, think of a data type where you've got a box
and you can put stuff in it.

  data Box a = Box a

The 'a' parameter means that we can put different types of stuff in the
box. For example,

  foo :: Box Int
  foo = Box 3

  bar :: Box String
  bar = Box "Hello"

If this makes sense to you, then (Result (Response a)) is doing exactly
the same thing as (Box Int) or (Box String), only with slightly more
complicated types.



More information about the Beginners mailing list