Difference between revisions of "Example code"

From HaskellWiki
Jump to navigation Jump to search
m (→‎Wiki examples: Link update)
(→‎Examples: Redirected link to the Web Archive)
 
(18 intermediate revisions by 6 users not shown)
Line 2: Line 2:
 
examples from various popular [[Libraries_and_tools|Haskell projects]].
 
examples from various popular [[Libraries_and_tools|Haskell projects]].
 
To start learning the language, good places to start are
 
To start learning the language, good places to start are
  +
[[Learning Haskell]],
[[Learning_Haskell|here]], [[Haskell_in_5_steps|here]] and
 
  +
[[Haskell in 5 steps]], and
[[Books_and_tutorials|here]].
 
  +
[[Books and tutorials]].
  +
  +
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 ===
   
 
Library code usually differs from application code: it is often highly
 
Library code usually differs from application code: it is often highly
structured, and documented with [http://haskell.org/haddock haddock],
+
structured, and documented with [http://www.haskell.org/haddock/ haddock],
 
and can be rather optimised. Some instructive examples (syntax
 
and can be rather optimised. Some instructive examples (syntax
 
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://haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html docs])
+
* [http://www.haskell.org/ghc/docs/latest/html/libraries/base/src/Prelude.html A Haskell Prelude.hs], foundational Haskell library ([http://www.haskell.org/ghc/docs/latest/html/libraries/base/Prelude.html docs])
  +
* [[Simple_unix_tools|Unix.hs]], simple unix tools, for beginner Haskellers ([http://www.cse.unsw.edu.au/~dons/data/Basics.hs raw])
+
* [[Simple_Unix_tools | Unix.hs]], simple unix tools, for beginner Haskellers
* [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://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://haskell.org/ghc/docs/latest/html/libraries/base/Data-Map.html docs])
+
* [http://hackage.haskell.org/packages/archive/base/latest/doc/html/src/Data.List.html Data.List], the standard list library ([http://hackage.haskell.org/packages/archive/base/latest/doc/html/Data-List.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://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://haskell.org/ghc/docs/latest/html/libraries/base/Control-Monad.html docs])
+
* [http://hackage.haskell.org/packages/archive/base/latest/doc/html/src/Data.Maybe.html Data.Maybe], the <code>Maybe</code> type ([http://hackage.haskell.org/packages/archive/base/latest/doc/html/Data-Maybe.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://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://haskell.org/ghc/docs/latest/html/libraries/base/Text-PrettyPrint-HughesPJ.html docs])
+
* [http://hackage.haskell.org/packages/archive/containers/latest/doc/html/src/Data.Map.html Data.Map], the standard finite map ([http://www.haskell.org/ghc/docs/latest/html/libraries/containers-0.4.2.1/index.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://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://haskell.org/haskore/ home])
+
* [http://hackage.haskell.org/packages/archive/containers/latest/doc/html/src/Data.Graph.html Data.Graph], a graph type ([http://hackage.haskell.org/packages/archive/containers/latest/doc/html/Data-Graph.html docs])
  +
  +
* [http://hackage.haskell.org/packages/archive/base/latest/doc/html/src/Control.Monad.html Control.Monad], basic monad support ([http://hackage.haskell.org/packages/archive/base/latest/doc/html/Control-Monad.html docs])
  +
  +
* [https://github.com/nick8325/quickcheck QuickCheck], a testing framework ([http://hackage.haskell.org/package/QuickCheck 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://hackage.haskell.org/packages/archive/transformers/latest/doc/html/src/Control-Monad-Trans-State-Lazy.html Control.Monad.Trans.State.Lazy], 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.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)
  +
* [http://www.cse.unsw.edu.au/~dons/data/RedBlackTree.html RedBlackTree.hs], a red-black tree ([http://www.polyomino.f2s.com/david/haskell/hs/redblacktree.hs.txt raw])
 
  +
* [http://www.polyomino.f2s.com/david/haskell/hs/RedBlackTree.hs.txt RedBlackTree.hs], 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://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.cse.unsw.edu.au/~dons/data/ByteString.html ByteString.hs], high-performance string type ([http://darcs.haskell.org/packages/base/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://hackage.haskell.org/packages/archive/base/latest/doc/html/src/Data.Foldable.html Data.Foldable], 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/SmallCheck.html SmallCheck.hs], a testing framework ([http://www.cse.unsw.edu.au/~dons/code/lambdabot/scripts/SmallCheck.hs raw])
 
  +
* [http://haskell.org/haskellwiki/Darcs_repositories More code ...]
 
  +
* [https://github.com/haskell/bytestring Data.ByteString], 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 SmallCheck], 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/haskellwiki/Darcs_repositories More code ...]
   
 
=== Application code ===
 
=== Application code ===
Line 36: Line 62:
 
monadic IO, and sometimes other advanced features such as concurrency:
 
monadic IO, and sometimes other advanced features such as concurrency:
   
  +
<!--
* [http://www.abridgegame.org/repos/darcs-unstable/PatchCommute.lhs Darcs], a revision control system (uses literate latex Haskell style) ([http://darcs.net home])
+
* [http://nanardon.zarb.org/darcs/darcs/PatchCommute.lhs Darcs], a revision control system (uses literate latex Haskell style) ([http://darcs.net home])
  +
-->
 
* [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-core/src/Yi/Core.hs Yi], a text editor, ([http://www.haskell.org/haskellwiki/Yi home], [https://raw.githubusercontent.com/yi-editor/yi/master/yi-core/src/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://darcs.haskell.org/~lemmih/downNova/src/Torrent.hs DownNova], a file downloading program
 
* [http://darcs.haskell.org/~lemmih/downNova/src/Torrent.hs DownNova], a file downloading program
  +
-->
* [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://haskell.org/ghc home])
 
* [http://www.augustsson.net/Darcs/Djinn/LJT.hs Djinn], a theorem prover ([http://darcs.augustsson.net/Darcs/Djinn home])
+
* [http://www.cs.york.ac.uk/fp/darcs/cpphs/Language/Preprocessor/Cpphs/Tokenise.hs cpphs], an implementation of the C preprocessor ([https://archives.haskell.org/projects.haskell.org/cpphs/ home])
  +
* [http://darcs.haskell.org/c2hs/c2hs/c/CTrav.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://darcs.haskell.org/ghc/compiler/simplCore/Simplify.lhs GHC], a Haskell compiler (literate latex style) ([http://www.haskell.org/ghc/ 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://darcs.augustsson.net/Darcs/Djinn/Djinn/LJT.hs Djinn], a theorem prover ([http://darcs.augustsson.net/Darcs/Djinn/ home])
* [http://haskell.org/haskellwiki/Darcs_repositories More code ...]
 
  +
 
* [http://code.haskell.org/c2hs/src/C2HS/C/Trav.hs c2hs], a C to Haskell interface generator ([https://github.com/haskell/c2hs 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 ...]
   
=== Wiki examples===
+
== 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).
Here is a list of other random code collected on this wiki, replacing
 
[http://haskell.org/hawiki/CodeOnTheWiki 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]]).
 
   
  +
; [https://web.archive.org/web/20201109033750/members.chello.nl/hjgtuyl/tourdemonad.html A tour of the Haskell Monad functions]: Small usage examples of the basic Haskell monad functions
Most examples are roughly intermediate to advanced in difficulty.
 
   
  +
[[Category:Code]]
* [[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 [http://en.wikipedia.org/wiki/Sudoku Sudoku]
 
* [[New monads]]: Useful monads that are not in the main library.
 
* [[Principal variation search]]
 
* [[Great language shootout]]: Examples still on the old wiki for the [http://shootout.alioth.debian.org/index.php The Computer Language Shootout Benchmarks].
 
* [http://www.informatik.uni-bonn.de/~ralf/ Ralf Hinze] maintains a [http://www.informatik.uni-bonn.de/~ralf/software.html Haskell software page]
 

Latest revision as of 11:17, 22 October 2022

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
  • Dfa.lhs, finite automata (literate Bird-style Haskell)

Application code

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

  • 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