Lisp Self Test #2

CIS347

 

Winston & Horn:

Which are atoms, which are lists, which are neither and which are both:

1. a. atom

   b. (this is an atom)

   c. (this is a list)

   d. (this (is also a list)

   e. )()(

   f. ()

   g. ((()))

 

Write a sequence of firsts and rests to pick out pear.

2. a. (apple orange pear grapefruit)

 

   b. (((apple) (orange) (pear) (grapefruit)))

 

   c. (apple (orange) ((pear)) (((grapefruit))))

 

Evaluate:

3. a. (rest ‘((a b)(c d)))

 

   b. (first (rest (first ‘((a b)(c d)))))

 

   c. (append ‘(a b c) ‘())

 

   d. (append ‘() ‘(a b c))

 

   e. (append ‘() ‘())

 

   f. (cons ‘() ‘())

 

(4-7) Define:

4. rotate-left

 

5. rotate-right

 

6. Celsius to Fahrenheit conversion, based on:

    F = (C + 40) x 9/5 - 40

 

7. Fahrenheit to Celsius conversion, based on:

    C = (F + 40) x 5/9 - 40

 

Set Fun

Define:

8. setu

       Input: set1, set2 (two sets with no duplication of elements)

        Returns: Union of set1 and set2.

 

9. setdf

        Input: set1, set2

        Returns: Set difference of set1 and set2.

 

10. setint

        Input: set1, set2

        Returns: Intersection of set1 and set2.

 

List Processing Web Page:

Answer the questions about the functions defined below:

(defun printlist (list)

     (if (null list)

        'done

        (and

          (print (first list))

          (printlist (rest list))

         )

        )

 )

 

(defun printodds (list)

     (cond ((null list))

           ((oddp (first list))

            (print (first list))

            (printodds (rest list)))

           (t (printodds (rest list)))

           ))

 

11. Why did we use if in printlist but cond in printodds?

 

12. Why did we use and in printlist but not in printodds?

 

13. What is the purpose of t in the last line of code?

 

14. Why could we get away with not stating an action for the first conditional of printodds?

 

 (defun printrl (list)

     (if (endp list)

        'done

        (and

             (printrl (rest list))

             (print (first list)))

        ))

 

Evaluate:

14. (printrl ‘(a b c))

 

15. (printrl ‘(a))

 

16. (printrl ‘())

 

Other:

(17-19) Define:

17. flatp: a function that takes a list as its argument and returns t if the list has no sublists; nil otherwise.

 

18. countodds: a function that takes a list of integers and returns the number of odd integers in that list.

 

19. pow: a function that takes a 2 positive integers, say base & expt, as argument and returns baseexpt, i.e., the 1st number raised to the power of the 2nd number.

 

(20-25) Given the code below:

 

(defun mys (list)

    (or (endp list)

         (and (atom (first list))

                (flatp (rest list))

         )))

 

Evaluate:

20. (mys ‘(a b))

 

21. (mys ‘((a b) c))

 

22. (mys ‘(()()))

 

23. (mys ‘((())(())))

 

24. (mys ‘a)

 

25. Write mys2 which is just like mys except that

          (mys2 ‘(()()))

          returns nil.

Lisp Self Test #2

Word Bank

CIS347

 

For questions #11 - 14.

A. Actions do not need to be specified when a conditional test fails

B. Because if cannot be used in conjunction with integers

C. Because in printodds we only needed to take one action

D. Because printlist involves recursion, but printodds does not

E. Because we wanted to return t as the final value

F. In printlist there were only 2 cases to consider; in printodds there were 3.

G. In printlist there were only 3 cases to consider; in printodds there were 2.

H. It serves as an else

I. It was assigned a value in the base case

J. To allow us to take 2 separate actions; if allows only 1 action to be taken,

        cond allows any number of actions.

K. When no action is specified for a true condition test, cond returns the value

        of the test itself.

L. When no action is specified, the Lisp interpreter skips this line of code