COMP1100/1130 [2020 S1]:
PROGRAMMING AS PROBLEM SOLVING Research School of Computer Science
Menu
menu
» Labs » Week 1: ANU environment, Linux, Haskell
Welcome to the Week 1 lab for Programming as Problem Solving! The aim of this lab is to help you get started in the lab computing environment and introduce tools you will use throughout the course. If you have questions you can ask your tutor for help. If your tutor is busy with another student feel free to work with one of your lab mates to Xgure things out.
You are not expected to complete all the programming tasks during a scheduled two hour lab. All students have 24-hour access to the N112, N113 and N114 CSIT labs using your student ID card. You can work in these labs whenever there is no scheduled activity : usually after 7pm on weekdays, on weekends, and even on public holidays. If there is an active tutorial running in the lab, you may only use the lab if the tutor allows it.
Important: Whenever you are Xnished working with a lab computer, you must log out. This closes your session so that the next user cannot access your account, and leaves the machine in a clean state for the next user. Do not shut down, restart, or physically turn off the computer.
Table of Contents
Pre-Lab Checklist Objectives
Essential web resources
Linux Introduction
Bash Introduction and Useful Commands VSCodium – Creating a new project Haskell – Getting Started
Academic Integrity Installing and trying Haskell Useful References
Pre-Lab Checklist
Know your ANU ID.
Register for a lab slot on StReaMS at http://cs.anu.edu.au/streams.
No prior experience with Linux, command line, or programming is required for this lab.
Objectives
Essential web resources
Linux Introduction
Bash Introduction and Useful Commands VSCodium – Creating a new project Haskell – Getting Started
Essential Web Resources COMP1100/1130 Course website
The primary resource for this course is the website you are reading right now
at https://cs.anu.edu.au/courses/comp1100. The site will contain the Course Outline, Lectures, Labs, Assignments, Past Exams and other helpful resources.
StReaMS
If you have not done so already, you enrol in (or change) labs through StReaMS at http://cs.anu.edu.au/streams.
Usage: You can check your marks for lab participation, assignments, and exams through this service.
Piazza
You should already have received an email with an invitation to join the Piazza forum at https://piazza.com/. Piazza helps us maximise communication and draw upon a large number of people who can help. This is a public space so please be polite. Remember that everything you post on Piazza is traceable back to you, although you can make yourself anonymous to fellow students. You can also get mobile apps so you can access Piazza wherever you go!
Usage: You can post questions or ideas and discuss them with other students on the course and course instructors, who will monitor this forum regularly and respond accordingly.
Make sure to check Piazza periodically (by default you will receive a digest of all new posts sent directly to you as an email) as the information on here will be invaluable through the semester.
GitLab
GitLab is an online Git repository manager with a wiki, issue tracking, continuous integration, and continuous deployment. Git is the most popular modern version control system, used to manage large and small software projects. The Web pages you are reading right now are stored, versioned, tested, and deployed using GitLab!
Usage: You will be using GitLab to access your lab and assignment source Xles, to track your progress on assessments, and to submit work and receive feedback from your tutors.
Software
In this course we use a suite of software tools that we think are suitable and valuable for you to learn and use. You will also use these tools in settings beyond this course.
The Linux Operating System
The computers in the computer science labs are running a distribution of the Linux operating system called Ubuntu. There are advantages in using Linux over Windows for certain tasks, and Linux is heavily used in cloud and server systems.
If you are coming from Windows or MacOS, Linux will be a little unfamiliar to you. Don’t worry — it’s not too different. You’ll need to know a few things to get started though:
Your home directory. Access this by clicking on the Xle cabinet icon in top left of screen, then click on the Home Folder icon. Code you write, Xles you need to use, etc., should go here. You can also have a look at the ANU HomeDrive that you should have access to as an ANU student.
A Web browser The basic browsers available to you on the lab machines are Google Chrome and Firefox — don’t worry too much about which one to use. You can access them through the Applications menu at the top of your screen.
A terminal. This is one of the main ways to interact with your computer, and it uses typed commands exclusively. Try pressing Ctrl + Alt + t (all at the same time!) to open a terminal, and get started with the next exercise.
Integrated Development Environment (IDE) An IDE combines a text editor (software for writing programs) with a terminal with other useful features to integrate the two. In the course, we will use VSCodium with the Haskell Syntax Highlighting plugin, as well as GHCID.
A text editor. You can use a text editor to create text Xles, including your programs. It is similar to programs such as Notepad++ on Windows
or TextEdit on MacOS.
Note: If you are already familiar with another text editor and would prefer to use that, you may. Disclaimer: In this course, we will only assist you in using VSCodium.
In your own time, take a look at Student Computing Environment. This is an manual for the student computing environment provided in the CSIT laboratories, and outlines the rules for using the computers.
Using the Terminal Emulator
Anytime we refer to your ANU ID, we will use the term uXXXXXXX. Please use your own ANU ID in these instances.
Press Ctrl + Alt + t OR click on the Terminal from Applications. This launches the terminal emulator.
You should see something like the following
uXXXXXXX@l124lt19:~$
This has the format username@machinename where
username refers to your ANU ID as mentioned above.
machinename refers to the lab machine that you are currently logged into.
Let’s try out some commands.
uXXXXXXX@l124lt19:~$ pwd You should see the following
The following table describes some of the commands that you may Xnd to be useful during this course.
Useful Commands for Navigation
Useful Commands for Files and Directories
Some useful shortcuts for the command line
In this activity, we have only shown you only a few shell commands that will be useful for the course. If you want to learn more, please feel free to search online for tutorials. The GNU Bash Manual can be found here.
A handy cheat sheet for many common commands can be found here. Activity 1
Launch the Terminal emulator. You should be in the Home directory; otherwise use the command to reach the Home directory. (You can check that you are at the home directory by typing pwd, and the result should be /students/uXXXXXXX.) Make a new directory named comp1100
Change the working directory to comp1100
Create new directories inside comp1100 named lectures, labs and assignments. Use the appropriate command to list all the folders in the directory comp1100.
Now you have a place to put Xles related to the various activities of the course (labs, assignments, lectures). Feel free to create additional sub-directories as you please. Once again, use the ls command to conXrm what you have done.
Creating a new project with VSCodium
We have decided to use an IDE (Interactive Development Environment) to simplify your progression into programming. Haskell is generally written with a text editor (such as TextMate, emacs, Sublime Text, Notepad++, etc.), and debugged and run with the Terminal. VSCodium provides a nice interface to combine these two functions, and much more. Note that VSCodium will be provided to you as a tool in exam environments, as will text editors and the Terminal.
1. IntheTerminal,navigatetothecomp1100/labsfolderusingcd(asdescribed above).
2. Create a new folder “Lab01” by typing mkdir Lab01.
3. Find VSCodium on your lab machine, and launch it. The Applications icon should
be on the bottom-left of the screen, and you can search for “VSCodium”.
4. Click “File” in the toolbar, and select “Open Folder”.
5. Navigate to the comp1100/labs/Lab01 folder.
6. Click “OK” in the top right.
In this lab, you are not required to do much inside VSCodium apart from the following exercises and some initial setup. We will teach you how to use some of the tools VSCodium provides throughout the semester.
Getting started with Haskell ConXguring VSCodium for Haskell
If this is your Xrst time opening VSCodium, you need to install a few plugins and conXgure VSCodium by following the instructions below.
1. Click “File” in the toolbar, and then select “Preferences” then “Extensions” to open the extensions window.
2. SearchforhaskellandinstalltheHaskellSyntaxHighlightingandhaskell- ghcid extensions by clicking the green Install buttons.
3. In the bottom left, click on the cog icon, then click settings. If the dropdown box at the top underneath “Files: Auto Save” says “off”, change it to “afterDelay”.
Haskell as a calculator
Once you have opened and conXgured VSCodium, you now will need to enable the Terminal tool, by clicking “Terminal” in the toolbar, then “New Terminal”. Now you will see a new subwindow in the bottom third of the screen. This is the terminal emulator within VSCodium.
GHCi
GHCi (Glasgow Haskell Compiler Interactive) is GHC’s interactive environment, in which Haskell expressions can be interactively evaluated and programs can be interpreted.
In the terminal emulator, type in ghci, and hit enter.
You will notice that the prompt (text at the beginning of bottom line) has changed to Prelude. This indicates we are ready to type in GHCi commands.
Now it’s time to type something in! What happens if you type in 1 + 1, and hit enter? Does it give you 2? Try to evaluate the following mathematical expressions (In Haskell * stands for “times”, / for division, ^ for power, or.. does it? If you are lost, feel free to use Google search):
uXXXXXXX@l124lt19:~$ pwd /students/uXXXXXXX uXXXXXXX@l124lt19:~$
Linux Command
Command Description
pwd
Print Working Directory. Shows the current location in the directory (folder) tree.
cd
Change Directory. When typed all by itself, it returns you to your home directory.
cd directory
Change into the speciXed directory name. e.g. cd comp1100
cd ~
~ is an alias for home directory. It can be used as a shortcut to your home, or other directories relative to your home.
cd ..
Move up one directory. If you are in students/uXXXXXXX/Desktop and you type cd .., you will end up in students/uXXXXXXX/.
cd –
Return to previous directory.
ls
List all Xles in the current directory, in column format.
ls directory
List the Xles in the speciXed directory. e.g. ls comp1100/
ls -l
List Xles one Xle per line. This also shows you additional info about the Xle.
ls -a
List all Xles, including “hidden” Xles. Hidden Xles are those Xles that begin with a “.”, e.g. The .bash_history Xle in your home directory.
ls comp1100/labs/l*
List all Xles whose names begin with the letter “l” in the comp1100/labs directory
Linux Command
Command Description
touch
Create a new Xle. e.g. touch file.txt
cat
Display the contents of a text Xle on the screen. e.g.: cat file.txt would display the Xle we created in the previous section.
file
Find out what kind of Xle it is. e.g., file /bin/cat tells us that it is a Linux executable Xle.
cp
Copies a Xle from one location to another. e.g. cp file.txt comp1100 (copies the Xle.txt Xle to the comp1100 directory)
mv
Moves a Xle to a new location, or renames it. e.g. mv file.txt comp1100 (copy the Xle to comp1100, and delete it from the original location)
rm
Delete a Xle. e.g. rm comp1100/file.txt Be Careful: Using rm without being aware of the casualty is irreversible. Read here [1] (https://docs.oracle.com/cd/E19253-01/806-7612/Xles-8/index.html) 2
mkdir
Make Directory. e.g. mkdir comp1100 creates a new directory named comp1100
rmdir
Remove Directory. e.g. rmdir comp1100 removes the existing directory named comp1100
Shortcut
Description
completion
If you type a partial command or Xlename that the shell recognizes, you can have it automatically completed for you if you press the TAB key. Try typing the Xrst few characters of your favourite Linux command, then hit TAB a couple of times to see what happens.
history
command
Show your complete command history.
Up/Down Arrow Keys
Scroll through your most recent commands. You can scroll back to an old command, hit ENTER, and execute the command without having to re-type it.
Scrolling the screen with
Scroll back and forward through your terminal
uXXXXXXX@machinename:~/Documents/comp1100/labs/Lab01$ ghci GHCi, version 8.6.5: http://www.haskell.org/ghc/ 😕 for help Prelude>
Please note: the following examples show some of what GHCi is capable of. We don’t expect you to understand how they work at this point – don’t stress!
The answers are:
2×9 8193/3
1/0
1292 (29 )12/5
2 × 9 = 18 8193/3 = 2731.0
1/0 = Infinity
1292 = 19260813958465………….
(29)12/5 = 3178688.0288904128
Did you get the correct answers? If so, congratulations! If not, why not? Think about it for a moment, and ask your tutor if you are unsure.
Hint: For power of integers (whole numbers), use ^ operation, otherwise, for fractional numbers, use **
Here are a few more expressions to for you try out:
To exit GHCi, simply type :quit or :q. Google for Haskell
6<4
compare
compare
Nothing
True &&
True ||
(1 + 1 == 2) || (1 + 1 == 3) 2^5
lcm 4 14
sqrt 2.0
sqrt 4*100 -- Think carefully about the output you get from this one. sin (pi/2.0)
[1 .. 10]
sum [1 .. 100]
product [1, 3 .. 10]
“Out” ++ “shined”
length “Room a thousand years wide”
words “Fell on black days”
4 5
(Just 5) Nothing
False
False -- The ‘|’ character is called a “pipe” (found near the
hat
If you need to edit your code, you can reload the changes in GHCi by typing in :reload or :r.
Now, write a function, areaSquare, that calculates the area of a square. For now, it should take in an Integer as an argument, and output another Integer as the result. Afterwards, test if it actually does its job!
Here is how part of the function can look like:
Replace undefined with the equation for the area of a square. Sample output, once loaded into GHCi:
A quick sidenote: areaSquare (-3) results in 9. But there is no such square with negative lengths? Keep this in mind. We will deal with these issues in Lab 03 and onwards.
Exercise 2
Now you’ve written and executed your Xrst Haskell function, it’s time to move onwards. What if I want to calculate the area of a rectangle? Now I need two inputs (the width and the height!).
What do you think is a useful function name in this case?
What do you think should be the type signature of this function?
What do you think should be on the second line of this function?
What could you include as a comment for the function that is useful to the reader?
Sample output:
Do some more tests to validate your function’s correctness.
Exercise 3
Not all rectangle and squares have nice Integers as the lengths of their sides. In fact, most of the time they will be fractional numbers. We call these numbers Floating Numbers. They are denoted in Haskell by Float or Double. We will use Double for toating point numbers during this course, Don’t use Floats.
If you try to run the above functions with toating numbers as their arguments, you will notice that error messages will come up instead of the answers. Read these error messages – what could they mean? Modify and test your functions for area of square and rectangle so that they will still work when the number given is not an Integer.
Again …
What do you think should be the type signature of these functions?
What do you think should be on the second line of these functions?
What could you include as a comment for the functions that is useful to the reader?
Do some tests to validate your function’s correctness. Check with your tutor or classmate if you wish.
Make sure you complete these three exercises before the next lab.
Academic Integrity
PLEASE READ THIS.
According to the Merriam-Webster online dictionary, to plagiarize is:
to steal and pass off (the ideas or words of another) as one’s own to use (another’s production) without crediting the source to commit literary theft to present as new and original an idea or product derived from an existing source
At the Australian National University, we take great care to uphold academic integrity, treating cases of plagiarism extremely seriously, and dealing with them according to university policies which you can read at ANU’s Academic integrity site. Every ANU student is expect to be familiar with these policies, which apply the principle that all work that you turn in for assessment should be original and authentic.
How those policies relate concretely to your courses in computer science, and speciXcally to this course:
All work submitted for assessment is by default assumed to be entirely your own. Besides obviously forbidding direct copying, this also implies that no part of your submission is inspired by, based on, or re-formulated work from another person. Reformulating the work of somebody else actually makes matters worse, as it adds clear intent to deceive to the already existing plagiarism.
Nevertheless, you sometimes will be inspired by another work or by a classmate. If that is the case, your obligation is to clearly indicate it in your submitted work. Obviously you will not receive the highest mark if all of your assessed work is derived from someone else’s work. By indicating ownership and sources clearly you will stay on the right side of the policy. Failure to indicate all of your inspirations, sources, or collaboration partners will be regarded as intention to deceive.
This course does allow for collaboration if properly indicated in the submission and some additional rules are followed:
The writing of code and documentation that you intend to submit must always be done entirely by you.
You may exchange ideas on scrap paper, boards, and the like, but you may not work together on shared documents that are intended for submission. You may not use any of the scrap documents for your Xnal submission—make sure that they stay in the meeting place, and do not Xnd their way into your pocket.
You may not collaborate or communicate with other students about your submission right before you start writing your submission documents. After you discuss ideas with anybody else, you need to wait at least six hours before you start writing your own submission. This implies that you need to plan your work and discuss your concepts (if you choose to do so) well ahead of any deadline.
Detection of plagiarism is very likely (even though it might not always appear so from an individual perspective), and the consequences can be severe, including academic discipline, mark and grade penalties, and suspension or expulsion from the university. Please help us to make this a clean course which focuses entirely on the learning process and not on policing.
Please ask questions of your tutors now to clarify your understanding about plagiarism and this course. It is important that you are fully aware of the issue and your obligations for a successful and productive semester. We don’t want to discourage collaboration to solve a problem, but it’s important you understand what constitutes appropriate collaboration, and what is plagiarism.
All good things come to an end
Congratulations, you have completed your Xrst computer lab tasks for COMP1100/1130 course at the ANU.
Installing Haskell
If you cannot wait to get started with Haskell, we have compiled a list of instructions for Haskell Installation and Environment Setup for Ubuntu, MacOS and Windows here: https://piazza.com/class/k6olab3u83z10i?cid=11
Haskell in browser
While you wait for the packages to install, why not try Haskell in the comfort of your web browser: https://tryhaskell.org/
Finally, please LOG OUT of your session before you leave the lab. You can do this by clicking the power button in the top right, then click on your name and then select “Log Out”.
References
[1] Bash Manual, https://www.gnu.org/software/bash/manual/bash.pdf
[2] Prelude, http://hackage.haskell.org/package/base-4.12.0.0/docs/Prelude.html [3] Haskell in 10 minutes, https://wiki.haskell.org/Learn_Haskell_in_10_minutes [4] Hoogle, https://www.haskell.org/hoogle/
[5] Hackage, https://hackage.haskell.org/
If you are not sure what any of these functions do, why not Hoogle it? Hoogle is a Haskell API search engine, which allows you to search many standard Haskell libraries by either function name, or by approximate type signature. Try it out yourself: search for the function words.
You may not understand some of the type signatures, but don’t worry, we will understand them as the course progresses.
Your Xrst Haskell function
Functions can be used to implement more complicated things than simple calculations. Let us have try to write a few functions that calculate the area of different shapes.
Exercise 1
In the VSCodium toolbar click “File” then “New File” to create a new Xle. Next type Ctrl + s to name the Xle. Type “Area.hs” and press Enter. Note that all Xle names in Haskell begin with a capitalised letter.
On the Xrst line, write:
module Area where
And now we are ready to begin writing functions.
First of all, here’s an example of what a Haskell function looks like:
The Xrst line of the function is called the type signature of the function.
addOne is the function name, which always start with a uncapitalised letter.
Function names in Haskell always start with uncapitalised letter. We are going to use camelCase names throughout the course, as opposed to snake_case.
VSCodium will probably automatically indent the next line, press backspace to return the cursor to the far left of the Xle.
— add 1 to an integer <- this a comment that tells the user w
addOne :: Integer -> Integer addOne int = int + 1
:: can be read as has type.
Integer -> Integer means it takes an Integer as input, and returns another
Integer.
On the second line, the function name is repeated, and x is the name of the input (the Integer). After the “=” is what the functions will return. Copy and paste the above code into your Xle.
If you’ve seen functions before in high school, you might have seen notation before like f : N → N, f(x) = x + 1 which is essentially the same thing as the function above.
In order to run the function to test its functionality, you will need to use the Terminal. If you don’t already have the Terminal window, go back to the “Haskell as calculator” section and follow the instructions to add Terminal to your VSCodium interface.
Go into your Terminal, make sure you are in bash (not in ghci) and type in ghci -Wall Area.hs, and hit enter. This loads the Area.hs Xle into GHCi. The -Wall tag shows all the warnings if there are any. If nothing has gone wrong, you should see the response below:
GHCi, version 8.6.5: http://www.haskell.org/ghc/ 😕 for help [1 of 1] Compiling Area ( Area.hs, interpreted )
Ok, modules loaded: Area.
*Area>
— calculate area of a square with only integer lengths
areaSquare :: Integer -> Integer areaSquare len = undefined
*Area> areaSquare 3 9
*Area> areaSquare 10 100
*Area> yourFunctionName 3 5 15
*Area> yourFunctionName 10 12 120
Floating point numbers can be thought of as a crude approximation to real numbers, as the computer only keeps track of a Xnite number of decimal places. This can cause numerical errors due to rounding, but for most purposes they’ve very adequate.
Please install Haskell in your personal computer as soon as possible, preferably by the beginning of week 2. Feel free to post any questions on Piazza. Keep an eye out for the CSSA InstallFest, where you can get personal assistance in the installation process.
Updated: 06 Jun 2020
Responsible Owcer: Director, RSCS
Page Contact:
Course Convenor
Contact ANU
Copyright
Disclaimer
Privacy
Freedom of Information
+61 2 6125 5111
The Australian National University, Canberra CRICOS Provider : 00120C ABN : 52 234 063 906
t