Created
September 24, 2017 19:04
-
-
Save joeltg/0e5b0ab21595fe2dbb98d165e7c98fde to your computer and use it in GitHub Desktop.
Circular list utilities for MIT Scheme
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(define (make-initialized-circular-list k procedure) | |
(let ((circle (make-initialized-list k procedure))) | |
(set-cdr! (last-pair circle) circle) | |
circle)) | |
(define (circular-list-length circle) | |
(if (null? circle) | |
0 | |
(let iter ((c (cdr circle)) (i 1)) | |
(if (eq? c circle) | |
i | |
(if (pair? c) | |
(iter (cdr c) (+ i 1)) | |
(error:wrong-type-datum circle "circular list")))))) | |
(define (circular-list-map procedure first . rest) | |
(assert (not (any null? rest))) | |
(if (null? first) | |
'() | |
(let iter ((circle (list (apply f (car first) (map car rest)))) | |
(circles (cons (cdr first) (map cdr rest)))) | |
(if (eq? (car circles) first) | |
circle | |
(if (pair? (car circles)) | |
(iter (cons (apply f (map car circles))) (map cdr circles)) | |
(error:wrong-type-datum circle "circular list")))))) | |
(define (circular-list->list circle) | |
(list-head circle (circular-list-length circle))) | |
(define (list->circular-list list) | |
(apply circular-list list)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment