[racket] Adding raw string syntax

91 views
Skip to first unread message

Joan Arnaldich

unread,
Aug 7, 2011, 11:44:02 AM8/7/11
to us...@racket-lang.org
Hi there!

I'm just starting to learn Racket. One of the thing that I appreciate
most so far is the language extensibility (IMHO, the feature that
makes it so unique compared to other languages...). So, when I
couldn't find anything like python's raw string syntax (strings
introduced by r'') or c#'s @"" strings, I thought... well, let's try a
bit of language DIY...

So I went through the Guide and the brainfudge tutorial and came up
with a solution that worked as I wanted. I blogged about it here:

http://jarnaldich.me/2011/08/07/raw-strings-in-racket.html

In fact, I wrote that entry (well most of them...) more as a reminder
for myself than as a tutorial, but I still think it makes sense to
publish it, if only to demonstrate how you can tune the language to
match your personal tastes... something you can't do in every
language, for sure...

The problem is, as I said, that I'm just starting with Racket, so
while the solution works as expected so far, maybe I misunderstood or
skipped something in the documentation and just got it all wrong... so
any comments would be much appreciated, if only to reassure myself...

BTW, I searched the list and the docs for this feature before
implemeting mine... the only thing I found were some old questions on
backslashing in strings for regexes. But maybe this was already
implemented somewhere else and I couldn't find it...

Thanks in advance!

Joan.
_________________________________________________
For list-related administrative tasks:
http://lists.racket-lang.org/listinfo/users

Sam Tobin-Hochstadt

unread,
Aug 9, 2011, 12:20:55 PM8/9/11
to Joan Arnaldich, us...@racket-lang.org
On Sun, Aug 7, 2011 at 10:44 AM, Joan Arnaldich <jarna...@gmail.com> wrote:

> [snip]

I love your post -- it's great to have lots of people writing about
their experience hacking in Racket.

> BTW, I searched the list and the docs for this feature before
> implemeting mine... the only thing I found were some old questions on
> backslashing in strings for regexes. But maybe this was already
> implemented somewhere else and I couldn't find it...

The @ syntax originally designed for Scribble is very helpful for this
sort of thing.
--
sam th
sa...@ccs.neu.edu

Eli Barzilay

unread,
Aug 9, 2011, 12:27:27 PM8/9/11
to Sam Tobin-Hochstadt, us...@racket-lang.org, Joan Arnaldich
5 minutes ago, Sam Tobin-Hochstadt wrote:
> On Sun, Aug 7, 2011 at 10:44 AM, Joan Arnaldich <jarna...@gmail.com> wrote:
> > BTW, I searched the list and the docs for this feature before
> > implemeting mine... the only thing I found were some old questions
> > on backslashing in strings for regexes. But maybe this was already
> > implemented somewhere else and I couldn't find it...
>
> The @ syntax originally designed for Scribble is very helpful for
> this sort of thing.

A concrete example for using it as a raw string:

#lang at-exp racket
(define r string-append)
(display @r{...nearly free text here...})

--
((lambda (x) (x x)) (lambda (x) (x x))) Eli Barzilay:
http://barzilay.org/ Maze is Life!

Jos Koot

unread,
Aug 9, 2011, 1:19:44 PM8/9/11
to Joan Arnaldich, us...@racket-lang.org
Thanks for sharing your tutorial (it is, although your initial intention was
as a reminder for yourself) Very clear exposition. You made a very important
step by discovering how to make your own languages. In the long past I have
made many languages for specific applications, but without the nice tools of
Racket (fortran and an assembler with a very good macro system allowing
recursion and meta macros, id est macros that produce macros) Making a
language with these tools would require a 1000 to 10000 lines of code. With
Racket a hundred or a few hundred lines of code may be sufficient. I share
your enthousiasm for Racket.
Kind regards, Jos

Joan Arnaldich

unread,
Aug 10, 2011, 3:34:31 AM8/10/11
to us...@racket-lang.org
> > The @ syntax originally designed for Scribble is very helpful for
> > this sort of thing.
>
> A concrete example for using it as a raw string:
>
> #lang at-exp racket
> (define r string-append)
> (display @r{...nearly free text here...})
>
Aha! So there it was! Thanks a lot for the pointer! Ignorance is
always ignorance, but... I can't help thinking that if I had known
that I would have missed a lot of fun ;)

Jukka Tuominen

unread,
Aug 10, 2011, 4:39:42 AM8/10/11
to Joan Arnaldich, us...@racket-lang.org

Not much of an analogy, but I've always found the following thought
inspiring:

"People that are busy trying to convince something cannot be done, mustn't
disturb the ones that are already doing it!"
(Sorry, could't find who's quote it is).

