TOC of the series
The next step involves a substantial transformation and the first touch on a prominent Racket feature: contracts.
When you create code that may be used as a library by other client code, you probably don't want to make public everything there. It is more likely that only certain parts are written for client use while the rest is only for the implementation. In order to specify the functions, predicates or whatever you want to export for public use, you have to add an initial section with
provide that states and documents from the very beginning the public interface.
Furthermore, you surely wish to clearly manifest what your exported functions expect as input and what they guarantee as the type of the output. Signatures are for that, but signatures don't receive actual checking of any kind. Contracts are precisely what you wish for. So in code that you write for others include contracts about everything you are going to export.
Besides, you may wish to use contracts locally in all functions, including the private ones. In such a case you can use
In the code below the first part is now the
provide section with contracts for all exported functions. The rest of the functions are now defined with
define/contract just for more practicing on function contracts.
Finally, since predicates and contracts are sufficient documentation for the code at hand I have also removed the Data Types section in prior versions.
As an aside, a new require has been added in order to use the contract
sequence/c provided by
racket/sequence in the definition of
Next article in the series: Racket: I/O, optional and keyword arguments