[SOLVED] CSCI316-Lisp Assignment 2

20.99 $

Category:

Description

5/5 - (2 votes)
  1. ) Can an atom be a variable?                (b) Can a number be a variable?

(c)  Can a list be a variable?                      (d) Can an atom have no value?

  • Can an atom have more than one value at the same time?
  • Can a variable have itself as a value?
  • Can a list (A B C) be the value of two different variables?

 

  1. Each of the following may be a single atom, a single list, or neither. Identify each         For the lists, also say how many elements the list has.  [You are      not asked to evaluate any expression––e.g., (+ 1 9) would be a list of 3 elements,      even though this list would evaluate to a numeric atom (i.e., 10).]           (a)  ATOMS                                     (b) (THIS IS AN ATOM)        (c)  )(

(d) ((A B)(C D)) 3 (3)               (e) (()())

(f) ((A B C                                          (g) (/ (+ 3 1) (- 3 1))         (h) (LIST 3)

 

3.[Exercise 4 on p. 37 of Wilensky]  Use SETF to assign X the value (A B C), and      then use X to produce the list (A B C A B C).

 

4.[Exercise 5 on p. 37 of Wilensky]  Write the expression  ”(A) using QUOTE      rather than ‘.   What is the data type of the expression ‘A ?

 

5.(a)  Use SETF to give Y the value (A B).  But, instead of writing ‘(A B), you must           use a Lisp function call to create the list (A B).

 

(b) Write code that makes the list  (D A).  However, you must get A from the                    variable Y, which has the value (A B) from part (a).

 

 

  1. Define a function called SQR that returns a list of the perimeter and the area of  a square, given the length of one side.  Thus (SQR 2) should return (8 4).

 

Lisp Assignment 2: Page 1 of  4

  1. Define QUADRATIC, a function with three parameters A, B and C that returns a list of the two roots of the equation Ax2 + Bx + C =   You should use the         built-in function SQRT.  Recall that the two roots are given by:

− +B B2 4AC     and    − −B B2 4AC

2A                                         2A

 

8.[Exercise 1 on p. 52 of Wilensky]  Write a Lisp function that computes the area of         a circle given its radius.  Use the predefined constant PI.

 

  1. Define a function called FTOC which takes as its argument a degree reading in

Fahrenheit and returns its Celsius equivalent.  (The Celsius equivalent of a                  Fahrenheit temperature is obtained by subtracting 32 and multiplying by 5/9.)

 

  1. Define a function ROTATE-LEFT which takes a list as argument and returns a new list in which the former first element has become the last element.  Thus              (ROTATE-LEFT ‘(A B C D)) should return (B C D A).

 

11.[Exercise 4 on pp. 52 – 3 of Wilensky] A point (x, y) in the plane can be        represented as a two-element list (x y).  Write a Lisp function that takes two such        lists as arguments and returns the distance between the corresponding points.        Recall that the distance between two points (x1, y1) and (x2, y2) is given by

(x1−x2)2 +(y1−y2)2.

 

12.[Exercise 5 on pp. 52 – 3 of Wilensky] Define Lisp functions HEAD and TAIL       that behave just like CAR and CDR, respectively.

 

13.[Exercise 6 on pp. 52 – 3 of Wilensky] Define a Lisp function SWITCH that        takes as its argument a two-element list and returns a list consisting of the same       two elements, but in the opposite order.  Example: (switch ‘(A B)) => (B A).

 

  1. Suppose you have just entered the following three Lisp expressions at successive Clisp > prompts (with no spaces before or after * and + in 8*7 and 8+7):

(setf 8*7 5)

(defun 8+7 (x y) (+ x y))                    (defun 8*7 () 9)

If you now enter the expression    (8+7 (* 8 7) (8+7 (8*7) 8*7))       what value will be printed by Clisp?   Check your answer using Clisp.

 

 

 

 

 

 

 

The next six questions are important.  Be sure to check your answers using Clisp!

 

  1. [Exercise 1 on pp. 36 – 7 of Wilensky] For each of (a), (b), and (c) below,     suppose SETF has just been used to give the variable E the specified value.

(E.g., for (a) we suppose (setf e ‘(a b x d)) has just been entered at Clisp’s        > prompt.)  In each case, write an expression that involves only E, car, and cdr,        and which evaluates to the symbol X.   [Hint: For a specified value of (A X C), you         would be expected to write the expression  (car (cdr E)) because the car of the cdr of

(A X C) is X.]

(a)          (A B X D)      (b) (A (B (X D)))  (c) (((A (B (X) D))))

16.[Exercise 2 on pp. 36 – 7 of Wilensky]  For each of the three lists in exercise 15,       write an expression that involves only quoted symbols, NIL, and calls of CONS,        and which evaluates to that list.   [Hint: For a list  (A X C), you would be expected         to write the expression   (cons ‘a (cons ‘x (cons ‘c nil))).]

Note: One way to solve such problems is to first write the list using calls of LIST, and then         rewrite the expression using appropriate calls of CONS.  (Another approach is to do a preorder         traversal of the tree-representations of the given lists––read p. 393 in Sethi for more on the         tree-representation of an S-expression.)

 

For questions 17 – 20, suppose E has been given a value as follows:

(    ‘((90 91 92 93 94 95 96 97 98 99) (+ 3 4 –) (9 19 29 39 49 59 69 79 89 99)))setf E     For each question, write an expression with the specified properties; your expressions may involve E, ‘A, ‘B and Lisp functions.  LIST is a good function to use.

 

Example  Write an expression that does not involve any numbers, but which evaluates to the following list:

(92 (29 39 49 59 69 79 89 99 + 3 4 -))

Solution        (list (third (first E)) (append (rest (rest (third E))) (second E)))

 [Or, equivalently, (list (caddr   (car E)) (append        (cddr (caddr E))   (cadr E))).] 17. Write an expression that does not involve any numbers, but which evaluates                 to the list  (((90 91) 92 93 94 95 96 97 98 99) (A B 29 39 49 59 69 79 89 99)).

  1. Write an expression that does not involve any numbers, but which evaluates        to the list ((90 A 92 93 94 95 96 97 98 99) 3  29 (4 29 39 49 59 69 79 89 99)).   19. Write an expression that does not involve any numbers, but which evaluates to             the list   ((90 91 92 93 94 95 96 97 98 99 3) (+ 3 4 – 29 39 49 59 69 79 89 99)). 20. Write an expression that does not involve any numbers, but which evaluates                 to the list ((A 91 92 93 94 95 96 97 98 99) (90 (19 29) 39 49 59 69 79 89 99)).