Posts tagged Racket
This revises my Keyword structs post to fix some mistakes, discuss the
match pattern, and rewrite the macro to use
syntax-parse and support default arguments.
A good rule of thumb in Racket is to use a
struct instead of
list when you’re juggling more than two or three items.
My first-ever open source contribution, a couple years ago, was to a project called Pygments. My motivation? GitHub was displaying Racket source code poorly. Pygments didn’t have a Racket lexer. GitHub was using a Scheme lexer for Racket code. The Scheme lexer was highlighting square brackets in red as an “error”. This was really distracting and ugly.
I contributed a new Racket lexer to Pygments, and waited for that to roll into a Pygments release and in turn be deployed on GitHub. Finally Racket code looked good! Later Dave Corbett substantially improved the Racket lexer beyond my small start.
A few days ago, I was confused to see that Racket code was displaying poorly again on GitHub. The square brackets were highlighted in red as errors — again??
Cartoon-me’s thought balloons: WAT, OMFG, FML, &c. Why are we going in circles?
If you’re coming to Racket from another REPL language (such as another Lisp), this post might be real Captain Obvious material.
But if you’re coming to Racket from an edit/compile/debug language like C or C++, it might be unclear what a typical workflow is. You might have questions like:
- How do I compile?
- How do I debug?
Yesterday a couple people asked me, “How and why do you use macros in a Lisp like Racket or Clojure?”.
I gave answers like:
Although all true, I wasn’t sure I was getting the full idea across.
This is an overview of things I’ve created using Racket. Two motivations for writing this now:
Over the last week I was at three conferences (whew!) where, when meeting or catching up with someone, I had to explain what I’ve been doing. I mentioned my current projects or projects I guessed they’d relate to. But that’s not necessarily representative of all that I’ve been able to do with Racket. I wish I’d been able to give a better overview. I have quite a few repos on GitHub, but that’s just a big list with no structure.
In about a week I start my batch at Hacker School. I’ll likely spend less time with Racket, because the whole point is to learn new things. Now is a good time to take inventory. And I’ll be better prepared to talk about Racket there.
As a result, here’s an inventory, grouped into categories.
Let’s say you need to destructure a list with
match, using a pattern that specifies a “rest” or “more” element. Be careful. You probably want to use
Most of my Racket projects don’t use a makefile. Why would they?
raco setup suffices.
But a makefile can consistently define some common project tasks. And it can really help when you want to generate HTML documentation from Scribble sources, and publish it to GitHub Pages using the automagical
Many languages have a variable (or preprocessor macro) called
__file__ whose value is the pathname of the current source file. Likewise
__LINE__ for the the source line number.
You probably need this less in Racket than you imagine. For example:
We wouldn’t test that
__FILE__ ends in
main.rkt; instead we’d use a
(module+ main <your code here>).
To get a data file
foo.dat located in the same directory as a source file we’d use
(define-runtime-path foo.dat "foo.dat"). If we’re a package or executable this works regardless of where our files happen to get installed.
But if you really did need a
__FILE__ in Racket, how would you do it?
Let’s say we want to use
find-collects-dir, which was added in Racket 6.0. We get a bug report from someone using Racket 5.3.6.
To fix this, we can
dynamic-require the desired function; when it doesn’t exist, we can use our own fallback implementation.1
Two parallel thoughts:
I haven’t blogged in awhile. I’ve been heads-down on a few projects. Plus I haven’t had ideas I feel are “big” or unique enough to merit a post.
It’s occurred to me that a “Racket Cookbook” might be a useful resource. Because examples. Because real-life, practical examples.1
Although I haven’t created a cookbook, my working assumption is that it would be better to write one recipe at a time. As they arise. As I think, “Ah right, this wasn’t obvious to me when I was learning Racket.”
So I plan to experiment with releasing the things one at a time, as short blog posts. Thereby terminating two avian organisms with one geologic projectile.
Not sure if I’ll keep it up. Definitely not sure if I’ll ever collect and polish them into a “book” of some form. They might only ever live as a
racket-cookbook tag on this blog.