Unescaping URLs in Python and Haskell
- Magnus Therning
A while ago I decided that in order to learn Haskell I should make an attempt to stop reaching for Python whenever I needed to solve a “small problem” and use Haskell instead. This morning I found a need to unescape URLs inside Vim and I didn’t catch myself until after I had written the following Python code:
#! /usr/bin/env python
import urllib
import sys
for l in sys.stdin:
print urllib.unquote(l),
After a little bit of cursing I started browsing Hoogle and after bit of searching I found the Network.URI
module. My Haskell solution ended up looking like this:
module Main where
import Network.URI
main :: IO ()
= interact $ unlines . (map unEscapeString) . lines main
Short, sweet, and easy to understand, I think.
In case you want to go the other direction, i.e. to escape strings in Haskell then combining escapeURIString
and isUnreserved
is the right thing to do.