functions: Only have one tabulate.

I used to have a distinction between tabulate and
tabulate/domain-list, but this made no sense, so tabulate/domain-list
becomes the only true tabulate.
This commit is contained in:
Sergiu Ivanov 2020-05-31 00:13:50 +02:00
parent e7b5a3931a
commit ece53ea522
1 changed files with 13 additions and 22 deletions

View File

@ -12,9 +12,8 @@
(provide
;; Functions
(contract-out
[tabulate/domain-list (-> procedure? (listof generic-set?) (listof list?))]
[tabulate*/domain-list (-> (listof procedure?) (listof generic-set?) (listof list?))]
[tabulate (->* (procedure?) () #:rest (listof generic-set?) (listof list?))]
[tabulate (-> procedure? (listof generic-set?) (listof list?))]
[tabulate* (-> (listof procedure?) (listof generic-set?) (listof list?))]
[tabulate/boolean (-> procedure-fixed-arity? (listof (listof boolean?)))]
[table->function (-> (listof (*list/c any/c any/c)) procedure?)]
[table->function/list (-> (listof (*list/c any/c any/c)) procedure?)]
@ -36,40 +35,32 @@
;;; Given a function and a list of domains for each of its arguments,
;;; in order, produces a list of lists giving the values of arguments
;;; and the value of the functions for these inputs.
(define (tabulate/domain-list func doms)
(tabulate*/domain-list `(,func) doms))
(define (tabulate func doms)
(tabulate* `(,func) doms))
(module+ test
(test-case "tabulate/domain-list"
(check-equal? (tabulate/domain-list (λ (x y) (and x y)) '((#f #t) (#f #t)))
(test-case "tabulate"
(check-equal? (tabulate (λ (x y) (and x y)) '((#f #t) (#f #t)))
'((#f #f #f) (#f #t #f) (#t #f #f) (#t #t #t)))))
;;; Like tabulate/domain-list, but takes a list of functions taking
;;; Like tabulate, but takes a list of functions taking
;;; the same arguments over the same domains.
(define (tabulate*/domain-list funcs doms)
(define (tabulate* funcs doms)
(for/list ([xs (apply cartesian-product doms)])
(append xs (for/list ([f funcs]) (apply f xs)))))
(module+ test
(test-case "tabulate*/domain-list"
(check-equal? (tabulate*/domain-list (list (λ (x y) (and x y))
(λ (x y) (or x y)))
'((#f #t) (#f #t)))
(test-case "tabulate*"
(check-equal? (tabulate* (list (λ (x y) (and x y))
(λ (x y) (or x y)))
'((#f #t) (#f #t)))
'((#f #f #f #f) (#f #t #f #t) (#t #f #f #t) (#t #t #t #t)))))
;;; Like tabulate, but the domains are given as a rest argument.
(define (tabulate func . doms) (tabulate/domain-list func doms))
(module+ test
(test-case "tabulate"
(check-equal? (tabulate (λ (x y) (and x y)) '(#f #t) '(#f #t))
'((#f #f #f) (#f #t #f) (#t #f #f) (#t #t #t)))))
;;; Like tabulate, but assumes the domains of all variables of the
;;; function are Boolean. func must have a fixed arity. It is an
;;; error to supply a function of variable arity.
(define (tabulate/boolean func)
(tabulate/domain-list func (make-list (procedure-arity func) '(#f #t))))
(tabulate func (make-list (procedure-arity func) '(#f #t))))
(module+ test
(test-case "tabulate/boolean"