# Why does pdfLaTeX fail when I try to use begin{lstlisting} inside a user-defined environment?

I'm having trouble with the listings package. I can't compile the following minimal example through pdfLaTeX:

\documentclass[a4paper, 11pt, twoside, openright, english]{memoir}

\usepackage{listings}

{%
\begin{lstlisting}%
}
{%
\end{lstlisting}%
}

\begin{document}
test
\end{document}


The compilation process just stops and appears to ask me for input:

* <The hell? I'll just try pressing ENTER then...>
(Please type a command or say \end')
*


But if I move the code in haskellCode directly into the document (i.e. use lstlisting directly) it compiles fine.

Any idea what's happening and how I can fix it?

-

The lstlisting environment is in many respects similar to the verbatim environment and TeX has to "see" explicitly the \end{lstlisting} to know it's finished.
The solution is to use \lstnewenvironment:
\lstnewenvironment{haskellCode}{}{}
`