From cd8cada92e4d9f807787e466b0685951f630b54d Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Thu, 25 May 2023 15:19:25 +0200 Subject: [PATCH] Add sbfs/state->lists+headers. --- scribblings/tbn.scrbl | 11 +++++++++++ tbn.rkt | 19 ++++++++++++++++++- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/scribblings/tbn.scrbl b/scribblings/tbn.scrbl index 464cb88..8d9f341 100644 --- a/scribblings/tbn.scrbl +++ b/scribblings/tbn.scrbl @@ -245,6 +245,17 @@ whether it is actually 0: (sbfs/state->lists (list (tbf/state (hash 'a 1 'b 2) 3))) ]} +@defproc[(sbfs/state->lists+headers [sbfs (Listof TBF/State)]) + (Pairof (Listof Variable) (Listof (Listof Real)))]{ + +Like @racket[sbfs/state->lists], but also shows the names of the +variables as column headers. + +@ex[ +(sbfs/state->lists+headers (list (tbf/state (hash 'a 1 'b 2) 0) + (tbf/state (hash 'a -2 'b 1) 0))) +]} + @section{Tabulating TBFs and SBFs} @defproc[(tabulate-tbfs/state [tbfs (Listof TBF/State)]) (Listof (Listof Real))]{ diff --git a/tbn.rkt b/tbn.rkt index 3ea212d..ea78049 100644 --- a/tbn.rkt +++ b/tbn.rkt @@ -29,7 +29,7 @@ lists+vars->sbfs/state lists+headers->sbfs/state lists->sbfs/state read-org-tbfs/state read-org-tbfs/state+headers tbfs/state->lists tbfs/state->lists+headers - sbfs/state->lists + sbfs/state->lists sbfs/state->lists+headers tabulate-tbfs/state tabulate-tbfs/state+headers tabulate-tbf/state tabulate-tbf/state+headers @@ -234,6 +234,23 @@ (tbf/state (hash 'a -2 'b 1) 0))) '((1 2) (-2 1))))) + (: sbfs/state->lists+headers (-> (Listof TBF/State) + (Pairof (Listof Variable) + (Listof (Listof Real))))) + (define (sbfs/state->lists+headers tbfs) + (cons (hash-map (tbf/state-w (car tbfs)) + (λ ([x : Symbol] _) x) #t) + (sbfs/state->lists tbfs))) + + (module+ test + (test-case "sbfs/state->list+headers" + (check-equal? + (sbfs/state->lists+headers (list (tbf/state (hash 'a 1 'b 2) 0) + (tbf/state (hash 'a -2 'b 1) 0))) + '((a b) + (1 2) + (-2 1))))) + (: tabulate-tbfs/state (-> (Listof TBF/State) (Listof (Listof Real)))) (define (tabulate-tbfs/state tbfs) (define vars (hash-map (tbf/state-w (car tbfs)) (λ ([x : Variable] _) x) #t))