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.

You are not expected to complete all tasks during a scheduled two hour lab.

All students have 24-hour access to the N112, N113 and N114 CSIT building 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 finished 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.

Pre-Lab Checklist

  • Know your ANU ID.
  • Register for a lab slot on MyTimetable.
  • No prior experience with Linux, command line, or programming is required for this lab.

Objectives

Essential Web Resources

COMP1100 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 Lectures, Labs, Assignments, Past Exams and other helpful resources.

MyTimetable

If you have not done so already, you enrol in (or change) labs through MyTimetable.

Usage: You can check your marks for lab participation, assignments, and exams through Wattle.

Ed

You should already have received an email with an invitation to join the Ed Discussions forum. The Ed forum 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 Ed is traceable back to you, although you can make yourself anonymous to fellow students.

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 Ed periodically 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 files, 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 have not yet logged in, go ahead and do so now by typing your ANU uid in the “Username” box, press return, and then enter your password. You will be brought to the Ubuntu Linux desktop.

If you are coming from Windows or MacOS, Linux may seem 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 file cabinet icon in top left of screen, then click on the Home Folder icon. Code you write, files 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 Microsoft’s Visual Studio Code.

  • A text editor. You can use a text editor to create text files, 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 VSCode.

In your own time later, take a look at Student Computing Environment. This is a manual for the student computing environment provided in the CS labs, and outlines the rules for using the computers.

Using the Terminal Emulator

Any time we refer to your ANU unique identifier (uid) in the following instructions we will write uXXXXXXX. Please use your own ANU uid instead.

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

uXXXXXXX@l124lt19:~$ pwd
/students/uXXXXXXX
uXXXXXXX@l124lt19:~$

The following table describes some of the commands that you may find useful during this course.

Useful Commands for Navigation

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 specified 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 files in the current directory, in column format.
ls directory List the files in the specified directory. e.g. ls comp1100/
ls -l List files one file per line. This also shows you additional info about the file.
ls -a List all files, including “hidden” files. Hidden files are those files that begin with a “.”, e.g. The .bash_history file in your home directory.
ls comp1100/labs/l* List all files whose names begin with the letter “l” in the comp1100/labs directory

Useful Commands for Files and Directories

Linux Command Command Description
touch Create a new file. e.g. touch file.txt
cat Display the contents of a text file on the screen. e.g.: cat file.txt would display the file we created in the previous section.
file Find out what kind of file it is. e.g., file /bin/cat tells us that it is a Linux executable file.
cp Copies a file from one location to another. e.g. cp file.txt comp1100 (copies the file.txt file to the comp1100 directory)
mv Moves a file to a new location, or renames it. e.g. mv file.txt comp1100 (copy the file to comp1100, and delete it from the original location)
rm Delete a file. e.g. rm comp1100/file.txt Be Careful: Using rm without being aware of the casualty is irreversible. Read here 1 and here 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

Some useful shortcuts for the command line

Shortcut Description
<TAB> completion If you type a partial command or filename that the shell recognizes, you can have it automatically completed for you if you press the TAB key. Try typing the first 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 <Shift><PgUp> and <Shift><PgDn> Scroll back and forward through your terminal

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 (refer to the tables above) to list all the folders in the directory comp1100.

Now you have a place to put files 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 confirm what you have done.

Creating a new project with VSCode

We have decided to use a lightweight 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. VSCode provides a nice interface to combine these two functions, and much more. Note that VSCode will be provided to you as a tool in exam environments, as will text editors and the Terminal.

Note: VSCode and VSCodium are basically the same thing. The lab computers will have VSCodium installed on them, but we will use the term “VSCode” to mean VSCode or VSCodium (whichever you are using on your machine). If you are curious, we would encourage you to look up the difference between VSCode and VSCodium, but for the purposes of this course you can think of these two as the same thing - it’s the IDE you will be writing Haskell with.

  1. In the Terminal, navigate to the comp1100/labs folder using cd (as described above).

  2. Create a new folder “Lab01” by typing mkdir Lab01.

  3. Find VSCode on your lab machine, and launch it. The Applications icon should be on the bottom-left of the screen, and you can search for “Code”.

  4. Click “File” in the toolbar, and select “Open Folder”.

    Open Folder

  5. Navigate to the comp1100/labs/Lab01 folder.

    Browse To Folder

  6. Click “OK” in the top right.

