Homework 3
This homework is due on Thursday 21st November (This deadline is
immutable). I encourage you to do questions 2 and 3 before the next
learning experience (Thursday 14th November).
This homework deals with Functional programming and JAVA.
This homework is to be done in a group of two students by following
the rules of 'Extreme Mathematical Thinking' and 'Pair
Programming'. Take advantage of working in group. The groups
are assigned. You can find the email of your group member in the
Blackboard. If you are alone it means that you asked me to be alone or
I do not have enough information (grades) to assign you a group. The
groups were provided.
You have to submit a report concerning your group member. These reports can be
given apart (but must be given on 11/21/2002). In case of problem with
your group member during the time of the homework please contact me
(but you cannot change group). Your grade will be based on the reports
you will submit. It means that if you are in the same group it does
NOT mean you will have the same grade.
Students have to hand-in a hard-copy with their answers and the
listing of all their JAVA and SML code. The presentation of your
homework will be taken into account and you have to do the questions
in order. The JAVA and SML code must be accessible from matrix in the
directory called CS361/HW3/. ALL your SML code must be saved in a file
called smlcode.txt. Do not put anything except SML code in
smlcode.txt. The SML code has to be submitted on-line also. Click
here to have information about how to submit your SML code
online. The JAVA code you write must be clear and commented. All the
explanations to access the code on matrix must be provided in your
hard-copy on the cover page. Your JAVA code will not be submitted
online but will be only available on matrix.
All the names (names of directories, files, classes, methods, ...)
must be respected (exactly) for automated testing.
1. We saw in class that to compute A andalso B (SML
syntax for A and B) SML uses a lazy evaluation. When computing
A && B, does JAVA use a eager or lazy evaluation? Provide the
JAVA code you used to test if A and B is evaluated by eager or
lazzy evaluation in JAVA.
2. We decide to represent a set by a list (without
duplicates). For example, the set {1,2,4,2,6} will be represented by
[1,2,4,6].
We propose to write the following functions on sets.
- Declare an exception called NOTASET using:
exception NOTASET;. This will help you to write functions on sets. You will have to raise the NOTASET exception in some cases. I do NOT ask you to write code to handle the exception.
-
isSet tests if a list is a set. For example, isSet[1,4,4,8] (or isSet([1,4,4,8]) returns false and isSet[1,4,8] (or isSet([1,4,8]) returns true.
-
isEmptySet tests if a set is empty or not. The parameter of the function must be a set otherwise you raise the NOTASET exception.
-
isInSet tests if an element is in a set. The parameter of the function must be a set otherwise you raise the NOTASET exception.
-
unionSets computes the union of 2 sets. The parameters of the function must be sets otherwise you raise the NOTASET exception and the result must be a set also. You have to force the result of the union of 2 sets to be a set.
-
intersectSets computes the intersection of 2 sets. The parameters of the function must be sets otherwise you raise the NOTASET exception and the result must be a set also. You have to force the result of the union of 2 sets to be a set.
-
differenceSets computes the difference of 2
sets. differenceSets(A,B) means A-B. The parameters of the function must be sets otherwise you raise the NOTASET exception and the result must be a set also. You have to force the result of the union of 2 sets to be a set.
For definitions concerning sets and set operations read these slides.
Write the functions isSet, isEmptySet, isInSet and ONE of the following functions: unionSets, intersectSets and differenceSets.
3. Choose and do ONE of the following exercises:
- Implement the same (recursive) algorithm of Bubble sort
(sorting an array of ints) in JAVA and SML (sorting a list of ints)
and compare the 2 codes. Your comparisons must be presented in a
table. What is the complexity of the algorithm of Bubble sort you
implemented in time and space?
Hints for SML:
1. Define a function Iteration that repeats the treatment on a
data while a condition on this data is not true.
The type of Iteration is: Iteration : ('a -> bool) -> ('a -> 'a) -> 'a -> 'a
2. Define a function isSorted that returns true if a list is
sorted, false otherwise.
3. Write a function Bubble:int list -> int list that implements the Bubble sort
using 1. and 2..
For JAVA create a file bubbleSort.java and have your main in
mainBubbleSort.java. The function that does Bubble sort in JAVA must be
called Bubble.
- Implement the same (recursive) algorithm of Insertion sort
(sorting an array of ints) in JAVA and SML (sorting a list of ints)
and compare the 2 codes. Your comparisons must be presented in a
table. What is the complexity of the algorithm of Insertion sort
you implemented in time and space?
Hints for SML:
Define a function Insert that inserts an int in a sorted
list. The type of Insert is: Insert : int * int list ->
int list
Use Insert to define insertionSort. The type of
insertionSort is: insertionSort : int list -> int list
For JAVA create a file insertSort.java and have your main in
mainInsertSort.java. The function that does Insertion sort in JAVA must be
called insertionSort.
4. Extra-credit Choose and do ONE of the following
exercises:
-
Read the chapter on Functional programming in the book. Compare the
Haskell language with SML. Present your comparisons in a table.
-
Compare the JAVA language with SML. Present your comparisons in a
table.
- Play with the haskcore music system and compose an orginal
music. (http://haskell.cs.yale.edu/haskore/)