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