Racket seems to be the tool for the doers - the threashold to DIY is very
low, as we have seen many times.

br, jukka

> -----Original Message-----
> From: users-...@racket-lang.org
> [mailto:users-...@racket-lang.org]On Behalf Of Joan Arnaldich

Joan Arnaldich

unread,
Aug 10, 2011, 3:08:13 PM8/10/11
to us...@racket-lang.org
> fortran and an assembler with a very good macro system allowing
> recursion and meta macros, id est macros that produce macros) Making a
> language with these tools would require a 1000 to 10000 lines of code.

Not even using lexer/parser generators... hats
up! (and shrivers down my spine...) ;)
Thanks for the answer, Jos!

Joan Arnaldich

unread,
Aug 10, 2011, 5:37:00 PM8/10/11
to us...@racket-lang.org
Jukka Tuominen <jukka.tuominen@...> writes:

> Racket seems to be the tool for the doers - the threashold to DIY is very
> low, as we have seen many times.

I see what you mean. Racket makes it hard to find those kind of
excuses: if you don't like or miss something, you can always go and
change it. How many languages will let you do that?

Thanks for the quote, Jukka!

Eli Barzilay

unread,
Aug 13, 2011, 8:58:09 PM8/13/11
to Joan Arnaldich, us...@racket-lang.org
On Wednesday, Joan Arnaldich wrote:
> > > The @ syntax originally designed for Scribble is very helpful for
> > > this sort of thing.
> >
> > A concrete example for using it as a raw string:
> >
> > #lang at-exp racket
> > (define r string-append)
> > (display @r{...nearly free text here...})
>
> Aha! So there it was! Thanks a lot for the pointer! Ignorance is
> always ignorance, but... I can't help thinking that if I had known
> that I would have missed a lot of fun ;)

Yes, it's certainly a good exercise in trying to get a quick
readtable-based grammar extension.

And speaking about this, I've noticed that someone put a link to your
tutorial on the Wikipedia page -- and I worry about getting more
people who are unaware of the scribble @-form syntax that does things
better when more people see it. Do you mind editing your post and
adding the above note so people won't miss it?

(I prefer that over dropping the reference, because it *is* a good
exercise in a quick extension, I just want to avoid people spending
time getting something like this more polished when the work is
already there.)

--
((lambda (x) (x x)) (lambda (x) (x x))) Eli Barzilay:
http://barzilay.org/ Maze is Life!

Joan Arnaldich

unread,
Aug 14, 2011, 12:05:14 PM8/14/11
to us...@racket-lang.org
Eli Barzilay <eli@...> writes:

> I've noticed that someone put a link to your
> tutorial on the Wikipedia page --

It was Asumu. I wouldn't feel confident enough for such a
self-plug ;P.

> and I worry about getting more
> people who are unaware of the scribble @-form syntax that does things
> better when more people see it. Do you mind editing your post and
> adding the above note so people won't miss it?

Done. I added a section at the end on the @form syntax, and a
note at the beggining so that people can skip the whole article if
they just want to get the functionality.

> (I prefer that over dropping the reference, because it *is* a good
> exercise in a quick extension, I just want to avoid people spending
> time getting something like this more polished when the work is
> already there.)

If the tutorials section in the article section is to stay, then I'll
add this one here, which I think is far more interesting than
mine:

http://matt.might.net/articles/implementing-a-programming-language/

Speaking of the article, after Asumu's mail I started to contribute a
little bit. I was planning to focus on the practical side of Racket,
so I added a brief section on this. I would like to add a link at the end of
the section pointing at a new wikipedia page with examples on how
Racket can be used in different application domains, and to show some
of the different languages / paradigms available (a bit like the
Haskell page does in the features section)... the problem is that it
is hard to come up with better examples than the ones already at
racket-lang.org, so could I use some of those (always with the
reference)?. I think more wikipedia readers will get to see the
examples if they're in a regular wikipedia page rather than having to
click to an external link and then browse back and forth and click on
the question mark for an explanation.

Matthias Felleisen

unread,
Aug 14, 2011, 5:16:58 PM8/14/11
to Joan Arnaldich, us...@racket-lang.org

On Aug 14, 2011, at 12:05 PM, Joan Arnaldich wrote:

> If the tutorials section in the article section is to stay, then I'll
> add this one here, which I think is far more interesting than
> mine:
>
> http://matt.might.net/articles/implementing-a-programming-language/


But Matt's page is just a discussion of interpreters.
That's a rather different notion of implementing a
language. You could do so in asm, no real tools needed.


