diff --git a/runtime.c b/runtime.c index 4f7c97d6..ab60d34b 100644 --- a/runtime.c +++ b/runtime.c @@ -880,13 +880,13 @@ object memqp(void *data, object x, list l) */ object equalp(object x, object y) { - object slow_lis = x, fast_lis; + object slow_lis = x, fast_lis = NULL; int second_cycle = 0; -// if (Cyc_is_pair(x) == boolean_t && -// Cyc_is_pair(cdr(x)) == boolean_t){ -// fast_lis = cdr(x); -// } + if (Cyc_is_pair(x) == boolean_t && + Cyc_is_pair(cdr(x)) == boolean_t){ + fast_lis = cdr(x); + } for (;; x = cdr(x), y = cdr(y)) { if (equal(x, y)) @@ -900,30 +900,31 @@ object equalp(object x, object y) if (boolean_f == equalp(car(x), car(y))) return boolean_f; -// // If there is no cycle, keep checking equality -// if (fast_lis == NULL || -// Cyc_is_pair(fast_lis) == boolean_f || -// cdr(fast_lis) == NULL || -// Cyc_is_pair(cdr(fast_lis)) == boolean_f || -// cddr(fast_lis) == NULL){ -// continue; -// } -// -// // If there is a cycle, handle it -// if (slow_lis == fast_lis) { -// // if this is y, both lists have cycles and are equal, return #t -// if (second_cycle) -// return boolean_t; -// // if this is x, keep going and check for a cycle in y -// second_cycle = 1; -// slow_lis = y; -// if (Cyc_is_pair(y) == boolean_t) { -// fast_lis = cdr(y); -// } -// continue; -// } -// slow_lis = cdr(slow_lis); -// fast_lis = cddr(fast_lis); + // If there is no cycle, keep checking equality + if (fast_lis == NULL || + Cyc_is_pair(fast_lis) == boolean_f || + cdr(fast_lis) == NULL || + Cyc_is_pair(cdr(fast_lis)) == boolean_f || + cddr(fast_lis) == NULL){ + continue; + } + + // If there is a cycle, handle it + if (slow_lis == fast_lis) { + // if this is y, both lists have cycles and are equal, return #t + if (second_cycle) + return boolean_t; + // if this is x, keep going and check for a cycle in y + second_cycle = 1; + slow_lis = y; + fast_lis = NULL; + if (Cyc_is_pair(y) == boolean_t) { + fast_lis = cdr(y); + } + continue; + } + slow_lis = cdr(slow_lis); + fast_lis = cddr(fast_lis); } }