File: Sort.lhs

package info (click to toggle)
lhs2tex 1.9-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,544 kB
  • ctags: 28
  • sloc: haskell: 3,364; sh: 2,773; makefile: 349
file content (73 lines) | stat: -rw-r--r-- 2,108 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
%if False

This is an example file that demonstrates the use of lhs2TeX.
Compile it with

lhs2TeX --math Sort.lhs > Sort.tex

and then run it through LaTeX.

The "if False" in the first line is an lhs2TeX directive. All
directives start with a % character. You can use the "if" directive to
conditionally include or exclude certain parts of your document. An
"if False" statement can be used to start a comment that will not
appear in the TeX output.

To make this a valid TeX document, we will start with a \documentclass
command, after closing this comment block by means of an "endif"
directive.

%endif

\documentclass{article}

%if False
We will now include the two files that are required for
using lhs2TeX, using the "include" directive.
To activate alignment of the code blocks, we set the
alignment column to 33, using the "align" directive.
%endif

%include lhs2TeX.fmt
%include lhs2TeX.sty
%align 33

%if False

Another use for conditionals is to exclude parts of the code
that are needed to make the module correct Haskell, but should
not appear in the typeset version. We place the Haskell module
header here.

> module Sort where

Now we start the TeX document with \begin{document}
and then include the module body that will be typeset:

%endif

\begin{document}

> quickSort                     :: (Ord a) => [a] -> [a]
> quickSort []                  =  []
> quickSort (a : as)            =  quickSort [ b | b <- as, b <= a ]
>                               ++ a : quickSort [ b | b <- as, b > a ]
>
> mergeSort                     :: (Ord a) => [a] -> [a]
> mergeSort []                  =  []
> mergeSort [a]                 =  [a]
> mergeSort as                  =  merge (mergeSort bs) (mergeSort cs)
>     where (bs, cs)            =  splitAt (length as `div` 2) as

The worst case execution time of |mergeSort| is $\Theta(n\log n)$.
NB. |splitAt| is given by

< splitAt                       :: Int -> [a] -> ([a], [a])
< splitAt k as                  =  (take k as, drop k as) {-"\enskip."-}

%if False
Finally, we have to end the LaTeX document and are done.
%endif

\end{document}