From: Paul Snively <psnively@mac.com>
To: Stephane Glondu <Stephane.Glondu@crans.org>
Cc: caml-list@yquem.inria.fr
Subject: Re: [Caml-list] Question re: camlp4 parser
Date: Tue, 26 Jul 2005 09:43:28 -0700 [thread overview]
Message-ID: <0C774C67-0FD2-49F3-9293-B81BB3A0991E@mac.com> (raw)
In-Reply-To: <42E58F35.8070204@crans.org>
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hello, Stephane!
On Jul 25, 2005, at 6:17 PM, Stephane Glondu wrote:
>
> The inferred type should have given you a warning:
> --> val printable : char Stream.t -> 'a = <fun>
>
> In other word, your function never returns a correct value.
>
Excellent point.
> Try this:
>
> let printable s =
> let buf = Buffer.create 100 in
> let rec aux = parser
> [< '' '..'~' as c; x = (Buffer.add_char buf c; aux) >] -> x
> | [< >] -> Buffer.contents buf
> in aux s ;;
> --> val printable : char Stream.t -> string = <fun>
>
> printable (Stream.of_string "Test!\013") ;;
> --> - : string = "Test!"
>
> Notice that you cannot remove the occurrences of "s" (even though it
> would have the same type) if you are planning to use this function
> several times.
>
Thanks, this is exactly the kind of thing I was hoping for! So the
key points are:
1) Use the | and an empty alternative pattern to capture the "no more
matches" case.
2) Use "as" and take advantage of expression sequencing to accumulate
the matches into a variable (Buffer, in this case).
That makes perfect sense and now seems obvious. :-)
One hopefully final question: is there a convenient shorthand for
saying something like "all printable characters except '=' or '['?" I
assume not--that is, we have ranges (' '..'~') or we have variants
('A' | 'B' | 'C'...) and that's it. I'm somewhat spoiled, I think, by
Spirit in C++, and its notion of "character sets" and operations on
them, so I can say, e.g. "print_p - '='" that that will match all
printable characters other than '='.
>
>> Many thanks and best regards,
>>
>
> You're welcome.
>
Thanks again,
> --
>
> Stephane Glondu.
>
> _______________________________________________
> Caml-list mailing list. Subscription management:
> http://yquem.inria.fr/cgi-bin/mailman/listinfo/caml-list
> Archives: http://caml.inria.fr
> Beginner's list: http://groups.yahoo.com/group/ocaml_beginners
> Bug reports: http://caml.inria.fr/bin/caml-bugs
>
Paul Snively
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (Darwin)
iEYEARECAAYFAkLmaEAACgkQO3fYpochAqIc5QCeOaHzKj+bTBOObRMisOSzdyO7
RrkAoKkWokql0JuuFvLUeelr5NgTNsgg
=IFMX
-----END PGP SIGNATURE-----
next prev parent reply other threads:[~2005-07-26 16:43 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2005-07-26 0:11 Paul Snively
2005-07-26 1:17 ` [Caml-list] " Stephane Glondu
2005-07-26 16:43 ` Paul Snively [this message]
2005-07-26 17:05 ` Stephane Glondu
2005-07-27 7:04 ` Virgile Prevosto
2005-07-28 1:27 ` Paul Snively
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=0C774C67-0FD2-49F3-9293-B81BB3A0991E@mac.com \
--to=psnively@mac.com \
--cc=Stephane.Glondu@crans.org \
--cc=caml-list@yquem.inria.fr \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox