Initial data commit
This commit is contained in:
parent
72d218235f
commit
f23f22d71c
199087 changed files with 3378941 additions and 0 deletions
29
Task/History-variables/Racket/history-variables.rkt
Normal file
29
Task/History-variables/Racket/history-variables.rkt
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
#lang racket
|
||||
|
||||
(require rackunit)
|
||||
|
||||
(struct hvar (current history) #:mutable)
|
||||
|
||||
(define (make-hvar v) (hvar v empty))
|
||||
|
||||
(define (hvar-set! hv new)
|
||||
(match-define (hvar cur hist) hv)
|
||||
(set-hvar-history! hv (cons cur hist))
|
||||
(set-hvar-current! hv new))
|
||||
|
||||
(define (hvar-undo! hv)
|
||||
(match-define (hvar cur (cons old hist)) hv)
|
||||
(set-hvar-current! hv old)
|
||||
(set-hvar-history! hv hist))
|
||||
|
||||
;; unit tests
|
||||
(define hv (make-hvar 0))
|
||||
(hvar-set! hv 1)
|
||||
(check-equal? (hvar-current hv) 1)
|
||||
(hvar-set! hv 2)
|
||||
(hvar-set! hv 3)
|
||||
(check-equal? (hvar-history hv) '(2 1 0))
|
||||
(hvar-undo! hv)
|
||||
(hvar-undo! hv)
|
||||
(hvar-undo! hv)
|
||||
(check-equal? (hvar-current hv) 0)
|
||||
Loading…
Add table
Add a link
Reference in a new issue