From 79a688a3e505155be9854168c5eeb83079f32489 Mon Sep 17 00:00:00 2001 From: Sergiu Ivanov Date: Thu, 21 Apr 2022 16:56:58 +0200 Subject: [PATCH] Type read-org-tbfs. --- functions.rkt | 32 +++++++++++++------------------- scribblings/functions.scrbl | 14 ++++++++++++++ 2 files changed, 27 insertions(+), 19 deletions(-) diff --git a/functions.rkt b/functions.rkt index bb441e5..d9abe29 100644 --- a/functions.rkt +++ b/functions.rkt @@ -28,7 +28,7 @@ random-boolean-table random-boolean-function random-boolean-function/list (struct-out tbf) tbf-w tbf-θ boolean->01/vector apply-tbf apply-tbf/boolean - list->tbf lists->tbfs) + list->tbf lists->tbfs read-org-tbfs) (module+ test (require typed/rackunit)) @@ -413,6 +413,17 @@ (check-equal? (lists->tbfs '((1 2 3) (2 3 4))) (list (tbf '#(1 2) 3) (tbf '#(2 3) 4))))) + (: read-org-tbfs (->* (String) (#:headers Boolean) (Listof tbf))) + (define (read-org-tbfs str #:headers [headers #f]) + (define sexp (cast (read-org-sexp str) (Listof Any))) + (define sexp-clean (cond [headers (cdr sexp)] [else sexp])) + (lists->tbfs (cast sexp-clean (Listof (Listof Real))))) + + (module+ test + (test-case "read-org-tbfs" + (check-equal? (read-org-tbfs "((1 2 1) (1 0 1))") + (list (tbf '#(1 2) 1) (tbf '#(1 0) 1))))) + (module untyped racket (module+ test (require rackunit)) @@ -500,7 +511,7 @@ random-boolean-table random-boolean-function random-boolean-function/list (struct-out tbf) tbf-w tbf-θ boolean->01/vector apply-tbf apply-tbf/boolean - list->tbf lists->tbfs) + list->tbf lists->tbfs read-org-tbfs) (require (rename-in (submod 'typed untyped) [tabulate tabulate/untyped] @@ -509,7 +520,6 @@ (provide ;; Functions (contract-out - [read-org-tbfs (->* (string?) (#:headers boolean?) (listof tbf?))] [tbf-tabulate* (-> (listof tbf?) (listof (listof (or/c 0 1))))] [tbf-tabulate (-> tbf? (listof (listof (or/c 0 1))))] [tbf-tabulate*/boolean (-> (listof tbf?) (listof (listof boolean?)))] @@ -528,22 +538,6 @@ ;;; Threshold Boolean functions ;;; =========================== -;;; Reads a list of TBF from an Org-mode string containing a sexp, -;;; containing a list of lists of numbers. If headers is #t, drops -;;; the first list, supposing that it contains the headers of the -;;; table. -;;; -;;; The input is typically what read-org-sexp reads. -(define (read-org-tbfs str #:headers [headers #f]) - (define sexp (read-org-sexp str)) - (define sexp-clean (cond [headers (cdr sexp)] [else sexp])) - (lists->tbfs sexp-clean)) - -(module+ test - (test-case "read-org-tbfs" - (check-equal? (read-org-tbfs "((1 2 1) (1 0 1))") - (list (tbf '#(1 2) 1) (tbf '#(1 0) 1))))) - ;;; Tabulates a list of TBFs. ;;; ;;; The result is a list of lists describing the truth table of the diff --git a/scribblings/functions.scrbl b/scribblings/functions.scrbl index 72ecb48..f7737d8 100644 --- a/scribblings/functions.scrbl +++ b/scribblings/functions.scrbl @@ -435,6 +435,20 @@ The main use is for reading TBFs from Org-mode tables read by (lists->tbfs '((1 2 3) (2 3 4))) ]} +@defproc[(read-org-tbfs [str String] [#:headers headers Boolean #f]) + (Listof tbf)]{ + +Reads a list of TBF from an Org-mode string containing a sexp, containing +a list of lists of numbers. If headers is @racket[#t], drops the first list, +supposing that it contains the headers of the table. + +The input is typically what @racket[read-org-sexp] reads. + +@examples[#:eval functions-evaluator +(read-org-tbfs "((1 2 1) (1 0 1))") +]} + + @section[#:tag "fuctions/untyped"]{Untyped definitions} @defmodule[(submod dds/functions typed untyped)]