|
|
@ -1,14 +1,16 @@
|
|
|
|
(module util mzscheme
|
|
|
|
#lang racket
|
|
|
|
(require mzlib/list)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(provide (all-defined))
|
|
|
|
(provide (all-defined-out))
|
|
|
|
|
|
|
|
|
|
|
|
(define max-char-num #x10FFFF)
|
|
|
|
(define max-char-num #x10FFFF)
|
|
|
|
|
|
|
|
|
|
|
|
(define-struct lex-abbrev (get-abbrev))
|
|
|
|
(define-struct lex-abbrev (get-abbrev))
|
|
|
|
(define-struct lex-trans (f))
|
|
|
|
(define-struct lex-trans (f))
|
|
|
|
|
|
|
|
|
|
|
|
#;(define-syntax test-block
|
|
|
|
(module+ test
|
|
|
|
|
|
|
|
(require rackunit))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#;(define-syntax test-block
|
|
|
|
(syntax-rules ()
|
|
|
|
(syntax-rules ()
|
|
|
|
((_ defs (code right-ans) ...)
|
|
|
|
((_ defs (code right-ans) ...)
|
|
|
|
(let* defs
|
|
|
|
(let* defs
|
|
|
@ -17,56 +19,62 @@
|
|
|
|
(printf "Test failed: ~e gave ~e. Expected ~e\n"
|
|
|
|
(printf "Test failed: ~e gave ~e. Expected ~e\n"
|
|
|
|
'code real-ans 'right-ans))) ...))))
|
|
|
|
'code real-ans 'right-ans))) ...))))
|
|
|
|
|
|
|
|
|
|
|
|
(define-syntax test-block
|
|
|
|
(define-syntax test-block
|
|
|
|
(syntax-rules ()
|
|
|
|
(syntax-rules ()
|
|
|
|
((_ x ...) (void))))
|
|
|
|
((_ x ...) (void))))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; A cache is (X ( -> Y) -> Y)
|
|
|
|
;; A cache is (X ( -> Y) -> Y)
|
|
|
|
;; make-cache : -> cache
|
|
|
|
;; make-cache : -> cache
|
|
|
|
;; table map Xs to Ys. If key is mapped, its value is returned.
|
|
|
|
;; table map Xs to Ys. If key is mapped, its value is returned.
|
|
|
|
;; Otherwise, build is invoked and its result is placed in the table and
|
|
|
|
;; Otherwise, build is invoked and its result is placed in the table and
|
|
|
|
;; returned.
|
|
|
|
;; returned.
|
|
|
|
;; Xs are compared with equal?
|
|
|
|
;; Xs are compared with equal?
|
|
|
|
(define (make-cache)
|
|
|
|
(define (make-cache)
|
|
|
|
(let ((table (make-hash-table 'equal)))
|
|
|
|
(let ((table (make-hash)))
|
|
|
|
(lambda (key build)
|
|
|
|
(lambda (key build)
|
|
|
|
(hash-table-get table key
|
|
|
|
(hash-ref table key
|
|
|
|
(lambda ()
|
|
|
|
(lambda ()
|
|
|
|
(let ((new (build)))
|
|
|
|
(let ((new (build)))
|
|
|
|
(hash-table-put! table key new)
|
|
|
|
(hash-set! table key new)
|
|
|
|
new))))))
|
|
|
|
new))))))
|
|
|
|
(test-block ((cache (make-cache)))
|
|
|
|
|
|
|
|
((cache '(s 1 2) (lambda () 9)) 9)
|
|
|
|
(module+ test
|
|
|
|
((cache '(s 2 1) (lambda () 8)) 8)
|
|
|
|
(define cache (make-cache))
|
|
|
|
((cache '(s 1 2) (lambda () 1)) 9)
|
|
|
|
(check-equal? (cache '(s 1 2) (lambda () 9)) 9)
|
|
|
|
((cache (cons 's (cons 0 (cons +inf.0 10))) (lambda () 22)) 22)
|
|
|
|
(check-equal? (cache '(s 2 1) (lambda () 8)) 8)
|
|
|
|
((cache (cons 's (cons 0 (cons +inf.0 10))) (lambda () 1)) 22))
|
|
|
|
(check-equal? (cache '(s 1 2) (lambda () 1)) 9)
|
|
|
|
|
|
|
|
(check-equal? (cache (cons 's (cons 0 (cons +inf.0 10)))
|
|
|
|
|
|
|
|
(lambda () 22)) 22)
|
|
|
|
|
|
|
|
(check-equal? (cache (cons 's (cons 0 (cons +inf.0 10)))
|
|
|
|
|
|
|
|
(lambda () 1)) 22))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; make-counter : -> -> nat
|
|
|
|
;; make-counter : -> -> nat
|
|
|
|
;; makes a function that returns a higher number by 1, each time
|
|
|
|
;; makes a function that returns a higher number by 1, each time
|
|
|
|
;; it is called.
|
|
|
|
;; it is called.
|
|
|
|
(define (make-counter)
|
|
|
|
(define (make-counter)
|
|
|
|
(let ((counter 0))
|
|
|
|
(let ((counter 0))
|
|
|
|
(lambda ()
|
|
|
|
(lambda ()
|
|
|
|
(begin0
|
|
|
|
(begin0
|
|
|
|
counter
|
|
|
|
counter
|
|
|
|
(set! counter (add1 counter))))))
|
|
|
|
(set! counter (add1 counter))))))
|
|
|
|
(test-block ((c (make-counter))
|
|
|
|
|
|
|
|
(d (make-counter)))
|
|
|
|
(module+ test
|
|
|
|
((c) 0)
|
|
|
|
(define c (make-counter))
|
|
|
|
((d) 0)
|
|
|
|
(define d (make-counter))
|
|
|
|
((c) 1)
|
|
|
|
(check-equal? (c) 0)
|
|
|
|
((d) 1)
|
|
|
|
(check-equal? (d) 0)
|
|
|
|
((c) 2))
|
|
|
|
(check-equal? (c) 1)
|
|
|
|
|
|
|
|
(check-equal? (d) 1)
|
|
|
|
|
|
|
|
(check-equal? (c) 2))
|
|
|
|
;; remove-dups : (list-of X) (X -> number) -> (list-of X)
|
|
|
|
|
|
|
|
;; removes the entries from l that have the same index as a
|
|
|
|
|
|
|
|
;; previous entry. l must be grouped by indexes.
|
|
|
|
;; remove-dups : (list-of X) (X -> number) -> (list-of X)
|
|
|
|
(define (remove-dups l index acc)
|
|
|
|
;; removes the entries from l that have the same index as a
|
|
|
|
|
|
|
|
;; previous entry. l must be grouped by indexes.
|
|
|
|
|
|
|
|
(define (remove-dups l index acc)
|
|
|
|
(cond
|
|
|
|
(cond
|
|
|
|
((null? l) (reverse acc))
|
|
|
|
((null? l) (reverse acc))
|
|
|
|
((null? acc) (remove-dups (cdr l) index (cons (car l) acc)))
|
|
|
|
((null? acc) (remove-dups (cdr l) index (cons (car l) acc)))
|
|
|
@ -75,42 +83,45 @@
|
|
|
|
(else
|
|
|
|
(else
|
|
|
|
(remove-dups (cdr l) index (cons (car l) acc)))))
|
|
|
|
(remove-dups (cdr l) index (cons (car l) acc)))))
|
|
|
|
|
|
|
|
|
|
|
|
(test-block ()
|
|
|
|
|
|
|
|
((remove-dups '((1 2) (2 2) (1 3) (1 4) (100 4) (0 5)) cadr null)
|
|
|
|
|
|
|
|
'((1 2) (1 3) (1 4) (0 5)))
|
|
|
|
|
|
|
|
((remove-dups null error null) null))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
(module+ test
|
|
|
|
|
|
|
|
(check-equal? (remove-dups '((1 2) (2 2) (1 3) (1 4)
|
|
|
|
|
|
|
|
(100 4) (0 5)) cadr null)
|
|
|
|
|
|
|
|
'((1 2) (1 3) (1 4) (0 5)))
|
|
|
|
|
|
|
|
(check-equal? (remove-dups null error null) null))
|
|
|
|
|
|
|
|
|
|
|
|
;; do-simple-equiv : (list-of X) (X -> nat) -> (list-of X)
|
|
|
|
;; do-simple-equiv : (list-of X) (X -> nat) -> (list-of X)
|
|
|
|
;; Sorts l according to index and removes the entries with duplicate
|
|
|
|
;; Sorts l according to index and removes the entries with duplicate
|
|
|
|
;; indexes.
|
|
|
|
;; indexes.
|
|
|
|
(define (do-simple-equiv l index)
|
|
|
|
(define (do-simple-equiv l index)
|
|
|
|
(let ((ordered (sort l (lambda (a b) (< (index a) (index b))))))
|
|
|
|
(let ((ordered (sort l (lambda (a b) (< (index a) (index b))))))
|
|
|
|
(remove-dups ordered index null)))
|
|
|
|
(remove-dups ordered index null)))
|
|
|
|
|
|
|
|
|
|
|
|
(test-block ()
|
|
|
|
(module+ test
|
|
|
|
((do-simple-equiv '((2 2) (1 4) (1 2) (100 4) (1 3) (0 5)) cadr)
|
|
|
|
(check-equal? (do-simple-equiv '((2 2) (1 4) (1 2)
|
|
|
|
|
|
|
|
(100 4) (1 3) (0 5))
|
|
|
|
|
|
|
|
cadr)
|
|
|
|
'((2 2) (1 3) (1 4) (0 5)))
|
|
|
|
'((2 2) (1 3) (1 4) (0 5)))
|
|
|
|
((do-simple-equiv null error) null))
|
|
|
|
(check-equal? (do-simple-equiv null error) null))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
;; replace : (list-of X) (X -> bool) (X -> (list-of X)) (list-of X) ->
|
|
|
|
;; replace : (list-of X) (X -> bool) (X -> (list-of X)) (list-of X) ->
|
|
|
|
;; (list-of X)
|
|
|
|
;; (list-of X)
|
|
|
|
;; If (pred? r) for some r in l, splice (get r) in place of r in the resulting
|
|
|
|
;; If (pred? r) for some r in l, splice (get r) in place of r in the resulting
|
|
|
|
;; list.
|
|
|
|
;; list.
|
|
|
|
(define (replace l pred? get acc)
|
|
|
|
(define (replace l pred? get acc)
|
|
|
|
(cond
|
|
|
|
(cond
|
|
|
|
((null? l) acc)
|
|
|
|
((null? l) acc)
|
|
|
|
((pred? (car l)) (replace (cdr l) pred? get (append (get (car l)) acc)))
|
|
|
|
((pred? (car l)) (replace (cdr l) pred? get (append (get (car l)) acc)))
|
|
|
|
(else (replace (cdr l) pred? get (cons (car l) acc)))))
|
|
|
|
(else (replace (cdr l) pred? get (cons (car l) acc)))))
|
|
|
|
|
|
|
|
|
|
|
|
(test-block ()
|
|
|
|
|
|
|
|
((replace null void (lambda () (list 1)) null) null)
|
|
|
|
(module+ test
|
|
|
|
((replace '(1 2 3 4 3 5)
|
|
|
|
(check-equal? (replace null void (lambda () (list 1)) null) null)
|
|
|
|
|
|
|
|
(check-equal? (replace '(1 2 3 4 3 5)
|
|
|
|
(lambda (x) (= x 3))
|
|
|
|
(lambda (x) (= x 3))
|
|
|
|
(lambda (x) (list 1 2 3))
|
|
|
|
(lambda (x) (list 1 2 3))
|
|
|
|
null)
|
|
|
|
null)
|
|
|
|
'(5 1 2 3 4 1 2 3 2 1)))
|
|
|
|
'(5 1 2 3 4 1 2 3 2 1)))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|