# CS 583: Advanced Functional Programming (Spring 2017)

## Course description

CS 583, Functional Programming, is a four-credit course for graduate students. This course introduces advanced topics of functional programming. The language being used is Haskell. Some of the topics to be covered are: type and constructor classes, monads, functional/persistent data structures, and graph reduction.

## Programming Workshop

You can learn a programming langauge only by programming in it. Therefore, in addition to the final project, we will have a series of small programming assignments that are due within 20 hours or so and that are peer-reviewed during the next class period. Each student submits his or her solution using CoE's TEACH web site, typically before noon of the day following the announcement of the programming exercise. After that all solutions will be made accessible to all other students. At the beginning of the next class, small groups will discuss the solutions and prepare a short response for the whole class, addressing clever ideas or typical errors, but also commenting on the style of solutions, potentiall offering suggestions for refactoring where applicable.

Please note: Submit only one .hs or a .lhs file for an exercise, and don't put anything into the comment box. Your solution doesn't have to be completely correct to earn credit, but it has to implement at least a partial solution, and it has to show that you tried to solve the problem. A minimum requirement is that your program compiles without errors. Put all non-working parts in comments.

Important: Only programs that compile without error will be considered and will receive credit!

### Current Programming Exercise

Solutions due: Apr 28, noon (no late submissions accepted)
Consider the following data type for representing decisions.
data Decision a = Certain a | Choice [a]
When two decisions are to be combined, a certain decision always trumps an uncertain one, but two certain decisions will be combined into one uncertain one. Two uncertain decisions remain uncertain while their arguments are concatenated.
1. Define a Monad instance for Decision. Note: It might be a good idea to define an auxiliary function for the combination of decision values that can then be used in the definition of the bind operation.
2. Consider the following function definition that tries to halve its argument. Since it is not clear what half of an odd number is, it returns a choice in that case.
half x | even x    = Certain (x `div` 2)
| otherwise = Choice [h,h+1] where h = x `div` 2
What is the outcome of the expression Certain 11 >>= half >>= half? Did you expect it? Explain why this result is obtained.

## Slides and code examples

I will make the slides and code examples available after the lectures.

 1 Introduction [pdf] (Version 03/29/17) [BinTree.hs] [RoseTree.hs] 2 Type Classes [pdf] (Version 04/11/17) [TypeClass.hs Table.hs] 3 Refactoring 4 Monads [pdf] (Version 04/17/17) [pdf (alternative slides)]

## Presentation topics and papers

Programming will be done in Haskell. There are many Haskell tutorials freely available on the web. One very nice and well written one is this:

Learn You a Haskell for Geat Good
The definite source of (basic) Haskell wisdom is, however, the following book.

Haskell 98 Language and Libraries: The Revised Report
Edited by Simon Peyton Jones. Cambridge University Press, 2003.
Another good textbook resource is Simon Thompson's book on Haskell.

Haskell: The Craft of Functional Programming
By Simon Thompson. Addison-Wesley, 3rd ed., 2011.
Finally, as background reading material for functional data structures, I recommend Chris Okasaki's book.

Purely functional data structures
By Chris Okasaki. Cambridge University Press, 1998.
I have placed all three books as a course reserve in the library.

We will mainly use the GHC interpreter, which is available on many different platforms.

## Prerequisites

Students should have already taken CS 581, Programming Languages, which introduces functional programming concepts, lambda calculus, and type systems.

NOTE: If you haven't taken CS 581, please talk to me before the class to see whether you are well enough prepared.