15

Does Literate Haskell support indexing function names, typeclasses and variable references? Is there a filter I can run on Literate Haskell source that will do this and give me either a nice PDF manual or a hyperlinked HTML document.

These are a really nice features of noweb and CWEB which I think it would spur widespread adoption of Literate Haskell.

As an example, look at the word count program written in CWEB. The code chunk on the first page in item #4 is footnoted with where that code is used. LHS doesn't support chunks but I'd like to know where the code is being used:

  1. Comment describing func.

    func = id

    Used in: (X.Y.Z.f, A.B.C.g, Section 1.5)

    func2 = indefined

    Used in: (A.B.C.x, Section 2.1)

And additionally an index that aggregates all the function names and variables along where they're referenced in the document and by other functions etc.

4

1 回答 1

1

在 Latex 中有一些可能性,下面使用包列表,与 makeindex 一起创建所有函数的列表。此外,\label 用于在不同部分之间创建交叉引用:

\documentclass[a4paper,11pt,reqno,twoside,pdflatex,makeidx]{amsart}

\usepackage[a4paper]{geometry}

\usepackage{listings}
\lstloadlanguages{Haskell}

\lstset{
    flexiblecolumns=false,
    basewidth={0.5em,0.45em},
    basicstyle=\ttfamily,
    language=haskell,
    % numbers=left, % optional numbering of code lines
    firstnumber=last,
    numberstyle=\tiny,
    stepnumber=2,  
    numbersep=5pt,
    index={fac,fac2}
}

\lstnewenvironment{code}{}{}

\usepackage{hyperref}

\title{The factorial function}
\author{Federico Squartini}
\date{}


\makeindex

\begin{document}
\maketitle
\section{Factorial function}
\label{code:fac1}
The factorial function can be defined as:

\begin{code}

fac 0 = 1
fac n = n * fac (n-1)

\end{code}
  
\section{Factorial function in constant space}
The code for the factorial defined section~\ref{code:fac1} uses $o(n)$ stack
space. The following function uses constant space:

\begin{code}

fac2 n = go 1 1
    where
      go !acc i| i <= n = go (acc*i) (i+1)
               | otherwise = acc

\end{code}

\printindex
\end{document}

编译:

pdflatex example.tex

生成索引示例.idx

pdflatex example.tex

pdflatex example.tex

生成的 pdf 文件在这里。这非常适合制作 pdf 文件。对于其他类型的输出(例如 html),您应该将 latex 与pandoc一起使用。

另一种选择是使用 pandoc 的 markdown 语法,与 ad hoc latex 命令(\label 和 makeindex)混合。这应该简化任务并在源文件中产生更少的句法噪音。

于 2011-07-19T10:01:20.067 回答