Difference between revisions of "Example code"

From HaskellWiki
Jump to navigation Jump to search
(Obsolete links removed, commented out, or updated (more to do))
(More link updating)
Line 8: Line 8:
 
More code may be found [http://www.haskell.org/haskellwiki/Category:Code on the wiki].
 
More code may be found [http://www.haskell.org/haskellwiki/Category:Code on the wiki].
   
 
<!--
  +
Some lines are commented out for later update
 
-->
   
 
=== Library code ===
 
=== Library code ===
Line 33: Line 36:
   
 
* [http://hackage.haskell.org/packages/archive/pretty/latest/doc/html/src/Text-PrettyPrint-HughesPJ.html PrettyPrint.hs], a pretty printing library ([http://hackage.haskell.org/package/pretty docs])
 
* [http://hackage.haskell.org/packages/archive/pretty/latest/doc/html/src/Text-PrettyPrint-HughesPJ.html PrettyPrint.hs], a pretty printing library ([http://hackage.haskell.org/package/pretty 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.haskell.org/ghc/docs/latest/html/libraries/mtl/Control-Monad-State-Lazy.html docs])
+
* [http://hackage.haskell.org/packages/archive/transformers/latest/doc/html/src/Control-Monad-Trans-State-Lazy.html State.hs], a state monad ([http://hackage.haskell.org/packages/archive/transformers/latest/doc/html/Control-Monad-Trans-State-Lazy.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.haskell.org/haskore/ home])
 
* [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.haskell.org/haskore/ home])
Line 42: Line 44:
   
 
* [http://www.polyomino.f2s.com/david/haskell/hs/RedBlackTree.hs.txt], a red-black tree
 
* [http://www.polyomino.f2s.com/david/haskell/hs/RedBlackTree.hs.txt], a red-black tree
  +
<!--
 
 
* [http://www.cse.unsw.edu.au/~dons/data/Prime.html Prime.hs], prime numbers ([http://andrew.bromage.org/darcs/numbertheory/Math/Prime.hs raw])
 
* [http://www.cse.unsw.edu.au/~dons/data/Prime.html Prime.hs], prime numbers ([http://andrew.bromage.org/darcs/numbertheory/Math/Prime.hs raw])
  +
-->
  +
* [http://hackage.haskell.org/packages/archive/base/latest/doc/html/src/Data-Foldable.html Foldable.hs], traversable data structures ([http://hackage.haskell.org/packages/archive/base/latest/doc/html/Data-Foldable.html docs])
   
* [http://www.cse.unsw.edu.au/~dons/data/Foldable.html Foldable.hs], traversable data structures ([http://darcs.haskell.org/packages/base/Data/Foldable.hs raw])
+
* [http://www.haskell.org/ghc/docs/latest/html/libraries/bytestring/src/Data-ByteString.html ByteString.hs], high-performance string type ([http://code.haskell.org/bytestring/Data/ByteString.hs raw], [http://hackage.haskell.org/package/bytestring docs])
   
  +
* [https://github.com/feuerbach/smallcheck/blob/master/Test/SmallCheck.hs], a testing framework ([https://raw.github.com/feuerbach/smallcheck/master/Test/SmallCheck.hs raw], [http://hackage.haskell.org/package/smallcheck docs])
* [http://www.haskell.org/ghc/docs/latest/html/libraries/bytestring/src/Data-ByteString.html ByteString.hs], high-performance string type ([http://darcs.haskell.org/packages/bytestring/Data/ByteString.hs raw], [http://www.cse.unsw.edu.au/~dons/fps/Data-ByteString.html docs], [http://www.cse.unsw.edu.au/~dons/fps.html home])
 
 
* [http://www.cse.unsw.edu.au/~dons/data/SmallCheck.html SmallCheck.hs], a testing framework ([http://www.cse.unsw.edu.au/~dons/code/lambdabot/scripts/SmallCheck.hs raw])
 
   
 
* [http://www.haskell.org/haskellwiki/Darcs_repositories More code ...]
 
* [http://www.haskell.org/haskellwiki/Darcs_repositories More code ...]
  +
   
 
=== Application code ===
 
=== Application code ===
Line 62: Line 65:
 
-->
 
-->
 
* [http://svn.openfoundry.org/pugs/src/Pugs/Eval.hs Pugs], a perl6 implementation ([http://pugscode.org home])
 
* [http://svn.openfoundry.org/pugs/src/Pugs/Eval.hs Pugs], a perl6 implementation ([http://pugscode.org home])
  +
* [http://www.cse.unsw.edu.au/~dons/code/yi/Yi/Core.hs Yi], a text editor, ([http://www.cse.unsw.edu.au/~dons/yi.html home])
 
  +
* [https://github.com/yi-editor/yi/blob/master/yi/src/library/Yi/Core.hs Yi], a text editor, ([http://www.haskell.org/haskellwiki/Yi home], [https://raw.github.com/yi-editor/yi/master/yi/src/library/Yi/Core.hs raw, [http://hackage.haskell.org/package/yi doc])
  +
 
* [http://j.mongers.org/pub/haskell/darcs/conjure/Conjure/Torrent.hs Conjure], a bittorrent client
 
* [http://j.mongers.org/pub/haskell/darcs/conjure/Conjure/Torrent.hs Conjure], a bittorrent client
 
<!--
 
<!--
Line 68: Line 73:
 
-->
 
-->
 
* [http://www.cs.york.ac.uk/fp/darcs/cpphs/Language/Preprocessor/Cpphs/Tokenise.hs cpphs], an implementation of the C preprocessor ([http://www.cs.york.ac.uk/fp/cpphs/ home])
 
* [http://www.cs.york.ac.uk/fp/darcs/cpphs/Language/Preprocessor/Cpphs/Tokenise.hs cpphs], an implementation of the C preprocessor ([http://www.cs.york.ac.uk/fp/cpphs/ home])
  +
 
* [http://darcs.haskell.org/ghc/compiler/simplCore/Simplify.lhs GHC], a Haskell compiler (literate latex style) ([http://www.haskell.org/ghc/ home])
 
* [http://darcs.haskell.org/ghc/compiler/simplCore/Simplify.lhs GHC], a Haskell compiler (literate latex style) ([http://www.haskell.org/ghc/ home])
  +
 
* [http://darcs.augustsson.net/Darcs/Djinn/Djinn/LJT.hs Djinn], a theorem prover ([http://darcs.augustsson.net/Darcs/Djinn/ home])
 
* [http://darcs.augustsson.net/Darcs/Djinn/Djinn/LJT.hs Djinn], a theorem prover ([http://darcs.augustsson.net/Darcs/Djinn/ home])
  +
 
* [http://code.haskell.org/c2hs/src/C2HS/C/Trav.hs c2hs], a C to Haskell interface generator ([http://www.cse.unsw.edu.au/~chak/haskell/c2hs/ home])
 
* [http://code.haskell.org/c2hs/src/C2HS/C/Trav.hs c2hs], a C to Haskell interface generator ([http://www.cse.unsw.edu.au/~chak/haskell/c2hs/ home])
  +
* [http://www.cse.unsw.edu.au/~dons/code/lambdabot/LBState.hs Lambdabot], an IRC bot ([http://www.cse.unsw.edu.au/~dons/lambdabot.html home])
 
* [http://www.cse.unsw.edu.au/~dons/code/hmp3/Core.hs hmp3], an curses mp3 player ([http://www.cse.unsw.edu.au/~dons/hmp3.html home])
+
* [http://code.haskell.org/lambdabot/LBState.hs Lambdabot], an IRC bot ([http://www.haskell.org/haskellwiki/Lambdabot home])
  +
  +
* [http://code.haskell.org/~dons/code/hmp3/Core.hs hmp3], a curses mp3 player
  +
 
* [http://www.haskell.org/haskellwiki/Darcs_repositories More code ...]
 
* [http://www.haskell.org/haskellwiki/Darcs_repositories More code ...]
  +
   
 
== Examples ==
 
== Examples ==

Revision as of 20:43, 25 February 2012

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 Learning Haskell, Haskell in 5 steps, and Books and tutorials.

More code may be found on the wiki.


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):

  • Unix.hs, simple unix tools, for beginner Haskellers


Application code

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

  • Conjure, a bittorrent client
  • cpphs, an implementation of the C preprocessor (home)
  • GHC, a Haskell compiler (literate latex style) (home)
  • c2hs, a C to Haskell interface generator (home)
  • hmp3, a curses mp3 player


Examples

Tying the Knot
An example that illustrates different ways to define recursive data structures. The example defines a simple language (illustrating how to define some recursive structures) and an interpreter for the language (illustrating how to work with the recursive structures).
A tour of the Haskell Monad functions
Small usage examples of the basic Haskell monad functions