(A) Atoms (B) (This Is An Atom) (C) ) ( (D) ( (A B) (C D) ) 3 (3) (E) ( ) (F) ( (ABC (G) (/ (+ 3 1) (-3 1) ) (H) (LIST 3)
(A) Atoms (B) (This Is An Atom) (C) ) ( (D) ( (A B) (C D) ) 3 (3) (E) ( ) (F) ( (ABC (G) (/ (+ 3 1) (-3 1) ) (H) (LIST 3)
For problems 6 – 13, use a text editor to create a file solutions.lsp that contains your
function definitions. For more information, see the Working with Lisp Files section of
this document on pp. 3 – 4. Some recommended text editors are mentioned on p. 4.
Lisp's comment character is “;”––this makes Lisp ignore the rest of the line, and is
used like “//” in C++ or Java. See p. 150 of Touretzky for more about comments.
IMPORTANT: Do NOT use SETF except when you are answering questions
which explicitly indicate that you may use them!
2. Each of the following may be a single atom, a single list, or neither. Identify each
accordingly. 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)
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).
9. 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.)
10. 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).
14. 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.
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)).
18. 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)).
Working with Lisp Files
Suppose xyz.lsp is a file, in your current working directory, that contains one or more Lisp function definitions. Then
you can "load" xyz.lsp into Clisp by entering (load "xyz.lsp") at Clisp's > prompt. When you do this, the
function definitions in xyz.lsp will be read into Clisp as if you had entered them one by one at Clisp's prompt. [If
(load "xyz.lsp") produces an error, there is a syntax error in the function definitions in xyz.lsp.] Unless you
are running Clisp as a subprocess of emacs (see below), each time you add a new lisp function definition to a file
xyz.lsp or modify an existing definition in the file, it's a good idea to immediately save the file, load the saved file
into Clisp, and then test the new or modified function. If you work this way, then whenever (load "xyz.lsp")
gives an error message it must be because of a syntax error in the new or modified function.