Welcome to CS 334 lab! In this first lab session, you will set up your development environment and get familiar with the tools we'll use throughout the course: the command line, a text editor, and the Lisp interpreter.
Log in to your lab machine. Open a terminal window (you can find
it in your applications menu, or press Ctrl-Alt-T on Linux).
You should see a prompt like:
$
This is your Unix shell. You'll type commands here throughout the semester.
If you are not comfortable with the Unix command line, work through this tutorial now:
Complete at least the first five sections. You should be comfortable
with navigating directories (cd, ls, pwd), creating and removing
files and directories (mkdir, rm, cp, mv), and viewing file
contents (cat, less).
Even if you already know Unix basics, skim through it quickly as a refresher.
Create a directory for this course and navigate into it:
$ mkdir cs334
$ cd cs334
You can verify where you are with:
$ pwd
/home/yourusername/cs334
We will use emacs as our text editor in this course. It runs in the terminal and is available on all the lab machines.
Why emacs instead of a GUI editor on your laptop? Throughout the
semester, you will often need to edit files on remote machines via
ssh. A terminal-based editor like emacs works everywhere --- on
the lab machines, over an ssh connection, and on your own computer.
Learning it now will pay off all semester (and beyond).
Here is what you need to know. In the table below, C- means
hold the Control key and M- means hold the Alt key (or press
Escape and release it before the next key). Skim the table, then do the
walkthrough below. Come back and consult the table until the commands
start to feel more natural.
Opening and saving files:
| Keys | Action |
|---|---|
emacs filename |
Open a file from the command line |
C-x C-f |
Open a file from inside emacs (it will prompt for the name) |
C-x C-s |
Save the current file |
C-x C-c |
Quit emacs (will ask to save unsaved changes) |
Moving around:
| Keys | Action |
|---|---|
| Arrow keys | Move the cursor |
C-a / C-e |
Jump to beginning / end of line |
C-v / M-v |
Page down / page up |
M-< / M-> |
Jump to beginning / end of file |
Editing:
| Keys | Action |
|---|---|
C-k |
Kill (cut) from cursor to end of line |
C-y |
Yank (paste) the last killed text |
C-_ |
Undo |
C-g |
Cancel the current command |
Searching:
| Keys | Action |
|---|---|
C-s |
Search forward (type your search text, press C-s again to find next match) |
C-r |
Search backward |
Buffers (when you have multiple files open):
| Keys | Action |
|---|---|
C-x b |
Switch to another open buffer (it will prompt for the name) |
C-x C-b |
List all open buffers |
Follow these steps now to practice. It should only take a few minutes.
Open a new file from the command line:
$ emacs practice.txt &
the & at the end tells the terminal to run emacs in the background so you
can continue entering shell commands while editing.
Don't use & after ssh-ing to a lab computer
If you ssh to a lab computer from your laptop, you can still run emacs,
but it runs inside the terminal window rather than in a separate window.
Just use a command like emacs practice.txt without the & in this case.
Type in a few lines of text. Anything is fine, for example:
The quick brown fox
jumps over the lazy dog
and runs into the forest
Save the file: press C-x C-s. You should see
"Wrote ...practice.txt" at the bottom of the screen.
Move around: use the arrow keys to move the cursor to
the middle of the second line. Now try C-a to jump to
the beginning of the line and C-e to jump to the end.
Press M-> to go to the end of the file and M-< to go
back to the top.
Kill and yank: move to the beginning of the second line
with C-a. Press C-k twice (once to kill the text, once
to kill the newline). The line is gone. Now move to the
end of the file with M-> and press C-y to paste it
there.
Undo: press C-/ a few times to undo what you just did.
The text should return to its original state.
Search: press C-s and type fox. Emacs highlights the
match as you type. Press C-s again to find the next match
(there is only one here). Press Enter to stop searching.
Open a second file without leaving emacs: press C-x C-f,
then type practice2.txt and press Enter. You are now
editing a new file. Type a line of text and save with
C-x C-s.
Switch buffers: press C-x b and press Enter to go back
to practice.txt. Press C-x b again, type practice2.txt,
and press Enter to switch to that file.
Quit: press C-x C-c. Emacs will ask about any unsaved
changes before exiting.
If something goes wrong at any point, press C-g to cancel
whatever emacs is doing, and try again. You can also run
emacs's built-in tutorial with C-h t for more practice.
Note: Emacs will feel unfamiliar at first, but you will get used to it quickly.
Let's make sure you can edit a file and run it from the command line.
Open a new file in emacs:
$ emacs hello.py &
Type the following Python program:
import sys
name = sys.argv[1]
print(f"Hello, {name}! Welcome to CS 334.")
Save the file with C-x C-s, then quit emacs with C-x C-c.
Now run it:
$ python3 hello.py Cow
Hello, Cow! Welcome to CS 334.
That's the basic workflow: edit a file in emacs, save it, switch to the terminal, and run it.
We will use clisp (Common Lisp) for the first few weeks. This section will walk you through using it for the first time.
Start the Lisp interpreter:
$ clisp
You should see a prompt like:
[1]>
This is the REPL: Lisp reads what you type, evaluates it, and prints the result. Try these expressions one at a time.
Numbers are values:
[1]> 42
42
[2]> 3.14
3.14
Arithmetic uses prefix notation -- the operator comes first, inside parentheses:
[3]> (+ 2 3)
5
[4]> (* 6 7)
42
[5]> (- 10 4)
6
[6]> (/ 15 3)
5
You can nest expressions:
[7]> (+ (* 3 4) (- 10 5))
17
[8]> (* (+ 1 2) (+ 3 4))
21
Comparisons return T (true) or NIL (false):
[9]> (> 5 3)
T
[10]> (< 5 3)
NIL
[11]> (= 7 7)
T
Conditionals with if:
[16]> (if (> 5 3) 'yes 'no)
YES
[17]> (if (< 5 3) 'yes 'no)
NO
Now quit the REPL by typing (quit) or pressing Control-D.
You can also put your Lisp code into a file and then run the
interpreter on that file. To run a program in the file
example.lisp, type
clisp < example.lisp
at the command line. The interpreter will read, evaluate, and print the result of each expression in the file, in order.
Open a new file:
$ emacs lab0.lisp &
Type the following:
; lab0.lisp -- My first Lisp file
; A function to compute the factorial of n.
(defun factorial (n)
(if (<= n 1)
1
(* n (factorial (- n 1)))))
; Test cases:
(factorial 5)
(factorial 10)
(factorial 0)
Save with C-x C-s and quit with C-x C-c.
Now run it:
$ clisp < lab0.lisp
FACTORIAL
120
3628800
1
Bye.
The interpreter evaluates the function definition (printing
FACTORIAL), then evaluates the three test expressions, and
then quits.
If your program contains an error, the Lisp interpreter will
print an error message and may wait for input. Just type
(quit) or press Control-D to exit.
sum-toNow add a function sum-to to your lab0.lisp file. sum-to
takes a positive integer n and returns the sum of all integers
from 1 to n.
(sum-to 1) ; => 1
(sum-to 5) ; => 15 (= 1+2+3+4+5)
(sum-to 100) ; => 5050
Think about the base case and the recursive case before you start writing code. Add test cases to your file to verify that your function works.
Submit your code to the GradeScope assignment named, for example, "Lab 1". You can submit in one of two ways:
Please do not change the names of the starter files. Also:
Autograding: Gradescope will run an autograder on your code that performs some simple tests. Be sure to look at the autograder output to verify your code works as expected. We will run more extensive tests on your code after the deadline.