<html><head><meta http-equiv="Content-Type" content="text/html charset=us-ascii"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;"><div>Though it is an off-label use of contracts, it can be done. I made something I call "coercion contracts" that mutate & validate the input (or output) argument as part of the contract:</div><div><br></div><div>(define/contract (function-that-wants-something-stringlike x)</div><div>    (coerce/string? . -> . coerce/boolean?)</div><div>    (do-things-to x))</div><div><div><br></div><div>Source code is here:</div><div><a href="https://github.com/mbutterick/sugar/blob/master/coerce/contract.rkt">https://github.com/mbutterick/sugar/blob/master/coerce/contract.rkt</a></div><div><br></div><div>Or do `raco pkg install sugar` and (require sugar/coerce/contract) </div><div><br></div><div><br></div><div>Please note: I am setting a terrible example.</div><div><br></div><div><br></div><div><br></div><br><blockquote type="cite"><blockquote type="cite">I feel like this has come up before, but I can't seem to find the<br>discussion in the archive. I have a procedure that needs to perform a<br>fairly expensive computation which is needed both for checking the<br>inputs and for producing output. I'd like to avoid computing it twice,<br>and I'd also like to use contracts to check the inputs. Is there any<br>reasonable way to do this?<br></blockquote><br><br>No. The contract system is designed so that the contractual <br>computations are in principle independent from the 'mutator'<br>computations. <br><br><br><blockquote type="cite">The code is like this:<br><br>(define (do-stuff x y z)<br> (define important-value (expensive x y z))<br> ... do stuff with important-value ...)<br><br>(provide/contract<br> [do-stuff (->i [x ...] [y ...] [z (x y)<br>contract-that-depends-on-important-value]) [result ...])])<br><br><br>I could memoize the expensive computation, but that would require<br>something like an LRU cache to avoid memory problems, and I'd rather<br>not have to worry about that.<br><br>Alternatively, I could shift things around so that the contract is on<br>the procedure that generates important-value, but then blame reporting<br>would be incorrect.<br><br>Any ideas?<br></blockquote><br><br>Define a syntactic abstraction that spans contracts and 'mutators'. <br><br>-- Matthias<br><br>____________________<br>  Racket Users list:<br>  <a href="http://lists.racket-lang.org/users">http://lists.racket-lang.org/users</a><br></blockquote></div><br></body></html>