[Tex/LaTex] How to write pseudocode similar to code presented in “Beautiful Code” by J. R. Heard

pseudocode

I came across an astonishing piece of text, which is called "Beautiful Code, Compelling Evidence" by J. R. Heard. I was wondering if there is a package or something that allows me to present my code similar to how J. R. Heard presented his code.

I would like line numbering though, but I figured first I need to find what he used to create a formatting like this. Note: the code is Haskell, but I would like to use it for pseudocode.

PS: it would be ideal if line numbering can be done after the vertical line. But I was also wondering whether it is possible to literally fix the width of the left column.

An example from his amazing work is shown below:
Example from Beautiful Code by J. R. Heard

Thanks for your time and effort to help me!

Best Answer

I didn't use the Haskell highlighting offered by listings, because it highlights too many words (like List, etc.) which are not highlighted in the example.

\documentclass[12pt,DIV13]{scrartcl}
\usepackage{fontspec,listings,xcolor,lipsum}
% Fonts
\defaultfontfeatures{Ligatures=TeX}
\setmainfont{Gill Sans Std}
\setmonofont{Lucida Sans Typewriter OT}
% Colors
\definecolor{darkgray}{HTML}{404040}
\definecolor{rulegray}{HTML}{DADADA}
\definecolor{keywordblue}{HTML}{1F497C}
% Listings
\lstdefinestyle{code}{
  basicstyle=\ttfamily,
  keywordstyle=\color{keywordblue},
  keywords={import,qualified,as,do,where}
}
\newcommand\clearlines[1]{%
  \if#10\else%
    \leavevmode\\%
    \expandafter\clearlines\expandafter{\the\numexpr#1-1}%
  \fi}
\begin{document}

\lipsum[1]
\begin{center}
  \footnotesize
  \begin{minipage}{.25\linewidth}
    \color{darkgray}\raggedleft\itshape
    imports, aliases (1-3) \clearlines{5}
    Split all lines in the file. (6-7) \clearlines{3}
    Insert them into the map (9)
  \end{minipage}
  \hfill{\color{rulegray}\vrule}\hfill
  \begin{minipage}{.73\linewidth}
    \begin{lstlisting}[style=code]
import Data.List (foldl')
import qualified Data.ByteString.Lazy.Char8 as BStr
import qualified Data.Map as Map

readDatafile name = do
    sheet <- (map (BStr.split '\t') . BStr.lines) `fmap`
              BStr.readFile name
    return $ foldl’ go Map.empty sheet
  where go m (x:xs) = Map.insert (BStr.unpack x) xs m
    \end{lstlisting}
  \end{minipage}
\end{center}
\lipsum[1]

\end{document}

enter image description here


Adapting Martin Scharrer's answer to a similar question one can obtain a simpler version:

\documentclass[12pt,DIV13]{scrartcl}
\usepackage{fontspec,listings,xcolor,lipsum}
% Fonts
\defaultfontfeatures{Ligatures=TeX}
\setmainfont{Gill Sans Std}
\setmonofont{Lucida Console}
% Colors
\definecolor{darkgray}{HTML}{404040}
\definecolor{rulegray}{HTML}{DADADA}
\definecolor{keywordblue}{HTML}{1F497C}
% Listings
\lstnewenvironment{beautifulcode}{
  \lstset{
    basicstyle=\footnotesize\ttfamily,
    keywordstyle=\color{keywordblue},
    keywords={import,qualified,as,do,where},
    frame=l,rulecolor=\color{rulegray},framesep=.5em,
    escapeinside={(*@}{@*)},
    escapebegin={\begin{lrbox}{0}\normalfont\itshape\footnotesize\color{darkgray}},
    escapeend={\end{lrbox}\llap{\box0\hspace{1em}}}
  }
  \hspace*{.25\linewidth}
  \minipage{.75\linewidth}
}{
  \endminipage
}
\begin{document}

\lipsum[1]

\begin{beautifulcode}
(*@imports, aliases (1-3)@*)import Data.List (foldl')
import qualified Data.ByteString.Lazy.Char8 as BStr
import qualified Data.Map as Map

readDatafile name = do
(*@Split all lines in the file. (6-7)@*)    sheet <- (map (BStr.split '\t') . BStr.lines) `fmap`
              BStr.readFile name
    return $ foldl’ go Map.empty sheet
(*@Insert them into the map (9)@*)  where go m (x:xs) = Map.insert (BStr.unpack x) xs m
\end{beautifulcode}

\lipsum[1]

\end{document}