Dana Vrajitoru
C311 Programming Languages

C311 Homework 6

Due Date: Wednesday, February 26, 2025.

This homework is about scope and about symbols in Lisp.

Ex. 1 Symbols in Lisp

Write the following functions in Lisp:

a. A function called next-day that takes one parameter that you can assume to be a symbol named after a day of the week (no need to check it), such as 'monday, and returns the symbol for the next day. For example,

(next-day 'tuesday)
should return the symbol 'wednesday. Implement this function with a complex conditional.

b. A function that generates a random day of the week, then displays a message saying that "Today is ... and tomorrow will be ...".

For this purpose, create a global variable before the function containing the days of the week as a list.

Then use the built-in function random first to generate a number between 0 and 6 (including). The expression (random) by itself generates a random integer. You can call it with one parameter to return a value within the range from 0 to the value of the parameter-1. For example, (random 10) will return a value between 0 and 9.

Next, use the number generated at the previous step to retrieve the symbol for the day of the week from the list. For that you can either copy your own function element-i from the previous homework, or use the built-in elt. Here's an example of a call to elt which evaluates to 3:

(elt '(1 2 3 4) 2) ; returns 3

We want the name of the day to appear capitalized. For this, extract the symbol-name of the day first, then apply the built-in function capitalize to it. Use the result in the princ function call, and do the same thing for the next day.

Make the function return true (t) instead of the last thing it evaluates, to avoid seeing the message printed more than once.

Ex. 2. Mutator Function

Write a list mutator function called insert-second that inserts a new value in a list in the second place. If the list is empty, just return nil without doing anything. For this, use the function setcdr combined with push or cons.

For example if the variable L contains the list '(2 5 4 1 8), then calling

(insert-second L 7)

should return the list '(2 7 5 4 1 8) and after the operation, L should also contain the new list.

Ex. 3. First-fit, best-fit

Write two Lisp functions inspired from memory allocation methods that will be discussed in class. Both functions take two parameters: a list containing numbers and a simple value that we can assume to be a number. Both functions should return nil if they don't find what they are looking for.

a. The first function should be called first-fit and it should return the first element in the list that is larger than or equal to the value.

For example,
(first-fit '(3 8 6 4 9) 5)
should return 8.

b. (*A3) (not optional) The second should be called best-fit and it should return the element of the list that best fits the value. For this, you have to look for the smallest value in the list that is larger than or equal to the value. Do not use the built-in function sort, nor sort the list in some other way. The solution should be linear and sorting is worse than linear.

For example,
(best-fit '(3 8 6 4 9) 5)
should return 6.

Homework Submission

Upload the Lisp file to Canvas, Assignments, Homework 6.