Dana Vrajitoru
C311 Programming Languages

C311 Homework 6

Due Date: Wednesday, February 22, 2023.

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 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) 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.

Ex. 3 (optional, 3 extra credit points)

Write a function that prints out the calendar for the current month. This function should take 2 parameters: the day of the week that the month starts from as an index (0 for Sunday, etc.) and the number of days in the month. For example, if you call the function

(calendar 2 30)
the result should be
S  M  T  W  T  F  S
      1  2  3  4  5
6  7  8  9  10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30

Print the initial of each day on the first line using the list of days defined at the previous exercise, the capitalized symbol-name as before, and the function substring to extract the first letter of the string. For example, the call:

(substring "Sunday" 0 1)
returns the string "S".

Show an example of evaluating this function.

Homework Submission

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