# Example code

### From HaskellWiki

DonStewart (Talk | contribs) (typo) |
DonStewart (Talk | contribs) (link to docs) |
||

Line 11: | Line 11: | ||

highlighting by [http://www.cs.york.ac.uk/fp/darcs/hscolour/ hscolour]): | highlighting by [http://www.cs.york.ac.uk/fp/darcs/hscolour/ hscolour]): | ||

− | * [http://www.cse.unsw.edu.au/~dons/data/Prelude.html A Haskell 98 Prelude.hs], foundational Haskell library ([http://www.cse.unsw.edu.au/~dons/data/Prelude.hs raw]) | + | * [http://www.cse.unsw.edu.au/~dons/data/Prelude.html A Haskell 98 Prelude.hs], foundational Haskell library ([http://www.cse.unsw.edu.au/~dons/data/Prelude.hs raw], [http://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html docs]) |

− | * [http://www.cse.unsw.edu.au/~dons/data/List.html List.hs], the standard list library ([http://www.cse.unsw.edu.au/~dons/data/List.hs raw]) | + | * [http://www.cse.unsw.edu.au/~dons/data/List.html List.hs], the standard list library ([http://www.cse.unsw.edu.au/~dons/data/List.hs raw], [http://haskell.org/ghc/docs/latest/html/libraries/base/Data-List.html docs]) |

− | * [http://www.cse.unsw.edu.au/~dons/data/Maybe.html Maybe.hs], the <code>Maybe</code> type ([http://www.cse.unsw.edu.au/~dons/data/Maybe.hs raw]) | + | * [http://www.cse.unsw.edu.au/~dons/data/Maybe.html Maybe.hs], the <code>Maybe</code> type ([http://www.cse.unsw.edu.au/~dons/data/Maybe.hs raw], [http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Maybe.html, docs]) |

− | * [http://www.cse.unsw.edu.au/~dons/data/Map.html Map.hs], the standard finite map ([http://darcs.haskell.org/packages/base/Data/Map.hs raw]) | + | * [http://www.cse.unsw.edu.au/~dons/data/Map.html Map.hs], the standard finite map ([http://darcs.haskell.org/packages/base/Data/Map.hs raw], [http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Map.html docs]) |

− | * [http://www.cse.unsw.edu.au/~dons/data/Graph.html Graph.hs], a graph type ([http://darcs.haskell.org/packages/base/Data/Graph.hs raw]) | + | * [http://www.cse.unsw.edu.au/~dons/data/Graph.html Graph.hs], a graph type ([http://darcs.haskell.org/packages/base/Data/Graph.hs raw], [http://haskell.org/ghc/docs/latest/html/libraries/base/Data-Graph.html docs]) |

− | * [http://www.cse.unsw.edu.au/~dons/data/Monad.html Monad.hs], basic monad support ([http://darcs.haskell.org/packages/base/Control/Monad.hs raw]) | + | * [http://www.cse.unsw.edu.au/~dons/data/Monad.html Monad.hs], basic monad support ([http://darcs.haskell.org/packages/base/Control/Monad.hs raw], [http://haskell.org/ghc/docs/latest/html/libraries/base/Control-Monad.html docs]) |

− | * [http://www.cse.unsw.edu.au/~dons/data/QuickCheck.html QuickCheck.hs], a testing framework ([http://darcs.haskell.org/packages/QuickCheck/Test/QuickCheck.hs raw]) | + | * [http://www.cse.unsw.edu.au/~dons/data/QuickCheck.html QuickCheck.hs], a testing framework ([http://darcs.haskell.org/packages/QuickCheck/Test/QuickCheck.hs raw], [http://haskell.org/ghc/docs/latest/html/libraries/QuickCheck/Test-QuickCheck.html docs]) |

− | * [http://www.cse.unsw.edu.au/~dons/data/HughesPJ.html PrettyPrint.hs], a pretty printing library ([http://darcs.haskell.org/packages/base/Text/PrettyPrint/HughesPJ.hs raw]) | + | * [http://www.cse.unsw.edu.au/~dons/data/HughesPJ.html PrettyPrint.hs], a pretty printing library ([http://darcs.haskell.org/packages/base/Text/PrettyPrint/HughesPJ.hs raw], [http://haskell.org/ghc/docs/latest/html/libraries/base/Text-PrettyPrint-HughesPJ.html docs]) |

− | * [http://www.cse.unsw.edu.au/~dons/data/State.html State.hs], a state monad ([http://darcs.haskell.org/packages/mtl/Control/Monad/State.hs raw]) | + | * [http://www.cse.unsw.edu.au/~dons/data/State.html State.hs], a state monad ([http://darcs.haskell.org/packages/mtl/Control/Monad/State.hs raw], [http://haskell.org/ghc/docs/latest/html/libraries/mtl/Control-Monad-State.html docs]) |

* [http://www.cse.unsw.edu.au/~dons/data/Music.html Music.hs], a music composition system (literate latex-style Haskell) ([http://darcs.haskell.org/haskore/src/Haskore/Music.lhs raw]) | * [http://www.cse.unsw.edu.au/~dons/data/Music.html Music.hs], a music composition system (literate latex-style Haskell) ([http://darcs.haskell.org/haskore/src/Haskore/Music.lhs raw]) | ||

* [http://www.ninebynine.org/Software/Swish-0.2.1/HaskellRDF/Dfa/Dfa.lhs Dfa.lhs], finite automata (literate Bird-style Haskell) | * [http://www.ninebynine.org/Software/Swish-0.2.1/HaskellRDF/Dfa/Dfa.lhs Dfa.lhs], finite automata (literate Bird-style Haskell) |

## Revision as of 08:06, 17 September 2006

To get a feel for what real world Haskell looks like, here are some examples from various popular Haskell projects. To start learning the language, good places to start are here, here and here.

### 1 Library code

Library code usually differs from application code: it is often highly structured, and documented with haddock, and can be rather optimised. Some instructive examples (syntax highlighting by hscolour):

- A Haskell 98 Prelude.hs, foundational Haskell library (raw, docs)
- List.hs, the standard list library (raw, docs)
- Maybe.hs, the
`Maybe`

type (raw, docs) - Map.hs, the standard finite map (raw, docs)
- Graph.hs, a graph type (raw, docs)
- Monad.hs, basic monad support (raw, docs)
- QuickCheck.hs, a testing framework (raw, docs)
- PrettyPrint.hs, a pretty printing library (raw, docs)
- State.hs, a state monad (raw, docs)
- Music.hs, a music composition system (literate latex-style Haskell) (raw)
- Dfa.lhs, finite automata (literate Bird-style Haskell)
- RedBlackTree.hs, a red-black tree (raw)
- Prime.hs, prime numbers (raw)
- Foldable.hs, traversable data structures (raw)
- ByteString.hs, high-performance string type (raw)
- Combinators.hs, array combinators
- SmallCheck.hs, a testing framework (raw)
- More code ...

### 2 Application code

Code from popular Haskell applications. Such code often makes use of a monadic IO, and sometimes other advanced features such as concurrency:

- Darcs, a revision control system (uses literate latex Haskell style)
- Pugs, a perl6 implementation
- Yi, a text editor
- Conjure, a bittorrent client
- DownNova, a file downloading program
- cpphs, an implementation of the C preprocessor
- GHC, a Haskell compiler (literate latex style)
- Djinn, a theorem prover
- c2hs, a C to Haskell interface generator
- Lambdabot, an IRC bot
- hmp3, an curses mp3 player
- More code ...

### 3 Wiki examples

Here is a list of other random code collected on this wiki, replacing CodeOnTheWiki. Contributors of code to the old area are encouraged to bring their code over here. This should only be done by the original author because anything on these pages is automatically licensed under the Simple Permissive License (HaskellWiki:Copyrights).

Most examples are roughly intermediate to advanced in difficulty.

- Prelude extensions: Simple things you've always wished were in the Prelude.
- Gallery: Complete small programs.
- Blow your mind: Short, useful, cool, magical examples, which should incite the reader's curiosity and (hopefully) lead him to a deeper understanding of advanced Haskell concepts.
- Sudoku: Solvers for popular puzzle Sudoku
- NewMonads: Useful monads that are not in the main library.
- PrincipalVariationSearch: Principal variation search
- Shootout Entries: Examples still on the old wiki for the The Computer Language Shootout Benchmarks.
- Ralf Hinze maintains a Haskell software page