### 2007-08-30T10:31:04+00:00

Often when creating text from a program, you encounter this situation: You have a value, e.g. `first_name`, that might be empty (depending on your programming language, this means `undefined`, `NILL`, `None`, `nil` or `Nothing`) or contain a string. In the latter case, you want to surround it by some markup, in the former you want to do nothing. Here is how I’d code that in `Template::Toolkit`:

`<table>[% IF second_name %] <tr><th>Second name</th><td><b>[% second_name %] </b></td></tr> [% END %][% IF first_name %] <tr><th>First name</th><td>[% first_name %]</td></tr> [% END %]</table>`

And here in python:

`output = "<table>"if second_name:    output += "<tr><th>Second name</th><td><b>%s</b></td></tr>" % second_nameif first_name:    output += "<tr><th>First name</th><td>%s</td></tr>" % first_nameoutput += "</table>"`

I really don’t like entering each variable name twice, when I basically want to do one thing with it.

Here is how you can use Haskell’s flexible syntax to help you out. First we define a unconditional “insert here” operator `++!++` and then a conditional one, named `++?++`:

`(++!++) :: String -> String -> String -> Stringpre ++!++ post = \ins -> pre ++ ins ++ post-- Or more haskell-style:-- pre ++!++ post = (pre ++).(++ post)(++?++) :: String -> String -> Maybe String -> Stringpre ++?++ post = \ins -> maybe "" (pre ++!++ post) ins-- Or more haskell-style:-- pre ++?++ post = maybe "" (pre ++!++ post)`

Note that the first line of each function, the type declaration, could be left out as Haskell can figure it out by itself. Also note that these function are not restricted to strings (as lists of characters), but can be used for any kind of list.

Now I can write the code up there as:

```output = "<table>" ++
("<tr><th>Second name</th><td><b>" ++?++ "</b></td></tr>")  second_name ++
("<tr><th>First name</th><td>" ++?++ "</td></tr>") first_name ++
"</table>"```

I think that’s a nice display of the flexibility of Haskell’s syntax, in this case, using symbols for function names and infix application.