cycle lab4

This commit is contained in:
Isaac Shoebottom 2025-02-05 00:57:23 -04:00
parent 9b1367ae80
commit fe11450530

View File

@ -3,6 +3,7 @@
[MPair (n : Number) (tail : (Boxof MList))]
[Empty])
; make
(define (mlist lst)
(cond
[(empty? lst) (Empty)]
@ -13,6 +14,7 @@
(test (mlist '(1)) (MPair 1 (box (Empty))))
(test (mlist '(1 2)) (MPair 1 (box (MPair 2 (box (Empty))))))
; take
(define (take k mlst)
(cond
[(<= k 0) empty]
@ -22,18 +24,20 @@
[(MPair n tail-box)
(cons n (take (sub1 k) (unbox tail-box)))])]))
; big
(define big-mlist
(mlist (build-list 50 identity)))
(test (take 10 big-mlist) '(0 1 2 3 4 5 6 7 8 9))
; set
(define (set-last! lst1 lst2)
(type-case MList lst1
[(MPair n t)
(type-case MList (unbox t)
[(Empty) (set-box! t lst2)]
[(MPair n t) (set-last! (unbox t) lst2)])]
[(Empty) (void)]))
[else (set-last! (unbox t) lst2)])]
[(Empty) (error 'empty "cannot set tail")]))
(define test-lst1 (mlist '(1 2 3)))
(define test-lst2 (mlist '(4 5 6)))
@ -43,11 +47,17 @@
(test (take 1000 test-lst1) '(1 2 3 4 5 6))
(test/exn (set-last! (Empty) test-lst1) "cannot set tail")
;(define small-cycle (cycle (mlist '(0 1 2))))
;(define big-cycle (cycle big-mlist))
;(test (cycle (Empty)) (Empty))
;(test (take 0 big-cycle) empty)
;(test (take 0 small-cycle) empty)
;(test (take 5 big-cycle) '(0 1 2 3 4))
;(test (take 5 small-cycle) '(0 1 2 0 1))
;(test (take 107 big-cycle) (build-list 107 (lambda (n) (modulo n 50))))
; cycle
(define (cycle lst)
(type-case MList lst
[(MPair n t) (begin (set-last! lst lst) lst)]
[(Empty) (Empty)]))
(define small-cycle (cycle (mlist '(0 1 2))))
(define big-cycle (cycle big-mlist))
(test (cycle (Empty)) (Empty))
(test (take 0 big-cycle) empty)
(test (take 0 small-cycle) empty)
(test (take 5 big-cycle) '(0 1 2 3 4))
(test (take 5 small-cycle) '(0 1 2 0 1))
(test (take 107 big-cycle) (build-list 107 (lambda (n) (modulo n 50))))