Introduction to the Code Examples

This page gives access to code examples related to subjects covered in COP 4020 at UCF, as taught by Gary T. Leavens. It does not cover concepts, definitions, proofs, and examples of such that are not embodied in code.

Examples

Functional Programming in Haskell

See haskell.org for reference material and other tutorial information about Haskell.

Functions over Integers

Write functions over the Integers:

Fact.hs with tests in FactTests.hs.

Pattern Matching

Use pattern matching to write functions over tuples:

FstSnd.hs, shows fst and snd, which are built-in.

Yodaize.hs with tests in YodaizeTests.hs

Max3.hs with tests in Max3Tests.hs

Use pattern matching to write functions over lists:

HeadTail.hs shows head and tail, which are built-in.

Use built-in functions to write functions over data represented as lists:

Movies.hs

Use function definition syntax in Haskell to define functions:

FunctionDefinitionSyntaxExamples.hs

Functions over flat lists

Write recursive functions on lists, either explicitly or using a list comprehension:

Map.hs shows various ways to program map, which is built-in.

Filter.hs shows various ways to program filter, which is built-in.

ProductBy.hs

Concat.hs shows various ways to program concat, which is built-in.

AddPairs.hs

AddLists.hs

FindIndices.hs with tests in FindIndicesTests.hs

InsertWhen.hs with tests in InsertWhenTests.hs

Write tail-recursive functions on lists:

ListOperations.hs with tests in ListOperationsTests.hs

Reverse.hs with tests in ReverseTests.hs

Functions over Other Grammars

Write recursive functions over new grammars. Many of the examples in this section are discussed in the paper Following the Grammar with Haskell.

Temperature Grammar

The Temperature grammar has only base cases. It is found in Temperature.hs.

SelectOuterWear.hs

Text Grammar

The Text grammar has two productions, each with one case containing a list. It is found in Text.hs.

The file TextExamples.hs has 3 examples written over the Text grammar. See also TextExamplesTests.hs.

A higher-order abstraction of the examples in TextExamples.hs is shown in FoldText.hs. See also FoldTextTests.hs.

ISeq Grammar

The ISeq grammar has only one recursive case. It is found in ISeq.hs. (In Haskell one can do similar things with infinite lists more conveniently.)

ISeqOps.hs contains several examples of functions defined on this grammar, including iSeqMap. There are tests in ISeqOpsTests.hs, which includes an efficient way to compute Fibonacci numbers.

Infinite Lists

Be able to take advantage of laziness in Haskell by writing functions that work on infinite lists. The grammar for infinite lists of elements of some type a can be thought of as follows:

data [a] = a : [a]

Note that there is no base case in this grammar.

Hailstone.lhs describes the Hailstone or 3x+1 problem, which uses laziness in the trajectory function.

HailstonePeaks.lhs has functions that produce lazy lists.

Window Layout Grammar

The Window Layout grammar is found in WindowLayout.hs.

AddToSize.hs with tests in AddToSizeTests.hs.

DoubleSize.hs with tests in DoubleSizeTests.hs.

TotalWidth.hs with tests in TotalWidthTests.hs.

Music Grammar

The Music grammar is found in Music.hs.

MusicFuns.hs with tests in MusicFunsTests.hs.

RealMusic Grammar

The RealMusic grammar is found in RealMusic.hs. It is similar to the Music.hs, but only allows chords to be composed of notes, not arbitrary (Real)Music values.

RealMusicFuns.hs with tests in RealMusicFunsTests.hs.

Boolean Expression Grammar

The Boolean expression grammar is found in Bexp.hs.

BEval.hs with tests in BEvalTests.hs

NegateBexp.hs with tests in NegateBexpTests.hs

Sales Data Grammar

The Sales Data grammar is contained in SalesData.hs.

NormalizeSalesData.hs with tests in NormalizeSalesDataTests.hs

Statements and Expressions Grammar

The Statements and Expressions grammar contains 2 mutually recursive nonterminals. It is found in StatementsExpressions.hs.

StmtAdd1.hs with tests in StmtAdd1Tests.hs

SubstIdentifier.hs with tests in SubstIdentifierTests.hs

A Grammar of Expressions

Eval.hs with tests in EvalTests.hs

A Grammar for the Lambda Calculus

The data definitions for this grammar are found in Lambda.hs.

Several examples, including counting variable references (countVarrefs), free variables (fv), bound variables (bv), and evaluation (eval) are found in LambdaExamples.hs. Tests for these are in LambdaExamplesTests.hs.

