Dana Vrajitoru
C311 Programming Languages

C311 Lab 1

Date: Wednesday, January 11, 2023.
Due Date: Wednesday, January 18, 2023.

The lab should be uploaded to Homework 1 under Assignments on Canvas for 5 points of Homework 1.

Introduction

In this lab you will familiarize yourself with the interface of Emacs Lisp or elisp.

Simple rules for understanding the syntax used in this lab:

Emacs Notations

Minibuffer - The small window (1 line, usually) at the bottom of the Emacs window, usually containing a single line. It is used by Emacs to communicate with the user.

Status bar - The gray line above the minibuffer. It displays information about the buffer above.

The image below shows an example of an Emacs window with the status bar and minibuffer pointed out.

Ex. 1. a. Lisp Interactive Mode

If your computer is running Windows, open a Putty connection to cs01.cs.iusb.edu (or 02 or 03), then open emacs with the emacs command. Remember that F10 gives you access to the menus.

If your computer is running MacOS, open a terminal and then open an ssh connection to cs01.cs.iusb.edu (or 02 or 03) by running the command
ssh cs01.cs.iusb.edu
and then open emacs with the emacs command.

If your computer is running Linux, open an Emacs window either by typing emacs in a terminal or by launching it from the Activities - Applications.

Either way, make sure you're in a buffer called *scratch* and that the status bar shows (Lisp Interaction).

Type the following expressions in the main window:

(+ 2 3)
(* 2 (+ 3 5))
'(monday tuesday wednesday thursday friday saturday sunday)
For each of them, place the cursor at the end of the line and type Ctrl-j. Notice what happens.

Here are some more expressions to evaluate. Try to understand the logic of each result. Ask me about it if any of them doesn't make sense.

(+ 1.0 2.0 3.0 4.0)
(- 1 2 3 4)
(/ 15 2)
(/ 15.0 2)
(% 10 3)
(1+ 3)
(1- 6)
(message "hi")
(message "hi" "lo")
(message (concat "hi" "lo"))
(message "A day has %d hours." 24)
On your own: write an expression to compute 35.0 * (12 / 2.7 - 4) and evaluate it.

Here are some examples of comparisons, where nil means false, or an empty/non-existing object, and t stands for true (without the quotes). Evaluate each of them and try to understand the logic of the result.

(> 1 2)
(<= 1 2)
(= 1 2)
(/= 1 2)
(= 1 1)

Here are some examples of some useful functions and combinations of function calls:

(not (equal 1 2))
(and (< 2 5) (<= 5 9))
(max 1 12 -50)
(expt 2 3)
(abs -15)
(sqrt 9)
(car '(monday tuesday wednesday thursday friday saturday sunday))
(cdr '(monday tuesday wednesday thursday friday saturday sunday))

Can you write an expression combining the functions in last two examples above that evaluates as the symbol tuesday?

1.b. Interactive Lisp Evaluation

Choose one of the expressions above.

Type Alt-: (hold the Alt key and the shift, and hit the key ;). Notice the minibuffer displaying Eval. Type the chosen expression in the minibuffer and press enter. Notice the result displayed in the minibuffer. This text will disappear as soon as you do anything else in Emacs. Repeat the operation to familiarize yourself with it.

1.c. Debug Information

Type the following expression:

(<= 1 2 3)
and evaluate it. You should get a debugging error. Try to understand what the problem is from the error message. How would you correct the expression to evaluate without an error?

To get rid of the debugger window, type Ctrl-x 0 (zero) or click with the scrolling button in the status bar of the scratch buffer (the main window). Notice that the debugging buffer is still available in the Buffers menu at the top, under the name *Backtrace*.

1.d. Function Help

Place the cursor on one of the functions you typed before, for example, on the max. Type Ctrl-h f, releasing the control button before the f. Emacs will ask if you want documentation for the function max. Type return to confirm. A small documentation window for this function should display. Once you've read it, to get rid of it type Ctrl-x 1 or use the mouse middle button again.

Ex. 2. File Editing Mode

Create a new file (Ctrl-x Ctrl-f) and give it the name lab1.el (the extension is important). The status bar should now display (Emacs-Lisp).

At the beginning of the file, write a line starting with a semicolon ; . Write in your name, class, lab number, and anything else you want. You have just created a comment. If this expands on more than one line, each of them should start with ;.

Copy some of the expressions from earlier in the lab to this new file. Save the file.

Place the cursor at the end of the expressions you want to evaluate then type Ctrl-x Ctrl-e (hold the control for both). Note the result appearing in the minibuffer. You can turn the interactive mode on for this file buffer by typing Alt-x followed by the expression (written literally as such) lisp-interaction-mode and then enter. After this, the file buffer will allow interactive evaluation with Ctrl-j and the results will also appear in this buffer.

Note. A summary of the commands you have seen in this lab and more can be found in the Elisp Interaction page, also linked from the course main page.

Lab Submission

Upload to Canvas: under Assignments, Homework 1, the Lisp file you created for Exercise 2. You can wait until you have both the lab and the homework files to upload them in a single submission.