Challenge Problems about
Do the problems below, starting in a fresh ACL2 session that starts with
the standard
Finally, you need not use ACL2 to prove that your
Our answers to the problems in this section are in community-books
file
Sample Question:
(defun symbol-to-integer-alistp (x) (declare (xargs :guard t)) (if (atom x) (equal x nil) (and (consp (car x)) (symbolp (caar x)) (integerp (cdar x)) (symbol-to-integer-alistp (cdr x))))) (defun sum-vals (alist) (declare (xargs :guard (symbol-to-integer-alistp alist))) (cond ((endp alist) 0) (t (+ (cdar alist) (sum-vals (cdr alist))))))
E.g.,
Define
Sample Solution:
(defun sum-vals-loop$ (alist) (declare (xargs :guard (symbol-to-integer-alistp alist))) (loop$ for pair in alist sum :guard (and (consp pair) (integerp (cdr pair))) (cdr pair)))
The
Verification of Sample Solution:
(defthm sum-vals-loop$-is-sum-vals (equal (sum-vals-loop$ alist) (sum-vals alist)) :rule-classes nil)
It is conceivable in some Common Lisps that the following would compile into more efficient code.
(defun sum-vals-loop$ (alist) (declare (xargs :guard (symbol-to-integer-alistp alist))) (loop$ for pair of-type cons in alist sum :guard (integerp (cdr pair)) (the integer (cdr pair))))
The thinking is that the
LP8-1 Define
If you want a slightly more challenging problem, omit the
You can always convert an “
LP8-2 The recursive function below is in the ACL2
sources (therefore, you will not have to define it in your session). Define
an equivalent function, named
(defun arglistp1 (lst) (declare (xargs :guard t)) (cond ((atom lst) (null lst)) (t (and (legal-variablep (car lst)) (arglistp1 (cdr lst))))))
Why is
LP8-3 The two recursive functions below are used in the ACL2
sources (thus, you won't have to define them in your session). Define
(defun atom-listp (lst) (declare (xargs :guard t :mode :logic)) (cond ((atom lst) (eq lst nil)) (t (and (atom (car lst)) (atom-listp (cdr lst)))))) (defun packn1 (lst) (declare (xargs :guard (atom-listp lst))) (cond ((endp lst) nil) (t (append (explode-atom (car lst) 10) (packn1 (cdr lst))))))——————————
LP8-4 Define
(defun select-corresponding-element (e lst1 lst2) (declare (xargs :guard (and (true-listp lst1) (true-listp lst2) (not (member nil lst2))))) (cond ((endp lst1) nil) ((endp lst2) nil) ((equal e (car lst1)) (car lst2)) (t (select-corresponding-element e (cdr lst1) (cdr lst2)))))
For example,
(select-corresponding-element 'wednesday '(sunday monday tueday wednesday thursday friday saturday) '(dimanche lundi mardi mercredi jeudi vendredi samedi)) = 'MERCREDI——————————
LP8-5 Define
(defun same-mod-wildcard (lst1 lst2) (declare (xargs :guard (and (true-listp lst1) (true-listp lst2) (equal (len lst1) (len lst2))))) (cond ((endp lst1) t) ((or (eq (car lst1) '*) (eq (car lst2) '*)) (same-mod-wildcard (cdr lst1) (cdr lst2))) ((equal (car lst1) (car lst2)) (same-mod-wildcard (cdr lst1) (cdr lst2))) (t nil)))
For example,
(same-mod-wildcard '(a * c d *) '(a x c * d)) = T——————————
LP8-6 The following function is part of the ACL2 source code, so you don't have to define it in your session.
(defun getprops1 (alist) (declare (xargs :guard (true-list-listp alist))) (cond ((endp alist) nil) ((or (null (cdar alist)) (eq (car (cdar alist)) *acl2-property-unbound*)) (getprops1 (cdr alist))) (t (cons (cons (caar alist) (cadar alist)) (getprops1 (cdr alist))))))
Define
Now go to lp-section-9 (or return to the