Simplified the EQUAL function logic
This commit is contained in:
parent
d2dfe23083
commit
544df91c27
|
@ -1,7 +1,6 @@
|
|||
package function.builtin.predicate;
|
||||
|
||||
import function.*;
|
||||
import function.builtin.cons.LIST;
|
||||
import sexpression.*;
|
||||
|
||||
@FunctionNames({ "EQUAL" })
|
||||
|
@ -17,10 +16,6 @@ public class EQUAL extends LispFunction {
|
|||
public SExpression call(Cons argumentList) {
|
||||
argumentValidator.validate(argumentList);
|
||||
|
||||
return callRecursive(argumentList);
|
||||
}
|
||||
|
||||
private SExpression callRecursive(Cons argumentList) {
|
||||
Cons rest = (Cons) argumentList.getRest();
|
||||
SExpression firstArgument = argumentList.getFirst();
|
||||
SExpression secondArgument = rest.getFirst();
|
||||
|
@ -29,43 +24,6 @@ public class EQUAL extends LispFunction {
|
|||
}
|
||||
|
||||
private SExpression equal(SExpression firstArgument, SExpression secondArgument) {
|
||||
if (isListPair(firstArgument, secondArgument))
|
||||
return listEqual(firstArgument, secondArgument);
|
||||
|
||||
return atomEqual(firstArgument, secondArgument);
|
||||
}
|
||||
|
||||
private boolean isListPair(SExpression firstArgument, SExpression secondArgument) {
|
||||
return firstArgument.isCons() && secondArgument.isCons();
|
||||
}
|
||||
|
||||
private SExpression listEqual(SExpression firstArgument, SExpression secondArgument) {
|
||||
Cons listOne = (Cons) firstArgument;
|
||||
Cons listTwo = (Cons) secondArgument;
|
||||
SExpression listOneFirst = listOne.getFirst();
|
||||
SExpression listTwoFirst = listTwo.getFirst();
|
||||
SExpression listOneRest = listOne.getRest();
|
||||
SExpression listTwoRest = listTwo.getRest();
|
||||
|
||||
SExpression firstEqual = callRecursive(makeArgumentList(listOneFirst, listTwoFirst));
|
||||
SExpression restEqual = callRecursive(makeArgumentList(listOneRest, listTwoRest));
|
||||
|
||||
return logicalConjunction(firstEqual, restEqual);
|
||||
}
|
||||
|
||||
private Cons makeArgumentList(SExpression one, SExpression two) {
|
||||
return new Cons(one, LIST.makeList(two));
|
||||
}
|
||||
|
||||
private SExpression logicalConjunction(SExpression firstEqual, SExpression restEqual) {
|
||||
return bothAreTrue(firstEqual, restEqual) ? Symbol.T : Nil.getInstance();
|
||||
}
|
||||
|
||||
private boolean bothAreTrue(SExpression firstEqual, SExpression restEqual) {
|
||||
return (firstEqual == Symbol.T) && (restEqual == Symbol.T);
|
||||
}
|
||||
|
||||
private SExpression atomEqual(SExpression firstArgument, SExpression secondArgument) {
|
||||
return isEqual(firstArgument, secondArgument) ? Symbol.T : Nil.getInstance();
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue