Dana Vrajitoru
C311 Programming Languages

C311 Homework 4

Due Date: Wednesday, February 8, 2023.

Note. The results of the entire exercise should be sent in one file with the extension .el uploaded to Canvas, Assignment 4. In this homework we are introducing the while loop and practicing the dotimes loop.

Ex. 1. Prime number

Implement a function in Lisp called is-prime checking if a number is prime. Use the dotimes loop for it. What limit can you use for the loop to make the function efficient?

Examples of results:
(is-prime 0) ; nil
(is-prime 1) ; nil
(is-prime 2 ) ; t
(is-prime 17)) ; t
(is-prime 24)) ; nil

Ex. 2. Previous to last

Implement a function in Lisp called prev-to-last that takes in one parameter that is a list and returns the previous to last element of the list. If the list has less than two elements, it should return nil. Use the while loop for this either combined with with the function pop, or following the model of the non-recursive function last from the slides.

Examples of results:
(prev-to-last '()) ; nil
(prev-to-last '(a)) ; nil
(prev-to-last '(12 83) ) ; 12
(prev-to-last '(a b c d e)) ; 'd

Ex. 3. Print list

Write a function in Lisp called print-list taking one parameter which we can assume to be a list. The function should print all the elements of the list using the function princ and a space between them. Use the loop dolist for this.

Show a few examples of testing these functions.

Canvas Submission

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