From dfb525157eb7cfe35bd51878ee511c431395a6d1 Mon Sep 17 00:00:00 2001 From: koba-e964 Date: Mon, 17 Mar 2014 10:43:39 +0900 Subject: [PATCH] [bugfix] circular-list? in srfi-1 circular-list? does not get stuck in an infinite loop if it is given a list such as '(1 2 3 2 3 2 3 2 3 ...). --- piclib/srfi/1.scm | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/piclib/srfi/1.scm b/piclib/srfi/1.scm index 1c3010f6..935b4138 100644 --- a/piclib/srfi/1.scm +++ b/piclib/srfi/1.scm @@ -51,19 +51,16 @@ ;; list= (define (not-pair? x) (not (pair? x))) - + ;; detects circular list using Floyd's cycle-finding algorithm (define (circular-list? x) - (and (pair? x) - (let rec ((lst (cdr x))) - (cond ((not-pair? lst) #f) - ((null? lst) #f) - ((eq? x lst) #t) - (else (rec (cdr lst))))))) + (let rec ((rapid x) (local x)) + (if (and (pair? rapid) (pair? (cdr rapid))) + (if (eq? (cddr rapid) (cdr local)) + #t + (rec (cddr rapid) (cdr local))) + #f))) - ;; if list? is support circular list, (define proper-list? list?) - (define (proper-list? x) - (if (not (circular-list? x)) - (list? x))) + (define proper-list? list?) (define (dotted-list? x) (and (pair? x)