Question: CS3723 Pgm2 Lisp (20 points) Code the functions listed below and use the specified test cases. Notes: Look at the set up information for more
CS3723 Pgm2 Lisp (20 points)
Code the functions listed below and use the specified test cases.
Notes:
Look at the set up information for more information on executing LISP files.
The only functions you can use are those we discussed in the LISP notes (including ones we developed as exercises), princ and terpri (see below), and you can reuse any of the functions developed below in subsequent functions.
Load your code using (load "p2Lisp.txt" :echo T :print T).
To execute the test cases using the file I provided: (load "p2LispRun.txt" :echo T :print T)
Your functions must be executed on a fox server using the specified test cases.
Turn in a zip file named LastNameFirstName.zip (no spaces) containing:
Your source LISP code (p2Lisp.txt)
Your log of the session (see the setup instructions). This should be a .txt file.
Your code must follow my LISP programming standards.
Some built-in output functions which you are allowed to use:
(princ arg) - prints an argument. It does not follow it with a linefeed.
(terpri) - prints a linefeed.
1. Code the function (repeat atm N) which constructs a new list with atm repeated N times.
Example:
> (repeat 'A 5) use ZEROP
(A A A A A)
2. Code the function (duplicate lis) which duplicates each atom at the top level of a list. If there is a list at the top-level, that list isn't duplicated, but it is included in the result.
Hint: You may have to use either CONS twice or REPEAT.
Examples:
> (duplicate ' (A B C))
(A A B B C C)
> (duplicate ' (GUM (HAS BEEN) CHEWED))
(GUM GUM (HAS BEEN) CHEWED CHEWED)
3. Code the function (duplicateN lis N) which duplicates each atom at the top level N times. If there is a list at the top-level, it isn't duplicated, but it is included in the result.
Hint: APPEND could be useful.
Examples:
> (duplicateN ' (A B C) 2)
(A A B B C C)
> (duplicateN ' (A B C) 3)
(A A A B B B C C C)
> (duplicateN ' (GUM (HAS BEEN) CHEWED) 2)
(GUM GUM (HAS BEEN) CHEWED CHEWED)
4. Code the function duplicateDeep which duplicates each atom at any level. If there is a list at any level, each atom in that list is duplicated. Hint: APPEND could be useful.
Examples:
> (duplicateDeep ' (A B C) )
(A A B B C C)
> (duplicateDeep ' (A (B D) E (F)) )
(A A (B B D D) E E (F F))
> (duplicateDeep '(A (B (D E) (F G)) (H I)) )
(A A (B B (D D E E) (F F G G)) (H H I I))
5. Code the function (printWOP lis) which is passed a list. It prints lis without surrounding parentheses on one line.
It first does a (terpri) to cause printing at the beginning of the line.
If the lis is an atom, simply PRINC it and do not do the next step.
For each entry in the list:
use PRINC to print the item
use PRINC to print a space (PRINC " ")
Finally, it does another (terpri).
Functionally, printWOP returns T.
Hint: break this into two functions. The step where we go through the list should probably be done by another function.
> (printWOP '(A B D))
A B D
T
> (printWOP '(A (B D) E F) )
A (B D) E F
T
> (printWOP NIL)
NIL
T
> (printWOP 'A)
A
T
6. Code the function, (evalEach lis) which evaluates each item in the lis using the built-in EVAL function. Its functional value is the value of the last expression evaluated.
> (evalEach '( (setf A 5) (print 'hello) (print 'there) A))
HELLO
THERE
5
> (evalEach '( (setf x 10 ) (setf A '(x y z)) (print A) (setf B (car A)) (set B (+ 5 x)) )
(X Y Z)
15
> (print B)
X
X
> (print X)
15
15
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
