functions: Add list->tbf.

This commit is contained in:
Sergiu Ivanov 2020-07-07 23:29:38 +02:00
parent db42e20f1c
commit dd3062652f

View file

@ -33,7 +33,8 @@
[tbf-θ (-> tbf? number?)] [tbf-θ (-> tbf? number?)]
[vector-boolean->01 (-> (vectorof boolean?) (vectorof (or/c 0 1)))] [vector-boolean->01 (-> (vectorof boolean?) (vectorof (or/c 0 1)))]
[apply-tbf (-> tbf? (vectorof (or/c 0 1)) (or/c 0 1))] [apply-tbf (-> tbf? (vectorof (or/c 0 1)) (or/c 0 1))]
[apply-tbf/boolean (-> tbf? (vectorof boolean?) boolean?)])) [apply-tbf/boolean (-> tbf? (vectorof boolean?) boolean?)]
[list->tbf (-> (cons/c number? (cons/c number? (listof number?))) tbf?)]))
(module+ test (module+ test
(require rackunit)) (require rackunit))
@ -278,3 +279,14 @@
(define f1 (tbf #(2 -2) 1)) (define f1 (tbf #(2 -2) 1))
(check-equal? (tabulate/boolean (λ (x y) (apply-tbf/boolean f1 (vector x y)))) (check-equal? (tabulate/boolean (λ (x y) (apply-tbf/boolean f1 (vector x y))))
'((#f #f #f) (#f #t #f) (#t #f #t) (#t #t #f))))) '((#f #f #f) (#f #t #f) (#t #f #t) (#t #t #f)))))
;;; Converts a list of numbers to a TBF. The last element of the list
;;; is taken to be the threshold, while the other elements are taken
;;; to be the weights.
(define (list->tbf lst)
(define-values (w θ) (split-at-right lst 1))
(tbf (list->vector w) (car θ)))
(module+ test
(test-case "list->tbf"
(check-equal? (list->tbf '(1 2 3)) (tbf #(1 2) 3))))