Type boolean-power.

This commit is contained in:
Sergiu Ivanov 2022-03-05 00:14:38 +01:00
parent f5c762b6d8
commit 347e17158d
2 changed files with 20 additions and 11 deletions

View File

@ -469,3 +469,12 @@ values of different types.
]}
@section{Boolean operations}
@defproc[(boolean-power [n Integer])
(Listof (Listof Boolean))]{
Returns the @racket[n]-th Cartesian power of the Boolean domain.
@examples[#:eval utils-evaluator
(boolean-power 2)
]}

View File

@ -25,7 +25,7 @@
update-vertices/unweighted update-graph dotit collect-by-key
collect-by-key/sets ht-values/list->set hash->list/ordered
multi-split-at lists-transpose in-random cartesian-product-2/stream
cartesian-product/stream)
cartesian-product/stream boolean-power)
(define-type Variable Symbol)
(define-type (VariableMapping A) (Immutable-HashTable Variable A))
@ -526,6 +526,14 @@
(2 4 b)
(2 5 a)
(2 5 b)))))
(: boolean-power (-> Integer (Listof (Listof Boolean))))
(define (boolean-power n)
(apply cartesian-product (make-list n '(#f #t))))
(module+ test
(test-case "boolean-power"
(check-equal? (boolean-power 2) '((#f #f) (#f #t) (#t #f) (#t #t)))))
)
(require 'typed)
@ -537,14 +545,13 @@
update-vertices/unweighted update-graph dotit collect-by-key
collect-by-key/sets ht-values/list->set hash->list/ordered
multi-split-at lists-transpose in-random cartesian-product-2/stream
cartesian-product/stream)
cartesian-product/stream boolean-power)
;;; Untyped section.
(provide
;; Functions
(contract-out [boolean-power (-> number? (listof (listof boolean?)))]
[boolean-power/stream (-> number? (stream/c (listof boolean?)))]
(contract-out [boolean-power/stream (-> number? (stream/c (listof boolean?)))]
[any->01 (-> any/c (or/c 0 1))]
[01->boolean (-> (or/c 0 1) boolean?)])
;; Contracts
@ -571,13 +578,6 @@
;;; Boolean operations
;;; ==================
;;; Returns the n-th Cartesian power of the Boolean domain: {0,1}^n.
(define (boolean-power n) (apply cartesian-product (make-list n '(#f #t))))
(module+ test
(test-case "boolean-power"
(check-equal? (boolean-power 2) '((#f #f) (#f #t) (#t #f) (#t #t)))))
;;; Like boolean-power, but returns a stream whose elements the
;;; elements of the Cartesian power.
(define (boolean-power/stream n) (cartesian-product/stream (make-list n '(#f #t))))