Computer Languages History
(Click or use local copy.)
 CS 3723/3721
 Programming Languages
 Spring 2005

 Recitation 10
 Lisp: Basics and Functions
    Week 10: Mar 28 - Apr 1
 Due (on time): 2005-04-04  23:59:59
 Due (late):        2005-04-08  23:59:59

Recitation 10 must be submitted following directions at: submissions with deadlines
  • 2005-04-04  23:59:59 (that's Monday, 4 April 2004, 11:59:59 pm) for full credit.
  • 2005-04-08  23:59:59 (that's Friday, 8 April 2004, 11:59:59 pm) for 75% credit.

Basics:

  1. Try out numeric computations. Do and calculate (You must convert these to Lisp notation, using S-expressions, with the function first.)
  2. Try Use setq to assign and similarly for b and c, to give lengths of the sides of a triangle. Then set and similarly for beta and gamma, to give the three angles of the triangle. Finally set The final answer should be approximately equal to the sum of the angles in a triangle, that is, 180.
  3. Try out car, cdr, and cons, using the following examples: Now try
  4. Try out append and list:

Functions:

    For this recitation and the next one, your Lisp functions should not have any iterative constructs (such as prog, do, etc.) but all iteration should be done with recursion. Inside functions you should not make use of the setf or setq functions.

  1. Consider the follwing lisp function (the lisp functions defun and cond will be discussed in class): Save this function as a file named "fact.l". Then try out
  2. Mimic the factorial function above to define a function harmonic, where (harmonic n) returns the value

    Special Note (Spring 2005): Only gcl on the Linux machines seems to be doing these calculations in double precision.

    1. First use the operator / with integers, so that the answers are rational numbers. [Try out n = 10, 20. As a check, the value of (harmonic 10) should be 7381/2520.]
    2. Then rewrite the harmonic function using constants with a decimal point to force Lisp to use floating point numbers. [Now as a check the value of (harmonic 10) should be 2.9289682539682538.]
    3. It turns out that (harmonic n) is approximately equal to log(n) + g, where g = 0.5772156649. . . is known as Euler's constant. Use Lisp to check the accuracy of this approximation for n = 10, 100, 1000. (Here log is the logarithm base e, which is available in Lisp under the name log.) [The accuracy should be better than 1/n.]

  3. Write a recursive function fibonacci that will return the nth fibonacci number F[n], where [For reference, (fibonacci 10) should return 55, (fibonacci 20) returns 6765 after a few seconds, and (fibonacci 30) returns 832040 after as much as several minutes. Why do you think this takes so long?]
  4. Write a recursive function maxvec that finds the maximum of a simple list of numbers. [Hint: if the cdr is nil, just return the car, and otherwise return the maximum of the car and of maxvec applied to the cdr. For example, (maxvec '(2 5 6 3)) returns 6.]
  5. Try out the function reverse in Lisp: Define your own function reverse1 which will behave the same way as reverse, reversing the order of elements of a list at the top level only. [Hint: Invoke reverse1 recursively on the cdr and tack on the car at the end.]

What you should submit: Refer to the submissions directions and to deadlines at the top of this page. The text file that you submit should first have Your Name, the Course Number, and the Recitation Number. The rest of the file should have the following in it, in the order below, and clearly labeled, including at the beginning the appropriate item numbers: 1 through 9.

 Contents of submission for Recitation 10:

Last Name, First Name; Course Number; Recitation Number.

Answers to questions 1 through 9 above. In several cases, such as numbers 3, 4, and 5, it's just a matter of providing a log of your runs of lisp. In case a questions asks you to define a function (6, 7, 8, 9), you must supply runs of the function, as well as the code for the function.


Key idea: Lisp is a highly recursively oriented language.


Revision date: 2005-03-23. (Please use ISO 8601, the International Standard Date and Time Notation.)