concept url in category haskell

appears as: URL, URLs
Get Programming with Haskell

This is an excerpt from Manning's book Get Programming with Haskell.

Suppose you want to create a website that compares prices of various items on other sites such as Amazon and eBay. You already have a function that returns the URL of the item you need, but you need to write code for each site that determines how to extract the price from the page. One solution is to make a custom function for each site:

Although this is useful, what happens if you need to extract items from 1,000 URLs, but all using amazonExtractor? Is there a way to capture this argument on the fly so you have to pass in only the url parameter for future calls?

5.2. Example: Generating URLs for an API

One of the most common ways to get data is to make calls to a RESTful API by using an HTTP request. The simplest type of request is a GET request, in which all of the parameters you need to send to another server are encoded in the URL. In this example, the data you need for each request is as follows:

Figure 5.3. Parts of a URL

Building a URL from these parts is straightforward. Here’s your basic getRequestURL builder.

Listing 5.2. getRequestUrl
getRequestURL host apiKey resource id = host ++ "/" ++ resource ++ "/" ++ id ++ "?token=" ++ apiKey

One thing that might strike you as odd about this function is that the order of your arguments isn’t the same as the order you use them or that they appear in the URL itself. Anytime you might want to use a closure (which in Haskell is pretty much anytime), you want to order your arguments from most to least general. In this case, each host can have multiple API keys, each API key is going to use different resources, and each resource is going to have many IDs associated with it. The same is true when you define ifEven; the function you pass will work with a huge range of inputs, so it’s more general and should appear first in the argument list.