Sei sulla pagina 1di 4

TDT4165 PROGRAMMING LANGUAGES

Fall 2008

Exercise 2
The Basics Revisited Introduction
In this exercise we will delve deeper inside the world of Oz, gaining experience and practice with some more advanced programming concepts. You should try to solve these tasks using declarative programming techniques only (that is, no explicit state, no cells).

Task 1: Map Part II


Write a function {MapTwo L1 L2 F} that takes as input two lists and a function of two parameters and iterates each list, applying {F E1 E2} to an element of the rst list and an element of the second list. For example, {MapTwo [1 2] [3 4] fun {$ X Y} X+Y end} {MapTwo [1 2] [3 4] fun {$ X Y} X*Y end} % returns [4 6] % returns [3 8]

Task 2: The Versatile Add


Write a function {Add A B} that returns the sum of the two input arguments. The arguments can be numbers, vectors of numbers or matrices of numbers (represented as nested lists). For example, {Add 1 2} % returns 3 {Add [1 2] [3 4]} % returns [4 6] {Add [[1 2] [3 4]] [[5 6] [7 8]]} % returns [[6 8] [10 12]]

Task 3: Matrix Multiplication


Write a function {MatrixMult M1 M2} that takes as input two nested lists representing matrices and calculates their product. 1
Have a look at http://en.wikipedia.org/wiki/matrix multiplication for the mathematical denition of matrix multiplication
1

Hint: use your solution to task 1 and 2. {MatrixMult [[1 0 2] [~1 3 1]] [[3 1] [2 1] [1 0]]} % returns [[5 1] [4 2]] {MatrixMult [[4 7] [3 1]] [[8 1] [1 7]]} % returns [[39 53] [25 10]]

Task 4: The Olympic Games


The Olympic Committee has given you an (unordered) list of countries and medals. They want you to create a function that will aggregate the information in the list so that each country appears only once with their respective medals in the aggregated list. For example, Medals = [medals(country:norway gold:3) medals(country:sweden silver:4) medals(country:denmark gold:2) medals(country:norway bronze:2) medals(country:norway silver:5) medals(country:denmark bronze:4)] {Aggregate Medals} % returns: % [medals(country:norway gold:3 silver:5 bronze:2) % medals(country:sweden silver:4) % medals(country:denmark gold:2 bronze:4)] Hint: have a look at the section on Records and Tuples in the Oz tutorial at http://www.mozart-oz.org/documentation/tutorial/node3.html#label20 for some helpful operations.

Task 5: List Trees


It is possible to dene tree structures using lists in Oz. Trees are built by having lists inside lists. The height of a tree is the greatest number of nodes from the root to a leaf. nil-nodes dont count. For example, the height of the tree [[a] b [c]] is 5. Write a method that calculates the height of a tree represented as a nested list. [[a] b [c]] / \ [a] [b [c]] / \ / \ a nil b [[c]] / \ [c] nil / \ c nil 2

Task 6: Merge
Write a function {Merge L1 L2 L3} that merges three lists. The second input list should start where the rst input list ends. The third input list should start where the second input list ends. Be sure to account for cases where the input lists are empty. For example, {Merge [1 2 3] [4] [5 6]} % returns [1 2 3 4 5 6] {Merge [6 5 4] nil [3 2 1]} % returns [6 5 4 3 2 1] {Merge nil [1] nil} % returns [1]

Task 7: Sort
Write a function that takes a list as input and sorts it. You may choose whatever sorting algorithm you want. Remember that characters are represented by their corresponding character codes. For example, {Sort [5 4 2 6]} % returns [2 4 5 6] {Sort [b a t]} % returns [a b t]

Task 8: Models of Computation


What is a model of computation? Describe the most important properties of the declarative model.

Task 9: Lost in Translation


You are now going to rewrite some known functions from the extended language. You can only use kernel language, that is, you can only use the syntax from table 2.1 and 2.2 in CTMC 2 and the built-in operations from table 2.3. fun {Append Xs Ys} case Xs of nil then Ys [] X|Xr then X|{Append Xr Ys} end end
Concepts, Techniques and Models of Computer Programming, Peter van Roy and Seif Haridi.
2

Task 10: Lost in Translation - The Sequel


Translate the following function from the extended language to the kernel language. fun {Max N M} if N==0 then M else if M==0 then N else 1+{Max N-1 M-1} end end end

Potrebbero piacerti anche