|
| From: | Elf |
| Subject: | Re: [Chicken-users] Cyclic lists and the Interpreter |
| Date: | Tue, 8 Apr 2008 09:01:23 -0700 (PDT) |
readline control-c does work. if its not catching it its because your system is thrashing.
according to srfi-1, length is not defined for circular lists. if you want a circular-list length, you need something like...
;; this assumes that it circles at the beginning:
(define (length* l)
(cond ((list? l)
(length l))
((circular-list? l)
(let loop ((t (cdr l))
(c 1))
(if (eq? t l)
c
(loop (cdr l) (+ 1 c)))))
(else
(##sys#signal-hook #:type-error 'length*
"argument is not a list or circular-list"
l))))
its much more expensive to check if it circles at any point:
(define (length* l)
(cond ((list? l)
(length l))
((circular-list? l)
(let loop ((t (cdr l))
(p (list l))
(c 1))
(if (memq t p)
c
(loop (cdr l) (cons t p) (+ 1 c)))))
(else
(##sys#signal-hook #:type-error 'length*
"argument is not a list or circular-list"
l))))
why this works:
eq? (and memq) test for pointer equality. circular lists are circular because
some cdr points to a previous pair.
-elf
On Mon, 7 Apr 2008, Alex Rozenshteyn wrote:
(define a (list 'a)) (set-cdr! a a) ;a is now a cyclic "list" (pair? a) ; -> #t (list? a) ; -> #f (length a) and now the interpreter gets stuck. control-c does not break (this is because I have the readline egg installed). I have two questions: 1) How do I get C-c to break? 2) Is the interpreter supposed to freeze if length is passed a cyclic list? Other scheme implementations generate an error.
| [Prev in Thread] | Current Thread | [Next in Thread] |