-
Notifications
You must be signed in to change notification settings - Fork 8
/
util.rkt
44 lines (38 loc) · 907 Bytes
/
util.rkt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#lang racket/base
(require
racket/contract
unstable/contract)
(provide
(contract-out
(list*/c (-> contract? contract?))
(non-empty-list*/c (-> contract? contract?))
(power-of-two? predicate/c)))
(define (list*/c ctc)
(cond
((flat-contract? ctc)
(flat-rec-contract rec
(cons/c ctc rec)
(list/c (listof ctc))))
((chaperone-contract? ctc)
(define rec
(recursive-contract
(or/c (cons/c ctc rec)
(cons/c (listof ctc) null))
#:chaperone))
rec)
(else
(define rec
(recursive-contract
(or/c (cons/c ctc rec)
(cons/c (listof ctc) null))))
rec)))
(define (non-empty-list*/c ctc)
(or/c
(cons/c ctc (list*/c ctc))
(list/c (non-empty-listof ctc))))
(define (power-of-two? v)
(and (exact-positive-integer? v)
(let loop ((w 1))
(if (>= w v)
(= w v)
(loop (* 2 w))))))