In this lab, you are not required to do much inside VSCode apart from the following exercises and some initial setup. We will teach you how to use some of the tools VSCode provides throughout the semester.

Getting started with Haskell

Configuring VSCode for Haskell

If this is your first time opening VSCode, you need to install a few plugins and configure VSCode by following the instructions below.

  1. Click “File” in the toolbar, and then select “Preferences” then “Extensions” to open the extensions window.

  2. Search for “haskell” and install the “Haskell Syntax Highlighting” extension by clicking the “Install” button.

    Install Extensions

If the extensions above do not appear in your VSCode list of extensions then you are likely using a different version.

  1. Check your version by clicking “Help”, and then select “About”.

If VSCode is not at least version 1.36.1, you will need to remove your current version and install the latest version.

Haskell as a calculator

Once you have opened and configured VSCode, you now will need to enable the Terminal tool, by clicking “Terminal” in the toolbar, then “New Terminal”. Now you will see a new sub-window in the bottom third of the screen. This is the terminal emulator within VSCode.

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.

uXXXXXXX@machinename:~/Documents/comp1100/labs/Lab01$ ghci
GHCi, version 9.2.5: http://www.haskell.org/ghc/  :? for help
ghci>

You will notice that the prompt (text at the beginning of bottom line) has changed to ghci>. This indicates we are ready to type in GHCi commands.

Please note: the following examples show some of GHCi’s capabilities. We don’t expect you to understand how they work at this point — don’t stress!

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):

\[2 \times 9\] \[8193 / 3\] \[1 / 0\] \[12^{92}\] \[(2^9)^{12 /5}\]

The answers are:

\[2 \times 9 = 18\] \[8193 / 3 = 2731.0\] \[1 / 0 = \mathit{Infinity}\] \[12^{92} = 19260813958465.............\] \[(2^9)^{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 the ^ operator, otherwise, for fractional numbers, use **.

Here are a few more expressions to for you try out:

6 < 4
compare 4 5
compare (Just 5) Nothing
Nothing
True && False
True || False -- The ‘|’ character is called a “pipe” (found near the <Enter> key).
(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”

To exit GHCi, simply type :quit or :q.

Google for Haskell

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 first 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 VSCode toolbar click “File” then “New File” to create a new file. Next press Ctrl + s to name the file. Type “Area.hs” and press “Enter”. Note that all file names in Haskell begin with a capitalised letter.

On the first line, write:

module Area where

VSCode will probably automatically indent the next line, press backspace to return the cursor to the far left of the file.

And now we are ready to begin writing functions.

First of all, here’s an example of what a Haskell function looks like:

-- add 1 to an integer              <- this a comment that tells the user what the function does.
addOne :: Integer -> Integer
addOne int = int + 1

The first 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.

  • :: 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 file.

If you’ve seen functions before in high school, you might have seen notation before like \(f : \mathbb{N} \to \mathbb{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 VSCode 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 file into GHCi. The -Wall flag shows all the warnings if there are any. If nothing has gone wrong, you should see the response below:

GHCi, version 9.2.5: http://www.haskell.org/ghc/  :? for help
[1 of 1] Compiling Area         ( Area.hs, interpreted )
Ok, modules loaded: Area.
ghci>

If you need to edit your code, you can reload the changes in GHCi by typing in :reload or :r.

Now, let’s 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:

-- calculate area of a square with only integer lengths
areaSquare :: Integer -> Integer
areaSquare len = undefined

Replace undefined with the equation for the area of a square. Sample output, once loaded into GHCi:

ghci> areaSquare 3
9
ghci> areaSquare 10
100

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 first 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:

ghci> yourFunctionName 3 5
15
ghci> yourFunctionName 10 12
120

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 floating point numbers during this course, Don’t use Float.

Floating point numbers can be thought of as a crude approximation to real numbers, as the computer only keeps track of a finite number of decimal places. This can cause numerical errors due to rounding, but for most purposes they’ve very adequate.

If you try to run the above functions with floating point 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 specifically 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 final submission—make sure that they stay in the meeting place, and do not find 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 first computer lab tasks for COMP1100 course at the ANU.

Installing Haskell

There is a list of instructions for Haskell Installation and Environment Setup for Ubuntu, MacOS and Windows here.

Feel free to post related questions on Ed.

Haskell in browser

While you wait for the packages to install, why not also try Haskell directly in the comfort of your web browser: https://tryhaskell.org/

Finally, please LOG OUT of your lab session before you leave. 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/

bars search times arrow-up