upscheme/scheme-tests/torus.scm

48 lines
1.2 KiB
Scheme
Raw Normal View History

(define (maplist f l)
(if (null? l) ()
2008-06-30 21:54:22 -04:00
(cons (f l) (maplist f (cdr l)))))
; produce a beautiful, toroidal cons structure
; make m copies of a CDR-circular list of length n, and connect corresponding
; conses in CAR-circular loops
; replace maplist 'identity' with 'copy-tree' for rapdily exploding memory use
(define (torus m n)
2008-06-30 21:54:22 -04:00
(let* ((l (map-int identity n))
(g l)
(prev g))
(dotimes (i (- m 1))
(set! prev g)
(set! g (maplist identity g))
(set-cdr! (last-pair prev) prev))
(set-cdr! (last-pair g) g)
2008-06-30 21:54:22 -04:00
(let ((a l)
(b g))
(dotimes (i n)
(set-car! a b)
(set! a (cdr a))
(set! b (cdr b))))
2008-06-30 21:54:22 -04:00
l))
(define (cyl m n)
2008-06-30 21:54:22 -04:00
(let* ((l (map-int identity n))
(g l))
(dotimes (i (- m 1))
(set! g (maplist identity g)))
2008-06-30 21:54:22 -04:00
(let ((a l)
(b g))
(dotimes (i n)
(set-car! a b)
(set! a (cdr a))
(set! b (cdr b))))
2008-06-30 21:54:22 -04:00
l))
(time (begin (write (torus 100 100)) ()))
;(time (dotimes (i 1) (load "100x100.scm")))
2008-06-30 21:54:22 -04:00
; with ltable
; printing time: 0.415sec
; reading time: 0.165sec
; with ptrhash
; printing time: 0.081sec
; reading time: 0.0264sec