From 21b17cfd59e19bc4d17625b5477d6503d9d32842 Mon Sep 17 00:00:00 2001 From: koba-e964 Date: Mon, 17 Mar 2014 10:03:29 +0900 Subject: [PATCH 1/2] [bigfix] circular-list? Fix a bug in circular-list? --- piclib/srfi/1.scm | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/piclib/srfi/1.scm b/piclib/srfi/1.scm index 13ada184..1c3010f6 100644 --- a/piclib/srfi/1.scm +++ b/piclib/srfi/1.scm @@ -55,7 +55,7 @@ (define (circular-list? x) (and (pair? x) (let rec ((lst (cdr x))) - (cond ((not-pair?) #f) + (cond ((not-pair? lst) #f) ((null? lst) #f) ((eq? x lst) #t) (else (rec (cdr lst))))))) From dfb525157eb7cfe35bd51878ee511c431395a6d1 Mon Sep 17 00:00:00 2001 From: koba-e964 Date: Mon, 17 Mar 2014 10:43:39 +0900 Subject: [PATCH 2/2] [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)