Question: I wanted to write a Haskell function that takes a string, and replaces any space characters with the special code %20. For example:

```
sanitize "http://cs.edu/my homepage/I love spaces.html"
-- "http://cs.edu/my%20homepage/I%20love%20spaces.html"

```

I am thinking to use the concat function, so I can concatenates a list of lists into a plain list.

Answer: 
```
changer :: [Char] -> [Char] -> [Char]
changer [] res = res
changer (x:xs) res = changer xs (res ++ (if x == ' ' then "%20" else [x]))

sanitize :: [Char] -> [Char]
sanitize xs = changer xs ""

main = print $ sanitize "http://cs.edu/my homepage/I love spaces.html"
-- "http://cs.edu/my%20homepage/I%20love%20spaces.html"

```

The purpose of `sanitize` function is to just invoke `changer`, which does the actual work. Now, `changer` recursively calls itself, till the current string is exhausted. 

```
changer xs (res ++ (if x == ' ' then "%20" else [x]))

```

It takes the first character `x` and checks if it is equal to `" "`, if so gives `%20`, otherwise the actual character itself as a string, which we then concatenate with the accumulated string.

**Note:** This is may not be the optimal solution.
Another expression of Shanth's pattern-matching approach:

```
sanitize = foldr go []
  where
    go ' ' r = '%':'2':'0':r
    go c   r = c:r

```