Dana Vrajitoru
C311 Programming Languages
C311 Homework 2
Due Date: Wednesday, January 25, 2023.
Note. The results of the entire homework should be kept in
one Lisp file named hw2.el to be uploaded to Canvas.
Ex. 1 Variables in Lisp
Write a Lisp program and do the following:
- Declare 3 variables, one of which should contain a description.
- Assign numerical values to the three variables.
- Show an example (as many expressions as you want) of using the
following mathematical functions, involving the variables you
declared: expt, random, abs, cos,
truncate.
- Invoke the help on one of the functions listed above and copy it
into the program, then comment it out.
Hint: when editing a file with the extension
.el, a menu called Emacs-Lisp should appear in
Emacs. You can select the entire region you want to comment out and
then use a command in this menu to comment it out.
- Turn the interactive mode on and evaluate your expressions to make
sure they are correct.
Ex. 2 Function Definition
Implement the following functions in the same file, complete with
short descriptions. Do not use loops for these functions (meaning, use
recursion when needed), nor built-in functions that can solve the
problem directly.
- A function with a single parameter called prod-list that
returns the product of the elements of a list. We can assume that the
argument is a list containing only numbers. If the list is empty, the
function should return 0.
Hint: use recursion combined with the functions car
and cdr.
- A function called is-in-list that takes two parameters,
the first being a list and the second a value, and returns true
(t) if the value is in the list and false (nil) if
not. Use the function equal to test for equality of two values.
- A function called median taking 3 arguments and computing
the median of them. For example, if the arguments are 5, 2 and 4, the
median is 4. This function should not be recursive. Note that some of
the arguments can be equal to each other: the median of 7, 3, and 7 is
7.
Evaluate all of these functions and show a few examples of testing
them and comment out the results. Add a comment at the top with your
name and homework number.
Upload to Canvas, Assignments, Homework 2, the
file hw2.el.