Introduction to Functional Programming – Lab 1 “Power” | TDA555 / DIT440, LP1 2018 |

Home | Schedule | Labs | Lectures | Exercises | Exam | About | FAQ | Fire | WaitList | Slack | TimeEdit | YouTube | Links |

Introduction to Functional Programming – Lab 1 “Power” | TDA555 / DIT440, LP1 2018 |

Home | Schedule | Labs | Lectures | Exercises | Exam | About | FAQ | Fire | WaitList | Slack | TimeEdit | YouTube | Links |

In this lab assignment, you will implement the well-known `power`

function in two different new ways. The power function takes two arguments *n* and *k* and computes *n*^{k}. Your implementation only has to work for non-negative *k*.

We have already seen one implementation of this function in the lecture:

You will implement two more ways in this lab assignment.

*Note:* Please make sure you follow the submission instructions when you write your code. Start by downloading a Haskell file Lab1.hs and add your solutions to your copy of this file.

First deadline (Monday, Sept. 10 at 12:00 2018): You need to have made a serious effort on completing the complete lab assignment.

Final deadline (Wednesday, Sept. 19 2018): You need to have gotten a pass on the lab.

In order to calculate `power n k`

, for a given `n`

and `k`

, how many computing “steps” are being used?

*Hint:*

`power n 0`

takes 1 step.

`power n 1`

takes 1 step, and then uses `power n 0`

.

`power n 2`

takes 1 step, and then uses `power n 1`

.

`power n 3`

takes 1 step, and then uses `power n 2`

.

…

Write your answer in the form of a function definition:

` stepsPower n k = ...`

Your function should not be recursive.

A different way of computing the power function is to use the standard Haskell function `product`

, which calculates the product (multiplication) of all elements in a list.

(You may not yet have learned about lists, but you can read about them here )

To calculate `power n k`

, first construct a list with `k`

elements, all being `n`

, and then use the function `product`

to multiply them all together.

Implement this idea as a Haskell function `power1`

.

*Hint:* You have to come up with a way of producing a list with `k`

elements, all being equal to `n`

. Use a list comprehension, or use the standard Haskell function `replicate`

. If you use `replicate`

, you might want to use the function `fromInteger`

too! Use Hoogle to find out more about standard functions.

A different approach to calculating the power function uses fewer computing steps.

We use the fact that, if `k`

is even, we can calculate *n*^{k} as follows:

*n*^{k} = (*n*^{2})^{k/2} (*k* is even)

In other words:

*n*^{k} = (*n* ⋅ *n*)^{k/2} (*k* is even)

So, instead of recursively using the case for *k* − 1, we use the (much smaller) case for *k*/2.

If *k* is not even, we simply go one step down in order to arrive at an even *k*, just as in the original definition of power:

*n*^{k} = *n* ⋅ (*n*^{k − 1}) (*k* is odd)

To sum up, to calculate `power n k`

- If
`k`

is even, we use (*n*⋅*n*)^{k/2} - If
`k`

is odd, we use*n*⋅ (*n*^{k − 1})

Implement this idea as a Haskell function `power2`

.

*Hints:*

- Do not forget to add a base case (what do you do when
*k*= 0?) - You need to find out when numbers are even or odd. Use the standard Haskell functions
`even`

and/or`odd`

. - To divide integer numbers, use the function
`div`

(and not the function`/`

, which is used to divide floating point and rational numbers).

We would like the three functions `power`

, `power1`

, and `power2`

to calculate the same thing. It is probably a good idea to test this!

- Come up with a number of test cases (inputs you will test your functions on).

Argue why you have chosen these test cases. (Think about for what inputs the functions are defined, and for what inputs the functions are not defined.)

Implement two functions: One function

`comparePower1`

(which given`n`

and`k`

, compares the result of the`power`

function with your`power1`

function), and a function`comparePower2`

(which does the same for`power`

and`power2`

). Your tests should give the result`True`

when the test succeeds, and`False`

if it runs without an error but does not give the same result in both cases. These two functions will make your tests easier to write!Write all your test cases as one or two Haskell functions that perform all tests. It is probably a good idea to use the functions

`comparePower1`

and`comparePower2`

here.

*Hint:* You can use a list comprehension to combine all possible cases you would like to test for `n`

and `k`

. Use the standard Haskell function `and`

to combine the results. If you are not familiar with list comprehensions, you do not have to use these.

This is an optional extra assignment.

Just for fun, measure and compare the speed of the different power implementations. You can do this by downloading the MeasureTime module to the same directory as the file for lab 1. Then add the following line at the top of the file for lab 1:

`import MeasureTime`

Now you can time the power implementations in the following way in the terminal:

```
*MeasureTime> measureTime2 power 10 100000
Start evaluation
Done after 2.9s
```

Write a Haskell function `table`

that takes `x`

and a maximum `n`

as an argument, and then generates a table in the following way:

```
*Main> table 2 10
n power power1 power2
0 1 1 1
1 2 2 2
2 4 4 4
3 8 8 8
4 16 16 16
5 32 32 32
6 64 64 64
7 128 128 128
8 256 256 256
9 512 512 512
10 1024 1024 1024
```

Exact details on how many spaces to insert where, how to line up the numbers, etc. are freely choosable for you.

*Hint:* Use `show`

to convert a number to a string, and use the functions `length`

and `replicate`

to calculate the right number of spaces between the strings. Use `unlines`

to convert a list of lines into one big text. Use `putStr`

to display the generated string on the screen. It would be cool to try to add a list of the speeds to the table. Go wild.

Use Hoogle to find out more information about these functions and others.

You must submit using the Fire system, preferably in groups of 3 (from Lab 2 onwards this will be strictly enforced by the fire system).

Before you submit your code, Clean It Up! Remember, submitting clean code is Really Important, and simply the polite thing to do. After you feel you are done, spend some time on cleaning your code; make it simpler, remove unneccessary things, etc. We will reject your solution if it is not clean. Clean code:

- 2018-08-30 Reformatted, minor changes (DS)