diff --git a/scribblings/tbn.scrbl b/scribblings/tbn.scrbl index 0a44ee2..3738e5a 100644 --- a/scribblings/tbn.scrbl +++ b/scribblings/tbn.scrbl @@ -174,3 +174,29 @@ the thresholds are set to 0. (lists+headers->sbfs/state '((x y) (1 2) (1 1))) (lists->sbfs/state '((1 2) (1 1))) ]} + +@defproc[(read-org-tbfs/state [str String]) (Listof TBF/State)]{ + +Reads a list of @racket[TBF/State] from an Org-mode string containing +a sexp, containing a list of lists of numbers. As in +@racket[lists->tbfs/state], the last element of each list is taken to +be the threshold of the TBF, and the rest of the elements are taken to +be the weights. + +Similarly to @racket[lists->tbfs/state], the names of the variables +are generated as @tt{xi}, where @italic{i} is the index of the +variable, starting from 0. + +@ex[ +(read-org-tbfs/state "((1 2 3) (1 1 2))") +]} + +@defproc[(read-org-tbfs/state+headers [str String]) (Listof TBF/State)]{ + +Like @racket[read-org-tbfs/state], but the first list in @racket[str] +is taken to contain the names of the variables, similarly to +@racket[lists+headers->tbfs/state]. + +@ex[ +(read-org-tbfs/state+headers "((a b f) (1 2 3) (1 1 2))") +]} diff --git a/tbn.rkt b/tbn.rkt index 143181a..4e95d2b 100644 --- a/tbn.rkt +++ b/tbn.rkt @@ -27,6 +27,7 @@ lists+vars->tbfs/state lists+headers->tbfs/state lists->tbfs/state lists+vars->sbfs/state lists+headers->sbfs/state lists->sbfs/state + read-org-tbfs/state read-org-tbfs/state+headers ) (: apply-tbf-to-state (-> TBF (State (U Zero One)) (U Zero One))) @@ -156,6 +157,30 @@ (list (tbf/state '#hash((x0 . 1) (x1 . 2)) 0) (tbf/state '#hash((x0 . 1) (x1 . 1)) 0))))) + + (: read-org-tbfs/state (-> String (Listof TBF/State))) + (define (read-org-tbfs/state str) + (lists->tbfs/state + (assert-type (read-org-sexp str) + (Listof (Listof Real))))) + + (module+ test + (test-case "read-org-tbfs/state" + (check-equal? (read-org-tbfs/state "((1 2 3) (1 1 2))") + (list (tbf/state '#hash((x0 . 1) (x1 . 2)) 3) + (tbf/state '#hash((x0 . 1) (x1 . 1)) 2))))) + + (: read-org-tbfs/state+headers (-> String (Listof TBF/State))) + (define (read-org-tbfs/state+headers str) + (lists+headers->tbfs/state + (assert-type (read-org-sexp str) + (Pairof (Listof Variable) (Listof (Listof Real)))))) + + (module+ test + (test-case "read-org-tbfs/state+headers" + (check-equal? (read-org-tbfs/state+headers "((a b f) (1 2 3) (1 1 2))") + (list (tbf/state '#hash((a . 1) (b . 2)) 3) + (tbf/state '#hash((a . 1) (b . 1)) 2))))) ) (module+ test