# Problem Sheet for Week 1
## The Jupyter Notebook
We will be using the Jupyter, a web-based interactive development
environment in order to have a standard platform for all the tasks in
this course. The School of Computer Science maintains its own
instances of the service which you may connect to
[here](https://jupyter.apps.okd.aws.cs.bham.ac.uk). Our first problem
set will be concerned with familiarizing yourselves with this
development environment.
*Note:* This link will be activated just before the first problem
session on Thursday 30 Sep.
## Important: Shutting Down
Time on the server is allocated with a quota. If you use up all the
time allotted to you, you will no longer be able to access the system.
For this reason, please remember to shut down your server instance
when you are finished with a session. You may do so by selecting “Hub
Control Panel” from the File menu and then clicking “Stop My Server”.
## The Terminal
Jupyter includes a Unix terminal system which can be used to create
files and directories, compile source modules and run programs. If
you are unfamiliar with the Unix terminal, this first exercise will
show you some of the most basic commands.
1. To open the terminal, select “Terminal” from the Jupyter launcher.
You should then see a command prompt with a blinking cursor.
2. The command `pwd` is for “print working directory”. Typing it and
pressing enter will show you your current location in the system.
3. You can create a new directory with the command `mkdir`. Try,
for example, `mkdir scratch` to create a new scratch directory.
4. You can change directories with the command `cd`. Try `cd scratch`
to change into the directory you just created.
5. There are many ways to create new files. A simple one is with the
`touch` command. Try `touch foo.txt` to create an empty text file.
6. You can list the files in the current directory with the `ls`
command. Try it now to see the file you create. For more information
about the files, you can try `ls -l` which will show you, for example, the creator, the size and the date and time of creation.
7. To include *hidden* files in the list (which are files beginning
with a “.”) you can use the command `ls -la`. Note that there are two
special hidden files (which are actually hidden *directories*) namely
`.` which means the current directory, and `..` which means the parent
directory. Try changing to these directories to see what happens.
We will meet more commands in some of the exercises below.
## Cloning the course repository
Git is one of the most popular available *version control systems*.
It is used to keep track of files as they are revised and changed
during the development process. Your module team uses this system to
update and revise the course materials.
1. Go to the [FP Learning Repository](https://git.cs.bham.ac.uk/mhe/fp-learning-2021-2022) on GitLab.
2. Click the blue “Clone” button and copy the HTTPS URL to your clipboard.
3. In Jupyter, open a terminal.
4. Type `git clone` and then paste the url from your clipboard with Ctrl-V. Press Enter. The whole
command should look like this:
“`
git clone https://git.cs.bham.ac.uk/mhe/fp-learning-2021-2022.git
“`
5. You should be prompted for your CS username and password.
6. When the cloning process has finished, you should see a new folder
named “fp-learning-2021-2022” in your home directory.
7. Explore the resulting folder with the Unix commands you have learned above.
## Running and Compiling the Game of Life
The source code for the Game of Life program discussed in this week’s lecture can be
found in the directory `fp-learning-2021-2022/LectureNotes/Sections`.
1. Let’s first create a copy of the file to work with. To copy a file, we use the `cp` command. (Note the use of the parent directory `..` in the `cp` command).
“`
cd /jupyter/work
mkdir life
cd life
cp ../fp-learning-2021-2022/LectureNotes/Sections/Life.hs .
“`
2. We can run the program directly with the following command
“`
runhaskell Life.hs
“`
3. Stop the program with `Ctrl-c`.
4. Alternatively, we can compile the source to an executable in order
to be able to run it directly from the system. This can be done
with ghc’s “make” option which will take care of compiling, linking,
as well as including any dependencies.
“`
ghc –make Life.hs
“`
5. We can run the resulting executable by entering
“`
./Life
“`
## Modifying the Game of Life
1. To view the source, open the file with the text editor.
2. By default, the program uses the “glider” grid defined at the top of the file. We
can see this in the line
“`
main :: IO ()
main = life glider
“`
3. Try switching to another one of the defined grids.
4. What character is being used to represent live cells on the screen?
Try changing it to a character of your choice.
5. Try adding a grid of your own. Some fun examples can be found on
the Wikipedia page for the [Game of Life](https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life).
## Making and Interpreting A module
1. Make a new file named `foo.hs` in a directory of your choice. You
may do so with the either the terminal or the file browser.
2. Add the following contents:
“`hs
double :: Int -> Int
double x = x + x
“`
2. Open a new terminal window.
3. Run `ghci` to start the Haskell interpreter.
4. Load the new file as follows:
“`
Prelude> :l foo.hs
[1 of 1] Compiling Main ( foo.hs, interpreted )
Ok, one module loaded.
“`
5. Run the function `double` with your favorite number:
“`
*Main> double 6
“`
## Inspecting Some Types in Ghci
Use `ghci` to find out the type of the following expressions:
1. `not (not (not False))`
2. `(True,False)` (see Section 3.4 of Programming in Haskell)
3. `[‘a’, ‘b’, ‘x’]` (see Section 3.3 of Programming in Haskell)
4. `[(3,4),(4,6)]`
4. `(++)` (What is strange about this type? See “polymorphic functions” discussed later.)
## Ill-typed Expressions
1. Write five ill-typed expressions in Haskell.
2. Check their types in `ghci` – what does `ghci` say?
3. What happens when you try to evaluate that expression?