Conditional based on if-then-else
; Example 1. The form (COND ((CONSP X) (FOO X Y)) ((SYMBOLP X) (BAR X Y)) (T (LIST X Y))) ; abbreviates the following. (IF (CONSP X) (FOO X Y) (IF (SYMBOLP X) (BAR X Y) (LIST X Y))) ; Example 2. The form (COND ((CONSP X)) ((SYMBOLP X) (BAR X Y))) ; abbreviates the following. (OR (CONSP X) (IF (SYMBOLP X) (BAR X Y) NIL))
The results above were obtained by typing
Macro:
(defmacro cond (&rest clauses) (declare (xargs :guard (cond-clausesp clauses))) (cond-macro clauses))
Function:
(defun cond-macro (clauses) (declare (xargs :guard (cond-clausesp clauses))) (if (consp clauses) (if (and (eq (car (car clauses)) t) (eq (cdr clauses) nil)) (if (cdr (car clauses)) (car (cdr (car clauses))) (car (car clauses))) (if (cdr (car clauses)) (list 'if (car (car clauses)) (car (cdr (car clauses))) (cond-macro (cdr clauses))) (list 'or (car (car clauses)) (cond-macro (cdr clauses))))) nil))