Posts tagged Racket

__FILE__ and __LINE__ in Racket

:: Racket, racket-cookbook

Many languages have a variable (or preprocessor macro) called __FILE__ or __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 main submodule (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?

Racket cookbook

:: Racket, racket-cookbook

Two parallel thoughts:

  1. 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.

  2. 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.

  1. Admittedly I rarely recall using any “cookbook” resource to find the answer to a problem I have. Taken literally, programming cookbooks are largely a failure, for me. On the other hand, flipping through one can give the “flavor” of a language. And as I’ve written, one valid learning strategy is to absorb things shallowly — you may not know the answer, but you know roughly where to find the answer. It rings a bell. Probably cookbooks fit that model — filling your head with, um, bells. 

Using syntax/loc and unit test macros

:: Racket, racket-cookbook, macros

In my previous post, I wrote about a nuance with syntax/loc, using the example of a macro that both defines and provides a function. But why don’t I back up, and look at a simpler example of why you’d want to use syntax/loc. The example is a simple macro you might often find yourself wanting, to reduce the tedium of writing unit test cases.

Using syntax/loc

:: Racket, macros

There’s a nuance to syntax/loc. The documentation says, emphasis mine:

Like syntax, except that the immediate resulting syntax object takes its source-location information from the result of stx-expr (which must produce a syntax object), unless the template is just a pattern variable, or both the source and position of stx-expr are #f.

What does “immediate” mean here?

Racket package management

:: Racket

Racket’s new package manager is great. It debuted with Racket 5.3.5. Although officially still in beta, it was already good enough to use for real projects. Racket developers wanted people to use it for real projects, to get the experience needed to make it even better.

Over many months, the Git HEAD version of Racket — what would become Racket 6 — gradually introduced a few new and changed features for package management.

However, you might not want to use the newer features, yet. Not if you want your package to be usable by people still using Racket 5.3.5 or 5.3.6 — or usable by other packages that wish to support such people.

Fortunately, the older features are still supported in Racket 6, and it’s not very difficult or inconvenient to use them. You just need to know what they are.

Using call/input-url

:: Racket, racket-cookbook

When I learned Racket, one of the first things I wanted to try was doing HTTP requests. And Racket’s net/url module is great.

Racket was the first real Lisp/Scheme family language I ever learned. As a result I was focused on building blocks like ports, and assuming I would need to open and close them directly all the time. At that early stage, I also didn’t really appreciate the value of higher-order functions. So I overlooked the value of call/input-url. I sometimes see other folks do the same, and wanted to write this short blog post.

Skim or sink

:: software, Racket

In my experience, the way to become a better programmer is to:

  • write lots of code
  • skim lots of reading material, and
  • defer answers until you have questions.

To learn why you should skim this post, please read it carefully.

Using Travis CI for Racket projects

:: Racket

(Updated 2014–06–06)

Travis CI is a continuous integration service for open source projects that has nice integration with GitHub.

Whenever you push a commit to GitHub, a build/test can launch. Notification of the result comes via a variety of methods. Also there’s a “badge” to show the build status, which you can link to in your README.md.