From dd3062652f713445d34735fb357435e16231f7cc Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Tue, 7 Jul 2020 23:29:38 +0200 Subject: [PATCH] functions: Add list->tbf. --- functions.rkt | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/functions.rkt b/functions.rkt index a39e09d..5639e2b 100644 --- a/functions.rkt +++ b/functions.rkt @@ -33,7 +33,8 @@ [tbf-θ (-> tbf? number?)] [vector-boolean->01 (-> (vectorof boolean?) (vectorof (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 (require rackunit)) @@ -278,3 +279,14 @@ (define f1 (tbf #(2 -2) 1)) (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))))) + +;;; 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))))