Jump to content
Main menu
Main menu
move to sidebar
hide
Navigation
Haskell
Wiki community
Recent changes
Random page
HaskellWiki
Search
Search
Create account
Log in
Personal tools
Create account
Log in
Pages for logged out editors
learn more
Contributions
Talk
Editing
All About Monads
(section)
Page
Discussion
English
Read
Edit
View history
Tools
Tools
move to sidebar
hide
Actions
Read
Edit
View history
General
What links here
Related changes
Special pages
Page information
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
== Haskell code examples == This appendix contains a list of all of the code supplied with the tutorial. The source code can be [https://web.archive.org/web/20061210172052/https://www.nomaware.com/monads/html/examples.html found here]. === [[All_About_Monads#Example 1|Example 1]] === This example is discussed in the section: [[All_About_Monads#Meet the Monads|Meet the Monads]]. The example code introduces the monad concept without using Haskell typeclasses. It shows how a monadic combinator can be used to simplify the construction of computations from sequences of computations which may not return a result. === [[All_About_Monads#Example 2|Example 2]] === This example is discussed in the section: [[All_About_Monads#Doing_it_with_class|Doing it with class]] The example code builds on the first example, and shows how do-notation can be used with an instance of the <code>Monad</code> class (in this case, <code>Maybe</code> is the monad used). === [[All_About_Monads#Example 3|Example 3]] === This example is discussed in the section: [[All_About_Monads#Monad_support_in_Haskell|Monad support in Haskell]]. The example code builds on the first two examples, and shows a somewhat atypical β but very powerful β use of the <code>foldM</code> function outside of a do-block. === [[All_About_Monads#Example 4|Example 4]] === This example is discussed in the section: [[All_About_Monads#Monad_support_in_Haskell|Monad support in Haskell]]. The example code shows a more typical use of the <code>foldM</code> function within a do-block. It combines dictionary values read from different files into a single dictionary using the <code>foldM</code> function within the IO monad. === [[All_About_Monads#Example 5|Example 5]] === This example is discussed in the section: [[All_About_Monads#Monad_support_in_Haskell|Monad support in Haskell]]. The example code shows the use of the <code>filterM</code> function within a do-block. It prints the subset of its arguments that specify directories and ignores non-directory arguments. === [[All_About_Monads#Example 6|Example 6]] === This example is discussed in the section: [[All_About_Monads#Monad_support_in_Haskell|Monad support in Haskell]]. The example code shows the use of the <code>liftM</code> function within a do-block. It looks up a name in a list and uses a lifted String manipulation function to modify it within the Maybe monad. === [[All_About_Monads#Example 7|Example 7]] === This example is discussed in the section: [[All_About_Monads#Monad_support_in_Haskell|Monad support in Haskell]]. The example code shows a higher-order application of <code>liftM2</code>. It folds lifted operations within the List monad to produce lists of all combinations of elements combined with the lifted operator. === [[All_About_Monads#Example 8|Example 8]] === This example is discussed in the section: [[All_About_Monads#Monad_support_in_Haskell|Monad support in Haskell]]. The example code shows a higher-order application of <code>ap</code>. It folds <code>ap</code> through a list of <code>Maybe (a->a)</code> functions to process sequences of commands. === [[All_About_Monads#Example 9|Example 9]] === This example is discussed in the section: [[All_About_Monads#Monad_support_in_Haskell|Monad support in Haskell]]. The example code shows the use of <code>msum</code> in the Maybe monad to select the first variable match in a stack of binding environments. === [[All_About_Monads#Example 10|Example 10]] === This example is discussed in the section: [[All_About_Monads#Monad_support_in_Haskell|Monad support in Haskell]]. The example code shows the use of <code>guard</code> in the Maybe monad to select only the records from a list that satisfy a predicate (equivalent to <code>filter</code>). === [[All_About_Monads#Example 11|Example 11]] === This example is discussed in the section: [[All_About_Monads#The_Maybe_monad|The Maybe monad]]. The example code shows how to use the <code>Maybe</code> monad to build complex queries from simpler queries that may fail to return a result. The specific example used is looking up mail preferences for someone based on either their full name or a nickname. === [[All_About_Monads#Example 12|Example 12]] === This example is discussed in the section: [[All_About_Monads#The_Error_monad|The Error monad]]. The example code demonstrates the use of the <code>Either</code> type constructor as an <code>Error</code> monad with a custom error type. The example parses hexadecimal digits and uses the exception handling mechanism of the <code>Error</code> monad to provide informative error messages in the event of a parse failure. === [[All_About_Monads#Example 13|Example 13]] === This example is discussed in the section: [[All_About_Monads#The List monad|The List monad]]. The example code uses the built-in list type constructor as a monad for non-deterministic computation. The example demonstrates parsing an ambiguous grammar consisting of integers, hex values, and words. === [[All_About_Monads#Example 14|Example 14]] === This example is discussed in the section: [[All_About_Monads#The IO monad|The IO monad]]. The example code implements a simple version of the standard Unix command "tr". The example demonstrates use of the IO monad including implicit <code>fail</code> calls due to pattern matching failures and the use of <code>catcherror</code>. === [[All_About_Monads#Example 15|Example 15]] === This example is discussed in the section: [[All_About_Monads#The State monad|The State monad]]. The example code shows how the State monad can be used instead of explicitly passing state. The example uses the State monad to manage the random number generator state while building a compound data value requiring multiple calls to the random number generator. === [[All_About_Monads#Example 16|Example 16]] === This example is discussed in the section: [[All_About_Monads#The Reader monad|The Reader monad]]. The example code shows how the Reader monad can be used to simplify computations involving a shared environment. The example uses the Reader monad to implement a simple template substitution system. The example code demonstrates the use of the Parsec monadic parser combinator library. === [[All_About_Monads#Example 17|Example 17]] === This example is discussed in the section: [[All_About_Monads#The Writer monad|The Writer monad]]. The example code shows how the Writer monad can be used to implement logging. The example implements a very simple firewall simulator and uses the Writer monad to log the firewall activity. === [[All_About_Monads#Example 18|Example 18]] === This example is discussed in the section: [[All_About_Monads#The Continuation monad|The Continuation monad]]. The example code shows how the Continuation monad's escape continuations work. The example computes a complex transformation of a number. === [[All_About_Monads#Example 19|Example 19]] === This example is discussed in the section: [[All_About_Monads#Combining monads the hard way|Combining monads the hard way]]. The example code shows how the Continuation monad can be nested within the IO monad given a suitable computational structure. The example is a slight modification of example 18. === [[All_About_Monads#Example 20|Example 20]] === This example is discussed in the section: [[All_About_Monads#Combining monads the hard way|Combining monads the hard way]]. The example code shows how the Continuation monad and IO monad can be used simultaneously, but without using monad transformers. The example builds on examples 18 and 19. === [[All_About_Monads#Example 21|Example 21]] === This example is discussed in the section: [[All_About_Monads#Monad transformers|Monad transformers]]. The example code shows how the transformer version of the Continuation monad can be used to create a combined monad for using continuations and doing I/O. The example builds on examples 18, 19 and 20. === [[All_About_Monads#Example 22|Example 22]] === This example is discussed in the section: [[All_About_Monads#Standard monad transformers|Standard monad transformers]]. The example code shows how the transformer version of the Writer monad can be used to create a combined monad for logging and doing I/O. The example adds timestamps to the log entries of the firewall simulator from example 17. === [[All_About_Monads#Example 23|Example 23]] === This example is discussed in the section: [[All_About_Monads#Standard monad transformers|Standard monad transformers]]. The example code shows how the transformer version of the Reader monad can be used to create a monad that combines a shared environment with I/O. The example converts the template system of example 16 to use files as templates. === [[All_About_Monads#Example 24|Example 24]] === This example is discussed in the section: [[All_About_Monads#Standard monad transformers|Standard monad transformers]]. The example code uses the <code>StateT</code> transformer on the List monad to create a combined monad for doing non-deterministic stateful computations. The example uses the combined monad to solve a logic problem. === [[All_About_Monads#Example 25|Example 25]] === This example is discussed in the section: [[All_About_Monads#An example with multiple transformers|An example with multiple monad transformers]]. The example code uses the <code>StateT</code> and <code>WriterT</code> transformers on the List monad to create a combined monad for doing non-deterministic stateful computations with logging. The example uses the combined monad to solve the N-queens problem. === [[All_About_Monads#Example 26|Example 26]] === This example is discussed in the section: [[All_About_Monads#Heavy lifting|Heavy lifting]]. The example code demonstrates the use of the <code>lift</code> function and the necessity of managing its use in complex transformer stacks. [[Category:Monad]] [[Category:Standard classes]] [[Category:Standard libraries]] [[Category:Standard packages]] [[Category:Standard types]] [[Category:Tutorials]]
Summary:
Please note that all contributions to HaskellWiki are considered to be released under simple permissive license (see
HaskellWiki:Copyrights
for details). If you don't want your writing to be edited mercilessly and redistributed at will, then don't submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource.
DO NOT SUBMIT COPYRIGHTED WORK WITHOUT PERMISSION!
Cancel
Editing help
(opens in new window)
Toggle limited content width