A (relatively) frequently asked question on comp.lang.lisp is "What's the equivalent to scanf()?". The usual answer is "There isn't one, because it's too hard to work out what should happen". Which is fair enough.
However, one year Christophe was bored during exams, so he wrote format-setf.lisp, which may do what you want.
It should be pointed out that currently the behaviour of this program is unspecified, in more senses than just the clobbering of symbols in the "CL" package. What would be nice would be to see a specification appear for its behaviour, so that I don't have an excuse when people say that it's buggy.
Quick assessment (2023-03-24)
- Tested with CCL, MKCL, and Allegro CL.
- Appears to work for the most obvious things.
- Includes atof.cl from the CMU AI Repository.
- Allegro CL balks because of a package lock.
For example:
correctly parses the string into x and y, whereas
fails because we used the wrong format directive.
Portability notes (2024-01-21)
There's a comment in the code asking why the definition of whitespacep (from atof.cl) has an eval-when around it. The reason is that some Lisps—CCL, at least—already have a function of that name, so there's an (unless (fboundp 'whitespacep) ...) around it. Because of this, the defining form is not a top level form. The equivalent function in the parse-float system is called whitespace-char-p.
An observation about the last line of the file—the defsetf associated with format-setf—is that it assumes the short-form defsetf will provide the simplest possible expansion. This is because format-setf is a macro that expects to receive literal arguments without evaluating them. In LispWorks, for example, macroexpand-1 gives:
But npt, on the other hand, does a lot of renaming to gensyms:
Which won't work. The workaround is to get rid of the defsetf and manage the expansion ourselves:
So the result from get-setf-expansion is:
The expander looks long-winded, but all it's doing is combining the setf expansions of its arguments.
Text