[racket] Question about string conversion

From: Greg Hendershott (greghendershott at gmail.com)
Date: Thu Jul 18 10:01:14 EDT 2013

To add to what Carl and Robby said:

On Thu, Jul 18, 2013 at 2:29 AM,  <m0nastic at tengulabs.com> wrote:
> (although truth be told, mail headers are surprisingly nonstandard even
> within a single message)

That's where net/head could definitely help. (Especially for SMTP
headers, which tend to be more "interesting" than typical HTTP
headers.)

> I eventually discovered that I could sort of cheat, by just wrapping the
> regexp-match function with a car (which worked, because this particular
> list only had one element), and then it was usable from then on (and
> validated true from "string?").

It's worth walking through what Carl described. Actually you're lucky,
you have a specific example of something you want to do, which touches
on a few basic aspects of Racket.

Having said that, one pattern I've settled into using with regexps is
to use `match`, which makes it convenient to "de-structure" the list.
For example:

(match "From: Me"
  [(pregexp "^(.+):\\s*(.+)$" (list all key val))
   (displayln all)   ; From: Me
   (displayln key)   ; From
   (displayln val)]) ; Me

If you don't need the "all" part, you can supply _ like so:

(match "From: Me"
  [(pregexp "^(.+):\\s*(.+)$" (list _ key val))
   (displayln key)
   (displayln val)])

Which is the pattern I use a lot.

Again, you probably want to use net/head to deal with the gory details
of SMTP headers as Robby suggested, and it would definitely be good to
walk through the explorations Carl described.

Posted on the users mailing list.