From 1f52342a554308ac4813e1ad9a556daed819c553 Mon Sep 17 00:00:00 2001 From: Matthew Butterick Date: Sat, 26 Dec 2020 18:12:16 -0800 Subject: [PATCH] add `make-var-names` helper --- csp/csp/hacs.rkt | 5 +++++ csp/csp/scribblings/csp.scrbl | 14 ++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/csp/csp/hacs.rkt b/csp/csp/hacs.rkt index c0e8bbe1..c9192c9a 100644 --- a/csp/csp/hacs.rkt +++ b/csp/csp/hacs.rkt @@ -80,6 +80,11 @@ ((name?) ((listof any/c)) . ->* . var?) (var name (list->set vals))) +(define/contract (make-var-names prefix vals [suffix ""]) + ((string? (listof any/c)) ((string?)) . ->* . (listof name?)) + (for/list ([val (in-list vals)]) + (string->symbol (format "~a~a~a" prefix val suffix)))) + (define/contract (add-vars! prob names [vals-or-procedure empty]) ((csp? (listof name?)) ((or/c (listof any/c) procedure?)) . ->* . void?) (for/fold ([vrs (vars prob)] diff --git a/csp/csp/scribblings/csp.scrbl b/csp/csp/scribblings/csp.scrbl index 050f0fe1..205ad640 100644 --- a/csp/csp/scribblings/csp.scrbl +++ b/csp/csp/scribblings/csp.scrbl @@ -309,6 +309,20 @@ Which would become: This is better, but also overkill, because if @racket[(< a b)] and @racket[(< b c)], then by transitivity, @racket[(< a c)] is necessarily true. So this is a case where pairwise expands into more constraints than we actually need. This will not produce any wrong solutions, but especially on larger lists of variables, it creates unnecessary work that my slow down the solution search. } +@defproc[(make-var-names +[prefix string?] +[vals (listof any/c)] +[suffix string? ""]) +(listof symbol?)]{ +Helper function to generate mass quantities of variable names. The @racket[_prefix] and (optional) @racket[_suffix] strings are wrapped around each value in @racket[_vals], and converted to a symbol. + +@my-examples[ +(make-var-names "foo" (range 6) "bar") +(make-var-names "col" (range 10)) +] + +} + @defproc[(solve [prob csp?] ) (or/c #false (listof (cons/c symbol? any/c)))]{