Homework 6 - More Haskell

Due: Wed Oct 24 11:59 PM

Since I was late in updating this homework, I only added 1 problem (instead of the 2 I promised). You want to make sure you finish it before we start talking about Prolog on the 25th since going back and forth between 2 new paradigms is a quick route to insanity.

The previous homework should have introduced you to basic functional programming, and especially the use of recursion.  The programs were fairly generic functional programs, and did not really use anything specific to Haskell (if you followed the rules).  In this assignment, you will gain experience writing some less trivial functions, including  some constructs supported by some but not all functional languages. You will also review the data structures that you are familiar with from CS 102.

You should follow all our Haskell conventions/requirements from the previous homework for this one.  However, you may also use list comprehension.

Write the following functions in Haskell. 

Problem 1: Data Structures (40%)

A directed graph G is a pair of nodes and edges, <N,E>, where each edge in E is represented as a pair of nodes (we will interpret the first element of the pair to be the source, and the second element the destination). Define a set of vertices S⊂N to be special if for every node m∈S, there exists a G-edge from m to a node in N\S (\ is the set difference symbol).

Define a type for graphs, and write the function, special graph subset.

To save time typing (and the grader a headache), use integers to represent nodes. Of course, a real program would probably use a Node constructor.

Problem 2: Recursive Data Structures (40%)

One way of defining ASTs is to have sequential composition nodes be binary and left associative (note that our example in class may have been binary and right associative). Another way is to allow sequential composition nodes to have an arbitrary number of children, one per statement (call this AST2). For this problem you will convert between these two formats.

Define an ASTBIN data structure with internal nodes named: Plus1, Minus1, Times1, Id1, Num1, Assign1, SeqComp1, Sel1, Iter1, and an AST2 data structure with internal nodes named: Plus2, ..., Iter2.  For example, a statement-list with n statements could be represented as an AST2 containing a SeqComp2 node with n children, or an ASTBIN containing n-1 SeqComp1 nodes.

Write a function ast2binast ast, that transforms an AST of type AST2 into an AST of type ASTBIN.

Make sure you submit non-trivial examples showing that your code works (including suitable boundary cases), and name these functions test2<n>.  For example, you may define a function:
test21 = ast2binast (Plus2 (Num2 2) (Num2 3))
(though this would be a silly test case).  Make sure you have good (and enough) test cases, as your grade is partly based on how well you tested your function.

Problem 3 (20%)

Consider the sequence defined by the following recurrence:
L0 = 1
L1 = 2
L2 = 3
Ln+3 = Ln + 2*Ln+1 for n>2
Write a function, seql n, that efficiently computes Ln.

You may use the built-in function zip but none other (though you can of course write your own equivalents).

Submission

The program should be submitted electronically to the grader, cc'd to the instructor.  All programs should be in one file, named  <firstname>_<surname>_hw6.hs. 

Hints / Clarifications / Corrections

  1. In problem 1, special is the name of the function, while graph and subset are the arguments (referred to as G and S in the spec). You will have to figure out how to define the data structure for G (though the problem gives it to you in English).
  2. In problem 2, you may assume that the input AST is well-formed (e.g., you don't have to support an AST with an assignment node whose left child is something other than an identifier).
  3. Problem 3 asks for an efficient solution. You should understand how we did a similar problem in class, and adapt it to this sequence. Hint: consider multiplying one sequence by 2 (since the given sequence has a "2*" term init).