IN LISP 1. Code the function (repeat atm N) which constructs a new list with atm
ID: 3600695 • Letter: I
Question
IN LISP
1. Code the function (repeat atm N) which constructs a new list with atm repeated N times. Example > (repeat 'A 5 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)) > (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) » (duplicateN' (A B C) 3) > (duplicateN GUM (HAS BEEN) CHEWED) 2) (A A B Bc C) (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 oeue wtnle D could be usefutExplanation / Answer
Solution:
code:
(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)
I hope this helps. Don't forget to give a thumbs up if you like this.