[racket] Question about string conversion

From: Carl Eastlund (cce at ccs.neu.edu)
Date: Thu Jul 18 03:25:13 EDT 2013


The regexp-match* function has a rather complex input/output specification,
because it is doing a lot of things at once.  If you only want the first
match of a regular expression, you might want to consider regexp-match
instead (no *); its specification is slightly simpler, though still a
little complex.

The car function, on the other hand, is very straightforward.  Given a
list, it extracts and returns the first element of that list.  The name
"car" is rather anachronistic; Racket also has a function called first that
does the same thing.  If it makes things clearer to you, use that one

If using functions like regexp-match* and car is giving you trouble in your
program, I suggest playing around with them in racket or DrRacket, outside
of any program.  Just apply regexp-match*, car, and any other functions you
want to learn more about, to different inputs and see what results you
get.  That should give you a clearer idea of how to put them together in
your program.  It will also give you a starting point for asking more
concrete questions about specific inputs and outputs the rest of us can
look at, try ourselves, and explain.

For instance, see what (regexp-match* "[abc]+" "banana") produces.  Also
see what (car (list 1 2 3)) produces.  Try string->list and list->string if
you're curious to see how they work, too.

When you've got a handle on how each function works individually, you can
get back to your program.  Write lots of tests for your program, and break
your program up into smaller "helper" functions, and write tests for them
too.  All these steps will make it much easier to see what each function is
doing, and if the output isn't what you expected it to be, these steps will
help you figure out where things went wrong and why.

Thanks for your question, feel free to ask more if you have them.  Good

Carl Eastlund

P.S. I know this isn't a simple "regexp-match* works like this" answer.
Your question is actually several questions -- what kind of output does
regexp-match* produce, how does one convert between lists and strings, how
does the function car work, and so forth.  I hope the process above will
help you work through some of the complexity and get to some simpler, more
direct questions that are easier to tackle.  Sorry if this feels like a bit
of a runaround, but I think you'll get a handle on it pretty quick!

On Thu, Jul 18, 2013 at 2:29 AM, <m0nastic at tengulabs.com> wrote:

> Hi, I'm super new to Racket, but had what I hope isn't too
> embarrassingly easy of a question.
> I'm writing a program that parses SMTP headers in order to automate some
> business-y workflow. I managed to put together a series of
> "regexp-match*"s to parse out the specific identifiers I was looking for
> (although truth be told, mail headers are surprisingly nonstandard even
> within a single message), and ran into an error later when trying to
> string-append them. I eventually discovered by process of elimination
> (after trying string?, etc. to figure out how the results of a regexp
> are stored) that they were a list. I tried list->string and a few other
> methods to covert them, but wasn't having any luck.
> 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?").
> So, my actual question is two-fold. First, It seems like I must have
> missed some pretty obvious way to capture the initial regex match as a
> string, but I'm not sure what it was.
> And secondly, and actually what I'm more curious about, why does it
> become a string when wrapped by car? I would have presumed that car
> would leave structures as a list.
> Again, I apologize if this is something dead-simple, but I really did
> spend a good portion of the day stuck in "regex-hell", and so was
> curious.
> -Chris
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20130718/7392667f/attachment-0001.html>

Posted on the users mailing list.