Isn't it true that the scanf format string is just a poor man's regular
expression pattern (i.e. just character classes) that however also allows you
to specify how to convert matches? It may be a succinct notation, but it is
also misleading (for example, a single space in the format string matches any
amount of whitespace in the input!?). And it is inflexible: What if I create a
new data structure? The implementation will not provide for a conversion.
I don't think there is need for a c-like scanf in racket. Why not just use
regexp-match and do something like this:
(define (regexp-match-format pattern input . conversion-procs)
(define reports (rest (regexp-match pattern input)))
(for/list ([r reports]
[c conversion-procs])
(c r)))
And use it like this:
(regexp-match-format #rx"\\[(.+), *(.+), *(.+)\\]"
(read-line)
string->number
values
string->number)
Am Mittwoch, 23. März 2016 05:06:18 UTC+1 schrieb Alexis King:
> Honestly, judging from the responses in this thread, it would seem there
> may be a hole in Racket’s toolbox. Nothing I’ve seen so far is
> particularly stellar, especially since this is a problem that does not
> seem like it should be hard.
>
> It may be overkill for this use case, but I would probably use the
> parsack package, since I think its interface is pretty intuitive.
> However, it would be nice to have some sort of inverse to printf that
> parses values given a template string. Racket has a very good set of
> tools for formatting text, but it seems that doing the inverse is much
> harder, which seems to be against Racket’s “batteries included”
> philosophy.
>
> Would anyone object to a scanf-like function in Racket itself? 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.
--
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 [email protected].
For more options, visit https://groups.google.com/d/optout.