Skip to content

Instantly share code, notes, and snippets.

@astanin
Created October 25, 2010 10:17
Show Gist options
  • Select an option

  • Save astanin/644724 to your computer and use it in GitHub Desktop.

Select an option

Save astanin/644724 to your computer and use it in GitHub Desktop.
An example of literate programming (LP) with Octave and Noweb
\section{Programming in Octave}
Just like any other programming language, Octave has its loop and conditional
constructs. The following example demonstrates how to generate the first 10
values of Fibonacci's sequence using a for loop:
<<Fibonacci sequence>>=
fib = [ 0, 1 ];
for i = 3:10
fib = [ fib, fib( i-2 ) + fib( i-1 ) ];
endfor
fib
@
\section{Source code}
This document was produced with \verb!noweb! from the single \LaTeX/Octave
source \verb!octavelp.nw!. See \verb!http://gist.github.com/644724!.
We can process this source with \verb!noweave! to produce the documentation:
\begin{quote}\begin{verbatim}
$ noweave octavelp.nw > octavelp.tex
$ pdflatex octavelp.tex
\end{verbatim}\end{quote}
To extract the source code, we use \verb!notangle!:
\begin{quote}\begin{verbatim}
$ notangle -RFibonacci\ sequence octavelp.nw > fibs.m
$ octave -q fibs.m
fib =
0 1 1 2 3 5 8 13 21 34
\end{verbatim}\end{quote}
@astanin

astanin commented Oct 25, 2010

Copy link
Copy Markdown
Author

After running notangle -RFibonacci\ sequence octavelp.nw > fibs.m, the contents of fibs.m is:

fib = [ 0, 1 ];
for i = 3:10
    fib = [ fib, fib( i-2 ) + fib( i-1 ) ];
endfor
fib

Formatted documentation looks like this: octavelp.pdf.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment