| CS 330 | Home | Schedule | Resources |
Estimated time: 4 hours
Acceptance tests for assignment 8.
Grading rubric (i.e. point values) for problems.
Objectives: Practice declarative techniques on lists and trees, and higher-order programming.
This assignment delves further into concepts related to declarative programming on lists. It focuses on the implementation of more complicated algorithms than those of Assignment 3. For all problems where you are directed to write or define a procedure, submit your code with your homework.
I sort, you sort, we all sort...
This exercise consists in implementing algorithms
in the declarative model . Implement and test ONE of the two sorting algoritms below. Whichever you choose, call the function Sort. Make your implementation as efficient as possible, using tail recursion and difference lists if reasonable. Each algorithm is illustrated with the list [5 1 7 2 6 4 3 ].
Insertion sort. Take the elements of the list one by one, and insert them into place in a sorted list. When the last element is inserted, the second list contains all the elements in order. Hint: Define a function which calculates the result of inserting a value X into a sorted list Ys . See the left figure below.
Selection sort. Remove from the list the smallest element, put it in the result, and start again with the remaining list. The elements are thus removed from the basic list exactly in the order of the final result. Hint: Define a function which returns the minimum element of a list, as well as a function which calculates the result of removing a value X from a list Ys . See the right figure below.
|
Example of sorting by insertion |
Example of sorting by selection |
SumInts which calculates the sum of all the integers in the list (ignoring the atoms).IsInt function, and deduce the various required cases from the EBNF. {Push X Lin ?Lout Nin ?Nout} {Pop ?X Lin ?Lout Nin ?Nout} and {IsEmpty ?X Lin ?Lout Nin ?Nout}.
(? means that the variable which follows is affected
by the procedure; it is thus a result of the procedure.)
Now that we have defined our abstract type, stack, we will be able to use
it as in this illustrative example:
Define a procedure { Twosteps Lin ?Lout Nin ?Nout } which
models with a stack the fact that somebody advances according to the song
"Three steps forward, two steps back...".
The modeling of a step is done by introducing the element forward
on the stack. For a step back, one withdraws one forward.
Twosteps will thus contain instructions of the type
{Push forward Lin Lout Nin Nout}
and the state will thus move
(L,N) successively through these states:
(nil, 0) |
([ forward], 1) |
([ forward forward], 2) |
([ forward forward forward], 3) |
([ forward forward], 2) |
([ forward], 1) |
Check this final state.
One can deduce from it that at the end of a stage "three steps forward, two
steps back", that one advanced overall 1 step ahead!
[Don't write a tree-building function, just assign it to a variable from a literal. If trees were composed of foos and bars, for instance, this would be Tree = foo(bar(bar)). In other words, show how to translate the EBNF into Oz code. -Max]
Note: You can use the Drawer
module to visualize the constructed tree.
Write a function {Sum T} which returns the sum
of the values of all the nodes of the tree. Use pattern matching and deduce
the various cases from the definition which you formalized. For full credit, make sure your
function is tail-recursive.
{DepthFirst T} which takes a tree as a parameter
and returns a list of the values of the nodes visited in depth-first
order. Hint: This problem can be solved similarly to Flatten covered in class and in the textbook at the bottom of page 144 by building the output list from tail to head.
Cube1 and Cube2, respectively. Compare their efficiency by running each with the same set of 100 different inputs and averaging their runtimes. (You can use the built-in Property.get function to get the current time in milliseconds, e.g.: CurrentTime = {Property.get 'time.total'}'.) Also, verify whether they behave the same in terms of visiblity of the helper routines.
How do you use Drawer ? Use these code snippets as an example.
% load the module Draw from the textbook's web page
declare
[Drawer] = {Module.link ["http://www.info.ucl.ac.be/notes_de_cours/LINF1251/Drawer.ozf"]}
Draw = Drawer.draw
% to draw a variable Tree
declare Tree = tree {Draw Tree}
% example tree--click on a node to expand it
{Draw tree(leaf(1) tree(tree(tree(leaf(2) tree(leaf(3) leaf(4))) leaf(5)) leaf(6)))}
% another example--Drawer doesn't need you to name nodes "tree" and "leaf"
{Draw flower(fish(1) fish(2) flower(fish(3) fish(4)))}