From d9734a5a35940836027c02331aed4ba5d5537c5c Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Thu, 21 Apr 2022 15:00:50 +0200 Subject: [PATCH] Type list->tbf. --- functions.rkt | 27 +++++++++++++-------------- scribblings/functions.scrbl | 9 +++++++++ 2 files changed, 22 insertions(+), 14 deletions(-) diff --git a/functions.rkt b/functions.rkt index b4ecc38..abf37f5 100644 --- a/functions.rkt +++ b/functions.rkt @@ -27,7 +27,8 @@ enumerate-boolean-functions/pv enumerate-boolean-functions/list random-boolean-table random-boolean-function random-boolean-function/list - (struct-out tbf) tbf-w tbf-θ boolean->01/vector apply-tbf apply-tbf/boolean) + (struct-out tbf) tbf-w tbf-θ boolean->01/vector apply-tbf apply-tbf/boolean + list->tbf) (module+ test (require typed/rackunit)) @@ -394,6 +395,15 @@ (check-equal? (tabulate/pv/boolean 2 (pvλ (x y) (apply-tbf/boolean f1 (vector x y)))) '((#f #f #f) (#f #t #f) (#t #f #t) (#t #t #f))))) + (: list->tbf (-> (Listof Real) tbf)) + (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)))) + (module untyped racket (module+ test (require rackunit)) @@ -480,7 +490,8 @@ enumerate-boolean-functions/pv enumerate-boolean-functions/list random-boolean-table random-boolean-function random-boolean-function/list - (struct-out tbf) tbf-w tbf-θ boolean->01/vector apply-tbf apply-tbf/boolean) + (struct-out tbf) tbf-w tbf-θ boolean->01/vector apply-tbf apply-tbf/boolean + list->tbf) (require (rename-in (submod 'typed untyped) [tabulate tabulate/untyped] @@ -489,7 +500,6 @@ (provide ;; Functions (contract-out - [list->tbf (-> (cons/c number? (cons/c number? (listof number?))) tbf?)] [lists->tbfs (-> (listof (listof number?)) (listof tbf?))] [read-org-tbfs (->* (string?) (#:headers boolean?) (listof tbf?))] [tbf-tabulate* (-> (listof tbf?) (listof (listof (or/c 0 1))))] @@ -510,17 +520,6 @@ ;;; Threshold Boolean functions ;;; =========================== -;;; 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)))) - ;;; Reads a list of TBF from an Org-mode table read by ;;; read-org-sexp. (define lists->tbfs ((curry map) list->tbf)) diff --git a/scribblings/functions.scrbl b/scribblings/functions.scrbl index 6c0bfa4..3e6f900 100644 --- a/scribblings/functions.scrbl +++ b/scribblings/functions.scrbl @@ -415,6 +415,15 @@ a Boolean value. (tabulate/pv/boolean 2 (pvλ (x y) (apply-tbf/boolean simple-tbf (vector x y)))) ]} +@defproc[(list->tbf [lst (Listof Real)]) tbf]{ + +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. + +@examples[#:eval functions-evaluator +(list->tbf '(1 2 3)) +]} + @section[#:tag "fuctions/untyped"]{Untyped definitions} @defmodule[(submod dds/functions typed untyped)]