• Tidak ada hasil yang ditemukan

Computer Science 3 - Functional Programming, 2013 1B

N/A
N/A
Protected

Academic year: 2025

Membagikan "Computer Science 3 - Functional Programming, 2013 1B"

Copied!
4
0
0

Teks penuh

(1)

0B

Computer Science 3 - Functional Programming, 2013

1BTutorial and Practical Topic=FP, PracName = 3:Simp

1. Binary Search Tree: Implement a sort based on building and flattening a binary search tree. Gather experimental data using the same pseudo random numbers that you did before. Use a spreadsheet, and plot the performance curve for pseudo random lists of lengths [100, 200 .. 1000]. On the same graph, generate columns and plot the function k* (n log n). (You'll have to guess / solve from your experiments a suitable constant value for k.) From your experiments, compute how many reductions you estimate it will need to sort 5000 numbers. Then run an actual experiment, and let us know what the expected value is, the actual measured value, and the percentage of variation.

2. Chapter 7.3 Exercises 4, 5.

3. Chapter 7.3 Ex 11: An AVL tree (or height-balanced tree) is a binary tree which is not necessarily perfectly in balance, but is "close" to balanced. More precisely, a binary tree is an AVL tree if, for every node, the height of the left and right subtrees differ by no more than one. Write a function to determine whether a given tree is AVL balanced.

The “simple” solution to this problem - traversing each sub-tree twice, once to find its height and once to ask if it is balanced, has very poor performance. Draw a full binary tree of 5 levels of nodes, and work through your code. Determine how many times each leaf node is visited. Rewrite your function so that it only performs one pass of each subtree, and returns both the height and whether the tree is balanced. Test the program and demonstrate or comment on the performance.

The Outcomes Based Education (OBE) Thingie

What are the key outcomes of your course? What should you be able to do by the hand-in date of this practical?

We want to toughen your ability to think more deeply and more abstractly about algorithms and solving problems. And to train you to exercise care, organization, precision, and exactness as you turn your concepts and deep thinking into programs.

You should be able to write programs to build and manipulate data structures other than simple lists.

In particular, in this practical we concentrate on binary tree algorithms, where one usually has to organize the recursive algorithm to process both sub-trees.

You must read and understand small programs written by others. Often (and in this case) these programs will be more complex than you are presently able to synthesize yourself. So there is a deliberate element of stretch – no pain, no gain. You will see new techniques, and must be able to demonstrate improving skills by being able to make modifications and add your own functionality.

You will be able to write programs to manipulate trees that represent algebraic expressions. The required manipulations test and build your ability to transfer existing skills (you already know from grade 10 how to simplify simple algebraic expressions) into programs.

(2)

4. Simplifying Algebraic Expressions: Some Haskell code has already been written to represent expressions as trees, and then to differentiate expressions with respect to some variable. Collect the files expr.hs and diffsimpskel.hs from the course web site. Expressions are represented as "trees"

with node values of this data type:

data Expr = Var Char -- one kind of leaf: one-char identifiers | Val Int -- another kind of leaf: Int constants | Add Expr Expr -- five kinds of binary nodes, for the | Sub Expr Expr -- binary operators + - * / ^

| Mul Expr Expr | Div Expr Expr | Pow Expr Expr

For example, the expressions

m

1

= 6 x + 4

and m2 =(3x(y+2))1+(x−2) are built by entering these expressions into Hugs: Code (admittedly ugly!) has been provided in

the skeleton to draw an expression tree. (Twist your head sideways!) m1 = Add (Mul (Val 6) (Var 'x')) (Val 4)

m2 = Add (Pow (Mul (Mul (Val 3) (Var 'x')) (Add (Var 'y') (Val 2))) (Val 1))

(Sub (Var 'x') (Val 2))

One can algebraically manipulate these expressions by manipulating the trees that represent them. We're going to differentiate the expressions, and subsequently simplify the results of the differentiation. (The reason why some math students fail to understand differentiation is probably because it is inherently recursive!) Here is a program to do some (but not all) of the most simple differentiation rules with respect to some variable. You are a lucky group: previously students had to write

this, but it has mostly been provided in the skeleton. READ, SWEAT, UNDERSTAND, AND STRETCH YOURSELF ON THIS CODE…

diff :: Expr -> Char -> Expr

diff (Val _) x = Val 0 diff (Var y) x | x == y = Val 1

| True = Val 0

diff (Add u v) x = Add (diff u x) (diff v x) diff (Sub u v) x = Sub (diff u x) (diff v x) diff (Mul u v) x = ??? –- complete this

diff (Div u v) x = Div (Sub (Mul (diff u x) v) (Mul u (diff v x))) (Pow v (Val 2))

diff (Pow u (Val n)) x

= Mul (Mul (Val n) (Pow u (Val (n-1)))) (diff u x)

-- more difficult cases will just give an error

> drawExpr m1 ,--- 4

-+

| ,--- x `--*

`--- 6

(3)

diff m1 'x' gives a new tree, which corresponds to the expression (0*x+6*1)+0 diff m2 'x' gives a bigger tree, corresponding to

(1*((3*x)*(y+2))^0)*((0*x+3*1)*(y+2)+(3*x)*(0+0))+(1-0)

As you can see, once you have completed the missing case above, diff does its job nicely, but the expressions are not simplified. This is where your next job begins. You must write an expression tree simplifier. Here is part of the simplifier already present in the skeleton, and a useful little function to make testing your code easier:

simplify :: Expr -> Expr

simplify e = e -- not written yet! Your job!!!

test e x = putStr ("Differentiating " ++ show e ++ " with respect to " ++ [x]

++ " gives\n " ++ show (simplify (diff (simplify e) x))) Complete the coding of the simplifier. Any expression which cannot be handled by your function should just be left unchanged. You should then be able to produce trees that correspond to expressions something like this:

test m1 'x' 6

test m2 'x' 3*(y+2)+1

Hints: the case-by-case decomposition of the problem so that it matches the data alternatives is particularly evident in diff. Make sure your simplifier does the same. Test your code for all cases supplied in the skeleton files. It is essential that you do some serious planning here, and think about your algorithm independently of the "coding": if you think you can see how to simplify a tree, try your algorithm by hand before you code it. Every year students mess up this exercise because they fail to think the few steps ahead before they start coding.

5. Study my use of algebraic types in the week 2 sample solution. Make sure you understand what I’ve done there. Then complete "Fixing the World", the parts scheduled for Week 3 as an extension to my week 2 sample solution. This need not be handed in this week – we will hold it over until next week, but we strongly recommend that you tackle it now.

To submit: Submit your Haskell files, and answers to questions 1-4, and any experimental data, to your demonstrator by Tuesday 05 March, 8pm.

PTO for the essay this week.

2B

(4)

Essay: This essay is due by Sunday 10 March, 12 noon, email it to [email protected]

Clarify the notion of deferred execution (also known as lazy evaluation, or deferred evaluation) by looking it up on the Internet and finding some good references, and also reading in the course notes section 12.1.

You'll need this idea to make sense of the video below…

Download and watch the video interview of Anders Hejlsberg at the course page: HU

http://www.cs.ru.ac.za/courses/csc302/funcprog/Resources/Anders_on_functional_bits_of_CSharp.zip or

http://www.cs.ru.ac.za/courses/csc302/funcprog/Resources/Anders_on_functional_bits_of_CSharp.wmv

Read at least the following two popular articles, and find at least one good one of your own:

http://www.ddj.com/architect/212201710 (Dr Dobbs Journal: It's Time to Get Good at Functional Programming)

http://www.joelonsoftware.com/items/2006/08/01.html (Joel on Software: Can Your Programming Language Do This?)

Find out about Google’s MapReduce. What is it? How is it used? Why is it important? What has it got to do with functional programming?

Get an overview of F#.

Now write an essay, 2 pages max, on how functional programming ideas are finding their way into mainstream languages.

This is optional, slightly long, but really worthwhile! Watch Anders Hejlsberg again, giving the keynote at the 2008 Java and Object Oriented conference (JA00 2008), talking about Where are programming languages going? You can download this from the course resources page (and then you have to find a player that plays it), or you can hit this link H

http://www.cs.ru.ac.za/courses/CSc302/funcprog\resources/JAOO_2008_keynote.html which should play it in your browser. (This is an hour long, about 373MB)

http://www.sdtimes.com/content/article.aspx?ArticleID=36103&page=1

Referensi

Dokumen terkait