CSCI 316: Lisp Assignment 2
This document includes Kong’s official scheme solutions.
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.
Assignment Requirements
Coding Style
- 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-5document 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.
Assignment
- Write a Lisp function
SQUAREthat takes a number as argument and returns the square of the number.
Solution
(defun square (x)
(* x x))
-
Define a Lisp function
SQR-PERIMETER-AREAthat 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-Lthat 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
SWITCHthat 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 $(x, y)$ in the plane can be represented as a list
(x y). Use theSQUAREfunction from problem 1 to write a Lisp functionDISTthat takes two such lists as arguments and returns the distance between the points they represent.The distance between two points $(x_1, y_1)$ and $(x_2, y_2)$ is $\sqrt{(x_1 - x_2)^2 + (y_1 - y_2)^2}$.
Solution
(defun dist (p1 p2)
(sqrt (+ (square (- (car p1) (car p2)))
(square (- (cadr p1) (cadr p2))))))
-
Define a Lisp function
QUADRATICthat has three parameters A, B, and C and returns a list of the two roots of the equation $Ax^2 + Bx + C = 0$. Use the built-in functionSQRT.The two roots are given by:
$\frac{-B + \sqrt{B^2 - 4AC}}{2A} \quad \text{and} \quad \frac{-B - \sqrt{B^2 - 4AC}}{2A}$
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))))
File Preparation
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.
Creating the File
You have two options:
Option A: Using an Editor on mars
Use an editor (nano, vim, or emacs) on mars to create the file. For example:
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
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!
Local Backup
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:
- Open a PowerShell window on the PC or a Terminal window on the Mac
- Enter:
mkdir ~/316lisp
Working with Lisp Files
Loading Function Definitions into Clisp
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!
Starting Clisp
When logged into mars, start Clisp by entering:
cl
at the xxxxx_yyyy316@mars:~$ prompt. If you have Clisp installed on your PC/Mac, you can run it locally:
- Enter:
cd ~/316lisp - Enter:
clisp
Loading a File
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.
Workflow Recommendation
Unless you are running Clisp as a subprocess of emacs, each time you add or modify a Lisp function definition:
- Save the file
- Re-LOAD it into Clisp
- 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.
Some Good Editors That Match Parentheses
Since Lisp uses many parentheses, always edit Lisp files with an editor that matches parentheses!
Editors on mars
- vim: Matches parentheses automatically. In command mode, type
%at a parenthesis to jump to the matching parenthesis. Entervimtutoron mars to bring up a tutorial. - emacs: Has automatic parenthesis matching. After starting emacs, type
Ctrl-h tto bring up the tutorial. - nano: Can match parentheses. Type
Ctrl-] Ctrl-]when the cursor is at a parenthesis to move to the matching parenthesis.
Editors for Windows PCs
- Notepad++: https://notepad-plus-plus.org
- VS Code: https://code.visualstudio.com
Editors for Macs
- VS Code: https://code.visualstudio.com
- vim and nano are preinstalled for use in a terminal window
Emacs for Lisp Development on mars
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.
Using emacs
Start emacs:
emacs
Then type Ctrl-h t to bring up the tutorial.
Parenthesis Matching in emacs
When you place the cursor at an opening parenthesis or immediately after a closing parenthesis, the matching parenthesis pair is highlighted.
Key Bindings
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 (typeCtrl-/to undo)Meta-t: Transpose the S-expression with the previous one
Running Clisp as a Subprocess of emacs
Start emacs with a filename:
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.
Copying Files from Your PC or Mac to mars
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.
Using SCP
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:
scp touretzky-2.lsp toure_davi316@mars.cs.qc.cuny.edu:
Note the colon at the end!
Opening PowerShell/Terminal
PowerShell (Windows PC):
- Press
Win-r - Type
powershelland press Enter
Terminal (Mac):
- Press
Cmd-Space - Type
terminaland press Enter
Changing Working Directory
If the file is in another directory, use cd before the scp command:
cd ~/316lisp
scp touretzky-2.lsp toure_davi316@mars.cs.qc.cuny.edu:
Submission Instructions
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:
submit_lisp_asn your-last-name 2