cycle lab4
This commit is contained in:
parent
9b1367ae80
commit
fe11450530
30
Labs/04.rkt
30
Labs/04.rkt
@ -3,6 +3,7 @@
|
|||||||
[MPair (n : Number) (tail : (Boxof MList))]
|
[MPair (n : Number) (tail : (Boxof MList))]
|
||||||
[Empty])
|
[Empty])
|
||||||
|
|
||||||
|
; make
|
||||||
(define (mlist lst)
|
(define (mlist lst)
|
||||||
(cond
|
(cond
|
||||||
[(empty? lst) (Empty)]
|
[(empty? lst) (Empty)]
|
||||||
@ -13,6 +14,7 @@
|
|||||||
(test (mlist '(1)) (MPair 1 (box (Empty))))
|
(test (mlist '(1)) (MPair 1 (box (Empty))))
|
||||||
(test (mlist '(1 2)) (MPair 1 (box (MPair 2 (box (Empty))))))
|
(test (mlist '(1 2)) (MPair 1 (box (MPair 2 (box (Empty))))))
|
||||||
|
|
||||||
|
; take
|
||||||
(define (take k mlst)
|
(define (take k mlst)
|
||||||
(cond
|
(cond
|
||||||
[(<= k 0) empty]
|
[(<= k 0) empty]
|
||||||
@ -22,18 +24,20 @@
|
|||||||
[(MPair n tail-box)
|
[(MPair n tail-box)
|
||||||
(cons n (take (sub1 k) (unbox tail-box)))])]))
|
(cons n (take (sub1 k) (unbox tail-box)))])]))
|
||||||
|
|
||||||
|
; big
|
||||||
(define big-mlist
|
(define big-mlist
|
||||||
(mlist (build-list 50 identity)))
|
(mlist (build-list 50 identity)))
|
||||||
|
|
||||||
(test (take 10 big-mlist) '(0 1 2 3 4 5 6 7 8 9))
|
(test (take 10 big-mlist) '(0 1 2 3 4 5 6 7 8 9))
|
||||||
|
|
||||||
|
; set
|
||||||
(define (set-last! lst1 lst2)
|
(define (set-last! lst1 lst2)
|
||||||
(type-case MList lst1
|
(type-case MList lst1
|
||||||
[(MPair n t)
|
[(MPair n t)
|
||||||
(type-case MList (unbox t)
|
(type-case MList (unbox t)
|
||||||
[(Empty) (set-box! t lst2)]
|
[(Empty) (set-box! t lst2)]
|
||||||
[(MPair n t) (set-last! (unbox t) lst2)])]
|
[else (set-last! (unbox t) lst2)])]
|
||||||
[(Empty) (void)]))
|
[(Empty) (error 'empty "cannot set tail")]))
|
||||||
|
|
||||||
(define test-lst1 (mlist '(1 2 3)))
|
(define test-lst1 (mlist '(1 2 3)))
|
||||||
(define test-lst2 (mlist '(4 5 6)))
|
(define test-lst2 (mlist '(4 5 6)))
|
||||||
@ -43,11 +47,17 @@
|
|||||||
(test (take 1000 test-lst1) '(1 2 3 4 5 6))
|
(test (take 1000 test-lst1) '(1 2 3 4 5 6))
|
||||||
(test/exn (set-last! (Empty) test-lst1) "cannot set tail")
|
(test/exn (set-last! (Empty) test-lst1) "cannot set tail")
|
||||||
|
|
||||||
;(define small-cycle (cycle (mlist '(0 1 2))))
|
; cycle
|
||||||
;(define big-cycle (cycle big-mlist))
|
(define (cycle lst)
|
||||||
;(test (cycle (Empty)) (Empty))
|
(type-case MList lst
|
||||||
;(test (take 0 big-cycle) empty)
|
[(MPair n t) (begin (set-last! lst lst) lst)]
|
||||||
;(test (take 0 small-cycle) empty)
|
[(Empty) (Empty)]))
|
||||||
;(test (take 5 big-cycle) '(0 1 2 3 4))
|
|
||||||
;(test (take 5 small-cycle) '(0 1 2 0 1))
|
(define small-cycle (cycle (mlist '(0 1 2))))
|
||||||
;(test (take 107 big-cycle) (build-list 107 (lambda (n) (modulo n 50))))
|
(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))))
|
Loading…
x
Reference in New Issue
Block a user