A generalization of the above examples is in FoldLambda.hs. Tests for these are in FoldLambdaTests.hs, which shows how to use foldLambda to recover countVarrefs, fv, bv, and eval. This example is interesting in that it doesn't recurse automatically into the bodies of Lambda expressions, because the Lambda cases of bv and eval need the body as an expression, instead of just the result of the recursion.

A Grammar for "Core Haskell"

The data definitions for this grammar are found in CoreHaskellAST.hs.

CoreHaskellFreeVars with tests in CoreHaskellFreeVarsTests.hs.

CoreHaskellBoundVars with tests in CoreHaskellBoundVarsTests.hs.

Abstract Data Types

Write Abstract Data Types (ADTs) using modules.

Fraction.hs with tests in FractionTests.hs

CoreHaskellSet.hs with tests in CoreHaskellSetTests.hs.

Higher-Order Functions

Write higher order functions.

Seq.hs, with tests in SeqTests.hs.

AndOr.hs, which demonstrates how to use foldr. See also the tests in AndOrTests.hs.

Gravity.lhs, which shows that fields are curried functions. See also GravityTests.hs.

Fix.hs, which contains a fixpoint combinator fix and a use of it to define the factorial function. See also FixTests.hs.

SKICombinators.lhs, which describes the S, K, and I combinators.

CombinatorsQuiz.lhs, which shows the power of combinators and higher-order curried functions.

Write higher-order functions that abstract some programming pattern.

A higher-order abstraction of the examples in TextExamples.hs is shown in FoldText.hs. See also FoldTextTests.hs.

FoldR.hs, which abstracts from some examples (see the file) of recursion over lists. See also FoldRTests.hs.

FoldTree.hs, which abstracts from some examples (see the file) of recursion over trees. See also FoldTreeTests.hs.

Tailrec.hs, which abstracts from some examples (see the file) of tail recursions. See also TailrecTests.hs.

FoldLambda.hs, which generalizes from the examples in LambdaExamples.hs (see above). See also FoldLambdaTests.hs, which shows how to use foldLambda to recover countVarrefs, fv, bv, and eval.

FloatTesting.hs, which builds on Testing.lhs, to add capabilities for testing floating point computations.

Monadic Programming

Write monadic programs, using the IO monad or other monads.

Testing.lhs, which is designed for testing. Many examples of its use appear above.

Declarative Parallel Programming

Write parallel programs using Eval and Strategies.

HailstonePeaksRun4.hs is a parallel implemention of the Hailstone peaks search.

Actor Programming with Erlang

Functional Programming in Erlang

Write functional programs in Erlang.

factorial.erl is a parallel implemention of the factorial function, written using tail recursion. See also factorialtests.erl.

hailstone.erl has some basic computations relating to the Collatz (or hailstone or 3x+1) function. It also contains a higher-order function, graph. See also hailstone_tests.erl.

higherorder.erl shows the code for foreach, foldr, map, and foldl in Erlang. These are all in the lists module in Erlang. See also higherorder_tests.erl.

testing.erl provides higher-order functions for testing Erlang code.

Server Programming

Understand the primitives and idioms for using them in Erlang.

concurrencydemo.erl demonstrates several idioms in Erlang, including use of spawn, send, and receive.

Write a simple stateless server in Erlang.

factserver.erl is a simple server that computes factorials of integers. See factclient.erl for the client code that works with this server. See also factservertests.erl.

mathserver.erl is a simple server that computes compositions of functions found in Erlang's math module. See mathclient.erl for the client code that works with this server. See also mathservertests.erl.

Write a simple stateful server in Erlang.

printspooler.erl is a simple server that acts as a print spooler. See printer.erl for code that mimics the way that a printer would interact with the spooler. See printclients.erl for the client code that works with this spooler.

resourcearbiter.erl is a simple server that acts as a resource arbiter (like a semaphore or mutual exclusion lock). See resourcearbiterclient.erl for the client code that works with this resource arbiter and see resourcearbiter_tests.erl for tests.

server1.erl is a simple server that is quoted from section 16.1 of Joel Armstrong's book Programming Erlang (Pragmatic Press, 2007).

gauge_server.erl is a simple server that keeps track of a count. It is written using server1.erl. See gauge_server_tests.erl for tests.

shoppinglist.erl is a simple server that keeps track of a shopping list (a list of atoms). It is written using server1.erl. See shoppinglist_tests.erl for tests.

Write a stateful server using Erlang's gen_server module.

runavg.erl is a server that keeps track of the running average of three floats. It is written using Erlang's gen_server module. See runavg_tests.erl for tests.


Last modified $Date: 2013/04/24 02:08:46 $.