In LISP, code the functions listed below and use the specified test cases. Notes
ID: 3601003 • Letter: I
Question
In LISP, code the functions listed below and use the specified test cases.
Notes:
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).
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)
(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))
Explanation / Answer
(defun repeat(atm N)
(cond
( (> N 0) (cons atm (repeat atm (- N 1))) )
)
)
(print (repeat 'Y '5))
(defun duplicate(lis)
(COND
( (NULL lis) NIL )
( (ATOM (CAR lis)) ( cons (CAR lis) ( cons (CAR lis) (duplicate (CDR lis)) ) ) )
( T (cons (CAR lis) (duplicate (CDR lis))) )
)
)
(print (duplicate '(X Y Z)))
(print (duplicate ' (GUM (HAS BEEN) CHEWED)))
(defun duplicateN(lis N)
(COND
((NULL lis) NIL)
((AND (ATOM lis) (ZEROP N)) NIL)
((AND (ATOM lis) (> N 0)) (cons lis (duplicateN lis (- N 1))))
((AND (ATOM (CAR lis)) (> N 1)) (append (duplicateN (CAR lis) N) (duplicateN (CDR lis) N)))
(T (cons (CAR lis) (duplicateN (CDR lis) N)))
)
)
;;(trace duplicateN)
(print (duplicateN '(A B C) 3))
(print (duplicateN ' (GUM (HAS BEEN) CHEWED) 4) )
(defun duplicateDeep(lis)
(COND
( (NULL lis) NIL )
( (ATOM (CAR lis)) ( cons (CAR lis) ( cons (CAR lis) (duplicateDeep (CDR lis)) ) ) )
( T (cons (duplicateDeep (CAR lis)) (duplicateDeep (CDR lis))) )
)
)
(print (duplicateDeep ' (A B C) ))
(print (duplicateDeep ' (A (B D) E (F)) ) )
(print (duplicateDeep '(A (B (D E) (F G)) (H I)) ) )
(defun printWOP(lis)
(terpri)
(COND
( (ATOM lis) (PRINC lis))
( T (printList lis) )
)
(terpri)
T
)
(defun printList(lis)
(COND
( (NULL (CDR lis)) (PRINT (CAR lis) ) )
)
(COND
( )
)
)
(defun evalEach(lis)
; (print lis)
; (print (CAR lis))
; (EVAL (CAR lis))
(COND
( ( NOT (NULL (CDR lis))) (EVAL (CAR lis)) )
)
(COND
( (NULL (CDR lis)) (EVAL (CAR lis)) )
( T (evalEach (CDR lis)) )
)
)
;(trace evalEach)
(print (evalEach '( (setf A 5) (print 'hello) (print 'there) A)))
(print (evalEach '( (setf x 10 ) (setf A '(x y z)) (print A) (setf B (car A)) (set B (+ 5 x)) )))
(print B)
(print X)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.