From 347e17158d0438f85d42b79fa02d9b03b2c925b2 Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Sat, 5 Mar 2022 00:14:38 +0100 Subject: [PATCH] Type boolean-power. --- scribblings/utils.scrbl | 9 +++++++++ utils.rkt | 22 +++++++++++----------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/scribblings/utils.scrbl b/scribblings/utils.scrbl index 14b3512..f6ff7a6 100644 --- a/scribblings/utils.scrbl +++ b/scribblings/utils.scrbl @@ -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) +]} \ No newline at end of file diff --git a/utils.rkt b/utils.rkt index f007dbf..50c0633 100644 --- a/utils.rkt +++ b/utils.rkt @@ -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))))