A). Write a function NEW-LIST that takes a number as its argument and constructs
ID: 639867 • Letter: A
Question
A). Write a function NEW-LIST that takes a number as its argument and constructs a list of that length containing all Ts. For example,
-> (new-lit 5)
-> (T T T T T)
B). The Lisp function LENGTH counts the number of elements in the top level of a list. Write a function ALL-LENGTH of one argument that counts the number of atoms that occur in a list at all levels. Thus, the following lists will have the following ALL-LENGTHs.
(A B C) => 3
((A (B C) D (E F)) => 6
(NIL NIL (NIL NIL) NIL ) => 5
Explanation / Answer
(A)
Online URL for testing : http://ideone.com/u5Qhna
(defun NEW-LIST(max)
(let ((result nil))
(dotimes (i max)
(push 'T result))
(nreverse result)))
(print (NEW-LIST 5))
(print (NEW-LIST 10))
Output:
(B)
Online URL for testing : http://ideone.com/YL1WhQ
(defun ALL-LENGTH (exp &optional (if-null 1))
(cond ((null exp) if-null)
((atom exp) 1)
(t (+ (ALL-LENGTH (first exp) 1)
(ALL-LENGTH (rest exp) 0)))))
(print
( ALL-LENGTH '(A B C)
)
)
(print
( ALL-LENGTH '((A (B C) D (E F)))
)
)
(print
( ALL-LENGTH '(NIL NIL (NIL NIL) NIL )
)
)
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.