>
> Speaking of the article, after Asumu's mail I started to contribute a
> little bit. I was planning to focus on the practical side of Racket,
> so I added a brief section on this. I would like to add a link at the end of
> the section pointing at a new wikipedia page with examples on how
> Racket can be used in different application domains, and to show some
> of the different languages / paradigms available (a bit like the
> Haskell page does in the features section)... the problem is that it
> is hard to come up with better examples than the ones already at
> racket-lang.org, so could I use some of those (always with the
> reference)?. I think more wikipedia readers will get to see the
> examples if they're in a regular wikipedia page rather than having to
> click to an external link and then browse back and forth and click on
> the question mark for an explanation.


A very good idea. When I made screen savers for my Mac from
them, I placed them on the web here (by accident):

http://www.ccs.neu.edu/home/matthias/Presentations/10ICFP/Background/

Eli Barzilay

unread,
Aug 14, 2011, 6:02:11 PM8/14/11
to Matthias Felleisen, Joan Arnaldich, us...@racket-lang.org
5 hours ago, Joan Arnaldich wrote:
>
> Done. I added a section at the end on the @form syntax, and a note
> at the beggining so that people can skip the whole article if they
> just want to get the functionality.

Thanks!


> If the tutorials section in the article section is to stay, then
> I'll add this one here, which I think is far more interesting than
> mine:
>
> http://matt.might.net/articles/implementing-a-programming-language/

Matthias Felleisen wrote:
>
> But Matt's page is just a discussion of interpreters.
> That's a rather different notion of implementing a
> language. You could do so in asm, no real tools needed.

Yeah -- when building an interpreter Racket does have its advantages,
but at that level it's pretty close to many other functional
languages. In any case, it looks like someone else saw that and
removed the entry.


> Speaking of the article, after Asumu's mail I started to contribute
> a little bit. I was planning to focus on the practical side of
> Racket, so I added a brief section on this. I would like to add a
> link at the end of the section pointing at a new wikipedia page with
> examples on how Racket can be used in different application domains,
> and to show some of the different languages / paradigms available (a
> bit like the Haskell page does in the features section)... the
> problem is that it is hard to come up with better examples than the
> ones already at racket-lang.org, so could I use some of those
> (always with the reference)?. I think more wikipedia readers will
> get to see the examples if they're in a regular wikipedia page
> rather than having to click to an external link and then browse back
> and forth and click on the question mark for an explanation.

That sounds like a very good idea -- not only include them, but also
add some descriptions. To get all the example sources in a convenient
way, you can just look in the source file that has them:

http://git.racket-lang.org/plt/blob/HEAD:/collects/meta/web/www/index.rkt

--
((lambda (x) (x x)) (lambda (x) (x x))) Eli Barzilay:
http://barzilay.org/ Maze is Life!

Greg Hendershott

unread,
Aug 15, 2011, 2:00:02 PM8/15/11
to Eli Barzilay, us...@racket-lang.org, Joan Arnaldich, Matthias Felleisen
I just want to suggest the idea that it's positive for the promotion
of Racket to have many usage examples, even if it's stuff not
particularly easy or special to do in Racket.

Something I think we've all seen with technology marketing (yeah, I
said the m-word): When I've built something, there's a natural
tendency to focus on what's-new-in-this-version or
what-makes-us-better-than-the-competition. That's what's most
interesting to me, to gatekeepers, and even to some subset of
customers. But many potential customers are interested in the basic
story, including all the bits that are old and unremarkable to me.

Plus in the case of Racket it might help to have more mundane
examples, rather than fewer. Such examples help convey: "See, you can
do practical stuff in Racket just like in your blub language. There's
all this other power and expressiveness waiting for you to try, when
you're ready. But Racket isn't only for PL researchers. Oh and don't
worry, you won't need Leibniz to printf."

Joan Arnaldich

unread,
Aug 15, 2011, 5:15:27 PM8/15/11
to us...@racket-lang.org
Greg Hendershott <greghendershott@...> writes:

> Plus in the case of Racket it might help to have more mundane
> examples, rather than fewer. Such examples help convey: "See, you can
> do practical stuff in Racket just like in your blub language. There's
> all this other power and expressiveness waiting for you to try, when
> you're ready. But Racket isn't only for PL researchers. Oh and don't
> worry, you won't need Leibniz to printf."

Agreed. In fact, I just added a wikipedia page with Racket code
examples. Most of them come from Racket's main page, but I think it
would be nice to add some practical and short applications of the kind
"look what you can do in only 7 lines of Racket"... that's why I added
the grep-like example... if you think that's ok, feel free to add your
own...

The page is here:

http://en.wikipedia.org/wiki/Racket_features#Language_extensions

I'll post more details on my modifications to the wikipedia article thread.

Reply all
Reply to author
Forward
0 new messages