Skip to content

CSCI 316: Lisp Assignment 2

Due Date: October 10, 2025

Prerequisite: Complete Lisp Assignment 1 first

There will be no extension of the submission deadline if mars fails to operate normally or becomes inaccessible at any time after 6 p.m. on the due date. Try to submit no later than noon on the due date, and on an earlier day if possible!

Note: Prior to Fall 2025, this assignment was part G of the previous assignment. The submission deadline is only two days after the submission deadline for Assignment 1.

  • Write function definitions in a purely functional style—must NOT use SETF
  • Don’t use any features of Lisp other than those introduced in lectures and/or the assigned reading
  • Follow the rules in the indentation-&-spacing-rules-for-Lisp-Assignments-2-3-4-5 document on Brightspace
  • Lisp’s comment character is ; which makes Lisp ignore the rest of the line (like // in C++ or Java). See section 5.7 of Touretzky for more about comments.

Write a Lisp function SQUARE that takes a number as argument and returns the square of the number.

Solution
(defun square (x)
(* x x))

Define a Lisp function SQR-PERIMETER-AREA that returns a list of the perimeter and the area of a square, given the length of one side.

Example: (SQR-PERIMETER-AREA 2) should return (8 4).

Solution
(defun sqr-perimeter-area (side)
(list (* 4 side) (* side side)))

Write a Lisp function ROTATE-L that takes a list as argument and returns a new list in which the former first element has become the last element.

Example: (ROTATE-L '(A B C D)) should return (B C D A).

Solution
(defun rotate-l (s)
(append (cdr s) (list (car s))))

Define a Lisp function SWITCH that takes as its argument a two-element list and returns a list consisting of the same two elements, but in the opposite order.

Example: (SWITCH '(A B)) returns (B A).

Solution
(defun switch (two-list)
(list (cadr two-list) (car two-list)))

A point in the plane can be represented as a list (x y). Use the SQUARE function from problem 1 to write a Lisp function DIST that takes two such lists as arguments and returns the distance between the points they represent.

The distance between two points and is .

Solution
(defun dist (p1 p2)
(sqrt (+ (square (- (car p1) (car p2)))
(square (- (cadr p1) (cadr p2))))))

Define a Lisp function QUADRATIC that has three parameters A, B, and C and returns a list of the two roots of the equation . Use the built-in function SQRT.

The two roots are given by:

Solution
(defun quadratic (a b c)
(list (/ (+ (- b)
(sqrt (- (* b b) (* 4 a c))))
(* 2 a))
(/ (- (- b)
(sqrt (- (* b b) (* 4 a c))))
(* 2 a))))

Write your six function definitions in a file named your-last-name-2.lsp (example: if your last name is Touretzky, name your file touretzky-2.lsp). Create this file in the home directory of your xxxxx_yyyy316 mars account.

You have two options:

Use an editor (nano, vim, or emacs) on mars to create the file. For example:

Terminal window
nano your-last-name-2.lsp

Example: If your last name is Touretzky, enter nano touretzky-2.lsp at the xxxxx_yyyy316@mars:~$ prompt.

More information about nano, vim, and emacs is provided in the “Some Good Editors That Match Parentheses” and “Emacs is a Good Editor for Writing Lisp Code on mars” sections below.

Option B: Create on Your PC/Mac and Copy to mars

Section titled “Option B: Create on Your PC/Mac and Copy to mars”

Alternatively, create the file on your own PC/Mac and copy it to mars as explained in the “Copying Files from Your PC or Mac to mars” section.

Important: If you choose this option, first try copying a file from your PC or Mac to mars at least 3 days before the submission deadline. Only use this option if you are confident you can copy files to mars. Problems with copying files to mars will not be regarded as legitimate reasons for late submission!

Regardless of which option you choose, also create a folder ~/316lisp on your PC/Mac and put copies of all your Lisp files (including your-last-name-2.lsp) there, so you will have access to those files if you lose access to files on mars.

To create the ~/316lisp folder:

  1. Open a PowerShell window on the PC or a Terminal window on the Mac
  2. Enter: mkdir ~/316lisp

Important: Every Lisp file should have a .lsp extension (not .lisp). You will not be able to submit a Lisp file if it doesn’t have a .lsp extension!

When logged into mars, start Clisp by entering:

Terminal window
cl

at the xxxxx_yyyy316@mars:~$ prompt. If you have Clisp installed on your PC/Mac, you can run it locally:

  1. Enter: cd ~/316lisp
  2. Enter: clisp

If you have a file xyz.lsp containing Lisp function definitions, load it into Clisp by entering:

(load "xyz")

at Clisp’s > prompt. This assumes there is no file named xyz, xyz.lisp, or xyz.cl in your working directory; rename any such files before loading.

When you enter (load "xyz"), the function definitions in xyz.lsp are processed as if you had entered them at Clisp’s prompt. If (load "xyz") produces an error, there is a syntax error in the file.

Unless you are running Clisp as a subprocess of emacs, each time you add or modify a Lisp function definition:

  1. Save the file
  2. Re-LOAD it into Clisp
  3. Immediately test the new/modified function

This way, whenever (load "xyz") gives an error, it must be due to a syntax error in the new or modified function.

Since Lisp uses many parentheses, always edit Lisp files with an editor that matches parentheses!

  • vim: Matches parentheses automatically. In command mode, type % at a parenthesis to jump to the matching parenthesis. Enter vimtutor on mars to bring up a tutorial.
  • emacs: Has automatic parenthesis matching. After starting emacs, type Ctrl-h t to bring up the tutorial.
  • nano: Can match parentheses. Type Ctrl-] Ctrl-] when the cursor is at a parenthesis to move to the matching parenthesis.

If using emacs and connecting from a Windows PC, I recommend using a terminal emulator such as PuTTY (https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html) because certain keyboard shortcuts won’t work with Windows’ default ssh client.

Start emacs:

Terminal window
emacs

Then type Ctrl-h t to bring up the tutorial.

When you place the cursor at an opening parenthesis or immediately after a closing parenthesis, the matching parenthesis pair is highlighted.

  • Ctrl-Meta-f: Move forward across one S-expression (from an opening parenthesis, this moves to the position after the matching closing parenthesis)
  • Ctrl-Meta-b: Move backward across one S-expression (from after a closing parenthesis, this moves back to the matching opening parenthesis)
  • Ctrl-Meta-k: Delete an S-expression (type Ctrl-/ to undo)
  • Meta-t: Transpose the S-expression with the previous one

Start emacs with a filename:

Terminal window
emacs test.lsp

Split the emacs window by typing Ctrl-x 2, then enter Meta-x run-lisp to get a Clisp prompt in one window.

Switch between editing and Clisp by typing Ctrl-x o.

Load a new/changed function definition while editing by typing Ctrl-Meta-x with the cursor within or immediately to the right of the definition. This allows testing before saving.

In the Clisp window, cycle through previously entered expressions with Ctrl-p or Ctrl-n.

Unsplit the window by typing Ctrl-x 1, then switch windows with Ctrl-x b.

You cannot copy files to mars unless your PC/Mac is connected to the QC VPN or you are on campus connected to the qwifi-secured wireless network.

If your mars username is toure_davi316, use this command in a PowerShell window (PC) or Terminal window (Mac) to copy touretzky-2.lsp from your local directory to mars:

Terminal window
scp touretzky-2.lsp toure_davi316@mars.cs.qc.cuny.edu:

Note the colon at the end!

PowerShell (Windows PC):

  1. Press Win-r
  2. Type powershell and press Enter

Terminal (Mac):

  1. Press Cmd-Space
  2. Type terminal and press Enter

If the file is in another directory, use cd before the scp command:

Terminal window
cd ~/316lisp
scp touretzky-2.lsp toure_davi316@mars.cs.qc.cuny.edu:

For submission guidelines, file naming requirements, testing, and verification instructions, see clisp-submission-instructions.md.

Submit your file your-last-name-2.lsp using the command:

Terminal window
submit_lisp_asn your-last-name 2