TOC of the series
As a final step, we are going to refactor a bit the prior code to show other useful Racket features.
First, we can generalize the exported functions to take any input in general and not only strings. This can be useful for clients that, for instance, prefer to pass as argument a text file.
The client then would call
find-isbn-list as follows (or in a similar way):
(with-input-from-file a-file-name isbn-find/list)
but the client could still pass strings with
(with-input-from-string a-string isbn-find/list)
with-input functions are provided by
Another commonly used feature is defining functions with optional arguments and/or with named (aka. keyword) arguments.
In the current refactoring the
find functions can receive one or two optional arguments, one of them named. Note that contracts for this kind of functions are special: they use the construct
For more information about function definitions with optional or keyword arguments see Functions (Procedures) [The Racket Guide].
Finally, as a cosmetic touch, I have refactored the two last helpers
isbn-normalize to show a programming style called point-free style that you may find sometimes in many functional programming languages. In this style arguments are implicit and everything on the surface is just functions and function composition. To write in this way we need
racket/function as well as
compose1, a variant of
compose for functions returning single values.
Next article in the series: Racket: exception handling, http requests, JSON