Docsity

CS 401/501 Assignment #4: LISP Function Exercises, Assignments of Programming Languages

Toggle sidebar
bg1
CS 401/501 ASSIGNMENT #4
Due Tuesday, March 17, 2009 1
1. Write a LISP function (listFromTo M N), where Mand Nare integers, which returns a list
of all integers from Mto N. Test your function on the following:
(listFromTo 2 5) โ‡’(2 3 4 5)
(listFromTo -1 -10) โ‡’nil
(listFromTo -10 -1) โ‡’(-10 -9 ... -1)
2. Write a LISP function (removeMultiples L N), where Lis a list of integers and Nis an
integer, which returns Lwith all elements which are divisible by Nremoved. Test your
function on the following:
(removeMultiples โ€™(2 3 4 5) 2) โ‡’(3 5)
(removeMultiples โ€™(5 10 15) 5) โ‡’nil
(removeMultiples โ€™(2 3 5 7) 9) โ‡’(2 3 5 7)
3. Write a LISP function (sieveOfEratosthenes N), where Nis an integer, which returns a list
of all prime numbers between 2 and N. The function should use the Sieve of Eratosthenes to
compute the prime numbers as follows:
Begin with a list of integers from 2 to N. The first integer, 2, is the first prime. Remove every
multiple of this prime. Repeatedly take the next integer in the list as the next prime and
remove every multiple of the prime.
Test your function on the following:
(sieveOfEratosthenes 1) โ‡’nil
(sieveOfEratosthenes 5) โ‡’(2 3 5)
(sieveOfEratosthenes 30) โ‡’(2 3 5 7 11 13 17 19 23 29)
4. Consider the Core program below:
input n;
odd := 1; sum := 1;
while (odd < 2 * n - 1) loop
odd := odd + 2;
sum := sum + odd;
end loop;
output sum;
Assuming that the input file contains the integers 3 and 2 (i.e., the list <3, 2>), use the
denotational semantics of Core to trace the interpretation of the program.
1This is the absolute deadline for turning in this assignment. No late assignments will be accepted.

Partial preview of the text

Download CS 401/501 Assignment #4: LISP Function Exercises and more Assignments Programming Languages in PDF only on Docsity!

CS 401/501 ASSIGNMENT

Due Tuesday, March 17, 2009 1

  1. Write a LISP function (listFromTo M N), where M and N are integers, which returns a list of all integers from M to N. Test your function on the following: (listFromTo 2 5) โ‡’ (2 3 4 5) (listFromTo -1 -10) โ‡’ nil (listFromTo -10 -1) โ‡’ (-10 -9 ... -1)
  2. Write a LISP function (removeMultiples L N), where L is a list of integers and N is an integer, which returns L with all elements which are divisible by N removed. Test your function on the following: (removeMultiples โ€™(2 3 4 5) 2) โ‡’ (3 5) (removeMultiples โ€™(5 10 15) 5) โ‡’ nil (removeMultiples โ€™(2 3 5 7) 9) โ‡’ (2 3 5 7)
  3. Write a LISP function (sieveOfEratosthenes N), where N is an integer, which returns a list of all prime numbers between 2 and N. The function should use the Sieve of Eratosthenes to compute the prime numbers as follows: Begin with a list of integers from 2 to N. The first integer, 2, is the first prime. Remove every multiple of this prime. Repeatedly take the next integer in the list as the next prime and remove every multiple of the prime. Test your function on the following: (sieveOfEratosthenes 1) โ‡’ nil (sieveOfEratosthenes 5) โ‡’ (2 3 5) (sieveOfEratosthenes 30) โ‡’ (2 3 5 7 11 13 17 19 23 29)
  4. Consider the Core program below:

input n; odd := 1; sum := 1; while (odd < 2 * n - 1) loop odd := odd + 2; sum := sum + odd; end loop; output sum;

Assuming that the input file contains the integers 3 and 2 (i.e., the list <3, 2>), use the denotational semantics of Core to trace the interpretation of the program.

(^1) This is the absolute deadline for turning in this assignment. No late assignments will be accepted.