Lab Exercises Week 2
Bring a printout of this page with you to your lab
The exercises this week are to get you used to using the labs. Your lab follows your tutorial. Your tutor will take you to the lab and will look after you in the labs each week.
You may find these first laboratory exercises strange or unfamiliar. Do not worry. This is common at first. Just go slowly and carefully and don't be afriad to experiment or redo things which don't quite work the first time.
If something unexpected or puzzling happens or you are not sure what to do, ask another student or your tutor. It's fastest to ask another student first if your tutor is already talking to someone, and if other student(s) can't help ask your tutor when they are free again.
Enable your account
When you sit down you will see a window asking for your login and a password.
Before you can log in for the first time you will need to obtain an account name and a password. To do this, type "sirius" as the account name and press the Enter key. For the password, simply press the Enter key. As instructed, put in your student number (just your student number) and other details. It will then ask you if you need directions - say no. All you need to do is click "Enable Account" in the top right-hand corner, to create a password and to enable your account. Remember the username and password carefully! The username is shown on the class roll, the password you'll need to remember. Write it down for now if you must - but once you have memorized it eat the piece of paper for security.
Once you have enabled your account proceed with the rest of the lab.
If you ever forget your username and/or password - go to the CSE help desk for help.
Enabling your account can take take 10 minutes or more. Do it before coming to the lab if you possibly can to give yourself as much time as possible to do more useful things in the lab.
Logging In
When you sit down you will see a window asking for your login and a password. At the login prompt type in the login name that you were given when you enabled your account: it should be a sequence of a few letters followed by a few numbers.
For example, a student named Jane Smith might have the following login:
jsmi747
This is your login name for the CSE network computers. Next, press Enter and type your account password into the box to the right of the password prompt, which is the same one you entered into Sirius when you enabled your account. You will notice that unlike your login, your password does not show up on the computer monitor: this is normal, as your password should not be seen by others around you. Now hit enter once again, and in a few moments you will be logged into your account for the first time.
Entering Commands
You should now see two large xterm windows in front of you. These are windows into which you can type UNIX commands to be run by the computer. You'll learn more about that next week. This week just type ghci in one of the xterm windows to start the Haskell interpreter.
This week we'll use the menus controlled by the mouse buttons. Notice that holding down the various mouse buttons whilst holding shift ctrl or alt brings up various menus.
Start a web browser "mozilla" or "firefox". Find this page on the web. Stop using the printout and work directly from the web page now. (It may be more up-to-date).
You can come back to the labs in your own time later and experiment with commands and the mouse menus. Bring the unix primer with you, it will tell you all you need to know.
Using ghci as a calculator
Ex 1.
Start ghci (type ghci in an xterm) and see if you can get ghci to calculate the answers to the following:
-
Multiply 12345 by 67890
-
Add 2468 and 13579
-
Multiply the first 5 numbers (eg 1 times 2 times 3 times 4 times 5)
-
Multiply the first 20 numbers
-
Add the odd numbers from 1 to 49
-
What is the remainder when 456 is divided by 10?
-
What is the remainder when 365 is divided by 7?
-
evaluate
when a is 11, b is 20, c is 3.
If you have any troubles ask your neighbour(s) for help. If you all have troubles ask the tutor who will be walking around the lab checking that everyone is OK.
When you are finished leave the lines you typed and the answers visible in the xterm to show to your tutor when you have finished the lab. Do the remainder of the lab using another xterm.
Simple Haskell Scripts
Use the mouse menus to start the nedit editor. nedit is an editor like notepad or ConText.
In the remainder of this week's lab exercises you will construct a Haskell module Lab02 containing several functions. Type your function definitions into the "nedit" window, and save the file as Lab02.hs.
Haskell ignores lines starting with --. Such lines are called comments. Put a header comment at the start of your module indicating who wrote the module, when it was written, and for what purpose it was written. Place a comment before each [non trivial] function you write explaining what is does. Make sure that your comments and your functions are formatted so they are easy to read. Each week your tutor will only award the lab marks for modules written with acceptable style. They will tell you each week what is considered to be good style.
After you have written the first function you will want to test it using ghci. At that time start ghci using the command
-
ghci Lab02
If you change your module in nedit you will need to to save it again, and then tell ghci to reload the new module. You can do this by using ghci's reload :r command.
If you type
-
:t blah
at the ghci prompt it will display the type signature of function blah
Below are descriptions of the the Haskell functions you are to write in the Lab02 module. Test each one as you complete it.
2. Imperial to Metric
You wish to convert heights in feet and inches to metres. Fortunately you remember that there are exactly 25.4 millimetres in an inch and 12 inches in a foot.
Write a function imperial2m to do this conversion. Here is how imperial2m is to behave:
Lab02> :t imperial2m imperial2m :: Float -> Float -> Float Lab02> imperial2m 0 1 0.0254 Lab02> imperial2m 6 2 1.8796 Lab02> imperial2m 5 8 1.7272
3. Leap Years
Hint: do the 'isEven' tutorial exercise before attempting this question
A leap year is a leap year if it is divisible by 4, unless it is also divisible by 100, except that years divisible by 400 are leap years.
Thus 2004, 2016 and 2000 are leap years but 2100, 2001 and 2003 are not.
Write a function leapYear:: Int->Bool that returns True if and only if its input is a leap year.
Lab02> :t leapYear leapYear :: Int -> Bool Lab02> leapYear 2100 False Lab02> leapYear 2001 False Lab02> leapYear 2102 False Lab02> leapYear 1996 True Lab02> leapYear 2003 False Lab02> leapYear 2004 True Lab02> leapYear 2000 True
When you have completed the above exercises show them to your tutor for this week's core mark.
Advanced Exercises
The advanced exercises may be marked at the start of the week 3 lab if you do not complete them during the lab time this week. Note: In future weeks exercises must be completed by the end of the lab time.
3b. Leap Years Continued
Actually even with the 400 year adjustment we still slowly gain time. A full day every 4000 years or so. Adjust your leap year function to make years divisble by 4000 non-leap years.
Lab02> :t leapYear leapYear :: Int -> Bool Lab02> leapYear 8000 False Lab02> leapYear 1900 False Lab02> leapYear 1901 False Lab02> leapYear 1902 False Lab02> leapYear 1992 True Lab02> leapYear 1993 False Lab02> leapYear 1996 True Lab02> leapYear 2000 True
Q4. bradCooper
Very big numbers which are too big for Int can be represented by type Integer. A number of type Integer can be as big as you like.
Under mandatory sentencing in the NT a young aboriginal man was sentenced to 365 days in jail for stealing $23 dollars worth of food.
Write a function alanBond :: Integer -> Integer to compute the number of days jail a person would get if sentencing was proportional to the value of the crime. Use the above scale of $23 = 365 days.
Lab02> alanBond 23 365 Lab02> alanBond 1200000000 19043478260
When you have completed the above exercises show them to your tutor for this week's second lab mark.
Brownie Points
This is where we'll normally put any particularly challenging exercises, or exercises which are interesting but irrelevent to the final exam. If you solve them or do good work we'll give you a brownie point. Brownie points are not worth anything. Don't worry about brownie points for the first few weeks if you are new to computing. Just concentrate on understanding and getting the core exercises working. One brownie point this week for the following exercise.
Q5. 1
in Haskell 4^3 computes 4 to the power of 3 (ie 4 cubed).
Try to calculate (35^2003) `mod` 2004. Try (30^2) `mod` 3. Try (31^2) `mod` 3.
Write a function to compute a^(b-1) `mod` b, and try it out for various values of a and b.
