Use local* variables, if any (arguments of functions will sometimes suffice); us
ID: 3745049 • Letter: U
Question
Use local* variables, if any (arguments of functions will sometimes suffice); usually local variables are introduced with (let (varl vr2 ...) body-of -code) or with a prog. Avoid assignment statements using setq or setf when nested function application (quite possibly including recursion) will do just as well. However, sometimes it's clearer to save intermediate values with a setq or setf If a function is intended for arguments of certain restricted types, include type checking in defining the function; i.e., if the user gives an argument which is not of the specified type, your function should print an error message (using the 'format' macro) and then return NIL or another value indicating failure 1. Define a recursive function in the manner (defun proper-list (input) ...) which returns T if !nput' is a proper list, i.e., of form (ell el2 elk), where the elements are any symbolic expressions, and returns NIL (-the empty list, ()) otherwise. For example, (proper-list nil) ==> T (proper-list 'nil) >T (proper-list 'this) --> NIL (proper-list '(this and that)) T (proper-list (cons 'A ))> NIL NOTE: (cons 'A B)> (A B), and (listp "(A. B)), so the Common Lisp 'listp' is not equivalent to 'proper-list'!}Explanation / Answer
(defun proper-list(x)
(or (null x) (and (consp x) (proper-list (cdr x))
)
)
)
(print (proper-list nil))
(print (proper-list 'nil))
(print (proper-list 'this))
(print (proper-list '(this and that)))
(print (proper-list (cons 'A 'B)))
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.