Posts Tagged ‘forecast’

Programming Praxis – Weather Forecast

November 5, 2010

In today’s Programming Praxis exercise our task is to print the weather forecast for a given city in the United States. Let’s get started, shall we?

The Scheme solution uses wget to download the file first. In Haskell, however, we can just use a library for this purpose.

import Network.HTTP

Thanks to the library, the actual function is trivial: download the file and print either the error message if it can’t be found or the weather forecast if it can.

showWeather :: String -> String -> IO ()
showWeather state city = either print (putStrLn . rspBody) =<<
    simpleHTTP (getRequest $ "http://weather.noaa.gov/pub/data/\
        \forecasts/city/" ++ state ++ "/" ++ city ++ ".txt")

A quick to see if everything works correctly:

main :: IO ()
main = showWeather "mo" "st_louis"

Not very warm at 47 degrees Fahrenheit, but at least it’s dry.