I have been stuck on this for a while. i have tried and I am not getting it. I thought I was getting it, but the fact that this isn’t working is confusing. I should due getting 1 but keep getting nil. The purpose is to simplify the expression using the rules(which I added below). My problem:
(defun simplify (main-list)
(setq count 1)
(if (and (eq t (atom (car (cdr main-list))))
(eq t (atom (car (cdr (cdr main-list))))))
(print "this says that the 2 ands are cons cells"))
(if (and (eq nil (cdr main-list))
(eq t (atom (car main-list))))
(print "reached end of file, going back now"))
(if (eq 'and (car main-list))
(progn
(if (and (eq t (atom (car (cdr main-list))))
(eq nil (atom (car (cdr (cdr main-list))))))
(if (or (eq nil (car (cdr main-list)))
(simplify (car (cdr (cdr main-list)))))
nil
(if (eq 1 (car (cdr main-list)))
(simplify (car (cdr (cdr main-list))))
(if (eq 1 (simplify (car (cdr (cdr main-list)))))))))
(if (and (eq t (atom (car (cdr main-list))))
(eq t (atom (car (cdr (cdr main-list))))))
(if (or (eq nil (car (cdr main-list)))
(eq nil (car (cdr (cdr main-list)))))
nil
(if (eq 1 (car (cdr main-list)))
(car (cdr (cdr main-list)))
(if (eq 1 (car (cdr (cdr main-list))))
(car (cdr main-list)))))))))
The list I am using is:
(and 1 (and 1 1))
This is a simple version of what I am trying to accomplish, but I am tackling it a step at a time since I am completely new to the language. These are the rules for the AND I am suppose to follow for this Homework:
(and x nil) => nil;
(and nil x) => nil;
(and x 1) => x;
(and 1 x) => x;
I have tested it by doing
(simplify (car(cdr(cdr x))))
and I added counts to see if its even looping, but it is not. So my guess it has something to do with the recursive function calls that are within the if statements in the first block of code. Any explanations as to why would be greatly appreciated.
1 Answer