On Tue, 22 Mar 2016 21:06:14 -0700, Alexis King
<lexi.lam...@gmail.com> wrote:

>Would anyone object to a scanf-like function in Racket itself? 

No.  

But the question is how to specify input patterns to make it really
useful.  Racket's native printf is pretty basic.  Should scanf mirror
the native printf?  Or should it mirror something like SRFI-48 format,
which has more functionality?  

Or should it work with predicates that mirror racket/format output
functions?  

Or, in general, should it accept any suitable predicate?  
[e.g., string->number]


>The
>obvious point of difficulty is how to handle errors, given that parsing
>can fail but printing cannot. Should it throw an exception, or should it
>return #f? I’m not sure there is great precedent set here, though
>throwing seems to be the Racket way in other places.

Printing can fail both for formatting and I/O errors.

C's scanf returns the count of patterns matched (in order from the
beginning of the string), but I don't recall offhand whether that
includes patterns that use '*' to suppress assignment to an argument.
C's scanf also allows to limit expected field widths.

And an I/O error does not lose matches that preceded the error.  Any
arguments that were bound to values before the error occurred retain
their values.

Not that a Racket scanf necessarily has to work anything like C's.


Returning multiple values presents a problem if there is a mistake in
the input unless you have a distinct *unbound* value for unmatched
patterns (which #f is not).  

I suppose #<void> works for an unbound indicator, but parsing
functions are expected to fail due to mistakes in the input. Returning
(values ...) from such functions ranks rather high on my offense meter
because I see little value to expecting, e.g., 10 return values and
getting back 8 of them set to #<void>.  And returning too few values
results in an program error.

Perhaps the best thing would be just to return a list of the
successful matches.  I would only throw I/O errors, not match failures
... similar to the way regex-match et al work.


George

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to racket-users+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to