Lab 0: Haskell Install Party
Today’s Goals
To create and turn in your assignments, you want four things on your computer:
- Haskell
- A terminal – to compile and run your code
- A good text editor – to write your code
- Git – to turn in assignments
The goal of today’s lab is to make sure you have these things, know where they are, know that they work, and have some idea of how to use them.
You may use the lab computers or your own computer to turn in assignments. Even if you plan to use your own computer, it’s a good idea to have some idea of how to use the lab. If something goes wrong on your own laptop, we can’t guarantee any help.
There is usually one assignment per week, due at 11:59:59pm on Friday, but this week there is nothing you need to turn in.
If your CNetID includes uppercase letters, to log in to the lab machines you must type your username in all lowercase.
Haskell
The lab computers should have Haskell pre-installed. On your own machine, you can install Haskell Platform. Despite what the lecture notes say, the minimal install should be fine.
To see if you have Haskell installed correctly, open up a Terminal and run
$ ghci
and try some basic commands:
Prelude> 2 + 2
4
Prelude> 8 ^ 10
1073741824
Note the $
above is just an indication that you’re on a command line. All you have to type is ghci
.
A Terminal
Compiling and running Haskell programs generally happens on the command line, so you’ll need to have a terminal application. The lab machines and Mac OS X have an app called “Terminal” already installed (try hitting ctrl-alt-t
on the lab machines). On Windows, if you don’t have a terminal you’ll get one for free when you install git below (it will be called “Git Bash”).
You will want to develop some comfort on the command line. I believe there is a primer workshop coming up that I recommend you attend if you haven’t worked with the command line. Here’s the bare essentials:
See where you are (“print working directory”):
$ pwd
List what’s in this folder:
$ ls
List all files (files that begin with .
are usually hidden), with “long” information:
$ ls -al
Learn about any command by looking at its man
page (manual page):
$ man ls
(Hit q
to quit.)
Enter a folder:
$ cd some_folder
Go up one folder:
$ cd ..
The “root” folder is /
, and your home directory is ~
.
Make a folder:
$ mkdir new_directory
Move (or rename!) a file or folder:
$ mv old_name new_name
Copy:
$ cp old_file new_file
Copy a folder (“recursive” copy):
$ cp -r old_folder new_folder
Log on to a remote machine:
$ ssh username@computername.cs.uchicago.edu
Each computer’s name is written on the side of the machine. Or, here’s a list of all CS machines. You can leave SSH with ctrl-d
.
Copy a file to another machine:
$ scp some_file username@computername.cs.uchicago.edu:~/some/path/on/other/machine
If you use a terminal text editor like emacs
or vim
, you can use ssh
to do your homework remotely.
If some app is running in a terminal and you want it to stop, hit ctrl-c
. If some app in the terminal is asking you for input and you don’t want to give it any, hit ctrl-d
(end of input). ctrl-d
is also a way to end your terminal session.
A Good Text Editor
Microsoft Word ain’t gonna cut it. You want a text editor that makes your code colorful and automatically indents every new line you type.
On the command line, the popular options are vim and emacs. Both these editors are pre-installed on the lab computers. Using a command-line editor takes some practice, but an experienced user can learn to rapidly perform complicated edits. The main disadvantage is you can’t use your mouse.
On my laptop I will be using a GUI text editor, Atom, which is free and cross-platform. If you choose Atom, be sure to at least install the language-haskell
package from within Atom. The lab machines have Sublime Text 2 installed, which is a respected GUI editor from the same tradition.
Git
Git (pronounced like “get”) is a version control system. Git lets you take snapshots of an entire folder and stores them as “commits”. You can share these snapshots with other people. It’s kind of like a fancy backup system for your code, but it also allows you to easily collaborate on the same project on different computers.
In our case, we will use git to submit assignments. Details are forthcoming, but you should make sure that your computer has git. There is nothing you need to turn in this week.
You can download git here. Mac OS X computers may already have git installed—try which git
on the command line to see if git exists already. If you see a path (e.g. /usr/bin/git
) then you have git and don’t need to install anything.
Hello World
Use the command line to make a cmsc_161_intro_programming
folder, and a lab0
folder inside that.
There are two ways to run Haskell files.
1. In GHCi
As discussed in the first lecture, you can write a few functions in a file and then load them into GHCi. This is good when you just want to try a few functions, and is probably how you want to approach most of the early class assignments.
In the lab0
folder, make a new file called MyMath.hs
. Write out the program:
distance rate time = rate * time
distance2 (rate,time) = rate * time
Make sure to save the file.
On the command line, make sure you are in the lab0
folder and then start ghci
by typing:
$ ghci
You should see Prelude>
. To load your functions, use :l
Prelude> :l MyMath
You should see:
Prelude> :l MyMath
[1 of 1] Compiling Main ( MyMath.hs, interpreted )
Ok, modules loaded: Main.
*Main>
You can now use the distance functions.
*Main> distance 10 20
200
*Main> distance2 (30, 40)
1200
If you change your functions in MyMath.hs
and save the file, to update the functions in GHCi you can just reload with :r
.
Prelude> :r
To leave GHCi, hit ctrl-d
(or ctrl-c
).
2. Compiling an Executable
In the lab0
folder, make a file called “Hello.hs”. Write out the program:
main =
putStrLn "Hello, world!"
Try compiling the file with:
$ ghc Hello.hs
GHC should produce an executable in the same folder called Hello
.
Run it:
$ ./Hello
You should see, “Hello, world!”
To show the output of a computation, you’ll need to do a bit of work. Try changing Hello.hs
and recompiling:
square x = x * x
main =
putStrLn (square 4)
What error do you get?
Here’s how you fix it.
main =
putStrLn (show (square 4))
Play around until you are comfortable.
Verify Git Installation
To make sure you have git installed, run this command on the command line:
$ which git
You should see a path, like:
/usr/local/bin/git
If you do, you’re good to go. We’ll discuss how to use git next week.
When you’re comfortable with your setup you’re free to leave. See you next week!