23 Nov 2014


posted by Tim Brown

800 (Racket Tasks On Rosetta Code)

Since (and even before) Asumu Takikawa’s post “200!” at the beginning of March 2013, folk have been beavering away, implementing tasks on Rosetta Code. And on November 15th 2014:

800 tasks have now been Implemented1 in Racket on the Rosetta Code website!

Before I go any further it must be said that, without a doubt… this is awesome! This achievement represents a lot of work, and a lot of code. And everyone who has participated should be thanked and congratulated for getting this far.

So thank you. And well done!

What is Rosetta Code?: Rosetta Code (RC) describes itself as:

… a programming chrestomathy site. The idea is to present solutions to the same task in as many different languages as possible, to demonstrate how languages are similar and different, and to aid a person with a grounding in one approach to a problem in learning another. Rosetta Code currently has 758 tasks, 134 draft tasks, and is aware of 560 languages, though we do not (and cannot) have solutions to every task in every language."2

Of these tasks, 800 have been implemented in Racket… some tasks, like Hello World/Text, have been implemented in hundreds of languages. Some, however, like Time-based One-time Password Algorithm, have only been implemented in 3 (including Racket).

If you haven’t already, I suggest you take a quick look about the site to get a feel of what that means in practice.

WARNING: Rosetta Code is a wiki. Like any wiki it will steal your time from you as you browse tasks, algorithms, languages and the occasional link to Wikipedia. Don’t say I didn’t warn you.

What Can You Do With Rosetta Code? —

Learn From It Rosetta Code is a valuable resource with plenty of material to absorb and ideas to be had from. If you’re new to Racket, there are tasks like Loops/For which will get you on your way with fundamental programming tasks.

If you want something juicier there are other tasks (like Nonogram solver which runs to over 400 lines of Racket) for you to pick over.

And there’s everything in between.

Write Code!

Each task gives you a chance to think, “Is this how I would do this?”

Even if I don’t submit something, I find it’s fun to write some code around the task. In fact, I don’t even have to type code into a REPL, the thought exercise is often fun enough!

Some tasks, like (“Chess Player”), are shall we say, very challenging. But don’t let even that put you off thinking of, tinkering around or coding a solution to them.

If there isn’t a Racket implementation for a task you like the looks of, have a go.

Someone might have a better idea of how to do it in Racket later. But if there isn’t an implementation now — change that now!

Remember that others will be reading your code to understand Racket all the better. So please try to adhere to the Racket Style Guide as best you can3. Again, don’t worry about getting that perfect. Like anything, learning Racket style takes practice, and nobody expects perfection. And the experienced contributors/documentors are always at hand to help correct style.


  • I always have a Wikimedia Cheatsheet to hand. I can never remember its markdown syntax (which unfortunately doesn’t really support <code/>, either)

  • Pick whatever task you want… but it would be good to clear all the “Complete Tasks” (as opposed to “Draft Tasks”) if you have a choice.

  • Don’t add code until it’s running and producing the output you expect. This isn’t Project Euler, you don’t have to guess the answer in most cases — it’s likely someone has some sample output to compare to.4

  • If you can’t get your head around the algorithm in the task description then try to translate another language into Racket. You’ll learn Racket, you’ll learn the other language, and in working it through for yourself you’ll also see how the algorithm takes shape and works.

  • My workflow for posting a new solution is this: Once I have something to submit, I add a “Stub” Racket implementation. I edit the whole Task page (because that is all that is available to edit at that point), find whatever is after Racket alphabetically, and insert boilerplate code. I check this template code as a “minor edit” described as “Racket stub added — implementation later”. This then allows is for me to be able to edit the Racket section in isolation and keeps the rest of the task (everyone else’s hard work) safe from any, er, silliness. {{header|Racket}} <lang racket> </lang racket> {{out}} <pre> </pre>

  • I have started to make it my habit (especially when showcasing one or two functions) to use #lang racket/base and requireing the salient individual functions.

  • It is often not appropriate to fully document the functionality of Racket functions in the RC task implementation. You can, however, point to the canonical documentation on the Racket website. So I also include a link to http://docs.racket-lang.org/reference/... when I need to.

  • The RC administrators have switched off image uploading (or I, at least, cannot find out how). Even though Racket can produce images as results, think hard about whether you want the hassle of trying to present images to the reader. If you find out a method that works for you please tell me, I’d love to know. I suppose you could also extend all of the above to coding in another language — if you really have to.

Improve What’s Already There

Rosetta Code is a wiki.

It is open to anyone to edit.

Don’t be afraid to. If you see something that could be implemented, styled or documented better — work to improve it.

Once you have your improved entry together, show it to the author of the original post. Besides being courteous, he or she might have an opinion on what else you might do. Often, there is something bugging them, and you are scratching an itch of theirs!

I have never had anyone react badly to a change request. Everyone appreciates that you have made an effort to produce your change (and that you’re not just standing in the aisles complaining that it doesn’t look right).

Teach Through It —

If there is an aspect of Racket, algorithm or other “CS task” (in the broadest sense) that you want to share: see to it that it is adequately illustrated on Rosetta Code. If it is not, then create a task to demonstrate it. Not only will you show how something is done properly (i.e. in Racket), but you will also be inviting others to implement the task in their own favourite languages.

The Competition

Back at 200, Racket was the 54th most popular language. But for some time now, it has been sitting at #2 in the popularity1 ranking for quite some time now. For a while, it has been placed a long way behind TCL, and being hotly pursued by Python (never more than 10 tasks behind).

One of my motivators is that having seen Racket get to #2 — I don’t want to see it any lower in the rankings. I’m sure there’s something in the Python lot that wants to overtake us! This healthy competition has kept both of the communities pushing ahead with implementing the tasks.

The Intro Projects page of the racket wiki has “Implement a Rosetta code task” as a “Small Project”. I think of it as slightly more of a “Recreational” project (this at least justifies to myself the element of competition that has crept in.)

The Rallying Call —

or “What Specifically Would Help Racket on Rosetta Code?”

RC is a good way to present Racket as a most general programming language. So as a tool for Racket advocacy, as well as for the purposes of RC, we need to:

  • Implement more tasks in Racket to keep a high profile: 800 tasks, #2 in the popularity stakes. This keeps Racket visible; and proves it capable of (almost) anything. I would so love to give TCL a run for its money — so there’s 41 tasks to go before we can even think of taking a breather!

  • We have implemented 800 tasks in Racket. The quote above says there are 892 (758+134) tasks in total. That means that there are 92 more tasks to get to grips with.

  • Suggest new tasks: Especially tasks that will demonstrate the latest shiny feature of the latest shiny versions of Racket!

Personally I can’t believe that there are less than 900 things that you would want to do with a programming language. If you think of a task, add it. Even impossible tasks provoke thought and imagination — and interesting solutions!

  • Improve those tasks that have been implemented in Racket: We want to maintain a body of good, useful code, to allow us to teach and demonstrate Racket. There are a number of reasons why existent tasks need revisiting:

  • Racket technology has moved on (and moves on) apace. What was unavailable and experimental even 18 months ago is now available and reliable. This new technology needs to be demonstrated.

  • Code that is even older is very “schemey” (I have in some cases simply copied the Scheme implementation and stuck a #lang racket tag on the front). Although compatible, Racket has moved quite a way on from Scheme.

  • Some implementors (not a million miles from where I’m standing, for example), were not as au fait with the language and/or style guide as they might be now. It’s a housekeeping job, I know, but giving the examples as consistent a style as possible will help satisfy this aspiration from the Racket Style Guide:

“Doing so will help us … and our users, who use the open source code … as an implicit guide to Racket programming.”

  • Document tasks: see my hint about documentation and links above for what I now think is good practice. If some code seems utterly heiroglyphic, see if it can be made clearer. Remember this is Racket, not APL.

  • General tidying up never goes amiss.

  • RC is run by someone outside the Racket community. At the bottom of the “Small Projects” section of the Racket wiki is a suggestion to collect the RC examples into something “owned” by the Racket community. I’ve been thinking about this… if anyone has suggestions, let me know. There are limits to what we can put on RC (defined by the purpose of RC itself). It would be good to remove those limits by implementing something along RC’s lines oursleves.

  • Very specifically… anyone with a joystick, drivers and some spare time - please could you do “Joystick Position”. The possession of a joystick puts you in a position of great power with respect to that task. Exercise your responsibility. And Finally…: Well done everyone again! Keep up the good work. And see you at 1000!

  • You can track Racket (and everyone else’s) progress on the Popular Programming Languages report, which is updated hourly or so.

  • Rosetta Code’s Front Page

  • The style guide is actually the chapter called “How to Program Racket” in the main Racket documentation. One of the RC “style” rules is that code should be 80 characters wide. Personally, I ignore that in favour of Racket’s more generous 102. Sometimes someone on RC objects. Sometimes I then care enough to put the required newlines in.

  • Even if there are example results don’t necessarily trust them. e.g. in The ISAAC Cipher, the cypher engine isn’t reset between test runs in the Pascal implementation. That error is propagated through all other implementations. Mine (Racket) conforms to show that I’m doing the same thing as everyone else; but I also do what I think to be a more correct test later.

  • Hold on a mo… this is meant to be a pedagogical exercise, not a competition

more →

04 Nov 2014

Racket v6.1.1

posted by Ryan Culpepper

Racket version 6.1.1 is now available from http://racket-lang.org/

  • The Mac OS X Yosemite compatibility problems are fixed. We bundled a patched Pango text-drawing library with Racket.

  • The Windows [32-bit] releases fixes the window-update crashes. We bundled a patched Cairo drawing library with Racket.

  • Typed Racket closes two safety holes in the exception system. The revised type system restricts raise to send only instances of the exn structure type and flat data to handlers. It also checks exception handlers properly. Note: Previously well-typed programs may fail to typecheck.

  • Typed Racket’s typed regions support casts and predicates.

  • 2htdp/image’s notion of equality ignores an image’s baseline.

  • The package manager supports a binary library installation mode, which allows users to install packages without source or documentation. Use the --binary-lib option with raco pkg install.

  • The new drracket-tool-lib package factors out parts of DrRacket’s IDE so that they can be reused with other editors, such as Emacs.

  • The compiler’s use-before-defined analysis has been repaired for certain forms of nested letrec, some let forms, and some uses of set! or with-continuation-mark.

  • The compiler performs additional bytecode optimizations. Thanks to Gustavo Massaccesi.

  • The CML library comes with a new replace-evt event constructor. Thanks to Jan Dvořák.

  • Redex’s benchmark suite comes with a description of the benchmark programs.

  • Redex’s metafunctions can be typeset using the “large left brace” notation for conditionals.

  • The contract library comes with an improved contract-stronger?. Its error messages note that the contract itself might be wrong.

  • The GUI library is DPI-aware on Windows.

  • The openssl library supports Server Name Indication for servers. Thanks to Jay Kominek.

  • The syntax/parse library allows the definition of new pattern forms via pattern expanders, similar to match expanders. Thanks to Alex Knauth.

  • OpenGL on Linux no longer depends on libgtkgl, and core profiles are supported (see set-legacy?).

  • The teaching languages’ unit test framework supports check-satisfied, a construct for checking whether a result satisfies a predicate, e.g.:

(check-satisfied (sort l) sorted?)

Feedback Welcome

more →

07 Oct 2014

PLT Redex Summer School, Call for Participation

posted by Robby Findler

LOCATION: University of Utah, Salt Lake City

DATES: July 27 - July 31, 2015


PLT Redex is a lightweight, embedded DSL for modeling programming languages, their reduction semantics, and their type systems. It comes with an IDE and a toolbox for exploring, testing, debugging, and type-setting language models. The PLT research group has successfully used Redex to model and analyze a wide spectrum of published models.

The summer school will introduce students to the underlying theory of reduction semantics, programming in the Redex language, and using its tool suite effectively. The course is intended for PhD students and researchers in programming languages. Enrollment is limited to 25 attendees.

While the workshop itself is free, attendees must pay for travel, room, and board. We expect room and board to be around $500, assuming an arrival in the evening of Sunday July 26 and leaving Friday July 31 or August 1. Partial financial support for PhD students is available.

To register, send email to Matthew Flatt ([email protected]). If you are a PhD student and requesting financial support, CC your advisor and ask for a one-line confirmation email.


Matthias Felleisen, Robert Bruce Findler, Matthew Flatt. Semantics Engineering with PLT Redex. MIT Press, 2012.

Casey Klein, John Clements, Christos Dimoulas, Carl Eastlund, Matthias Felleisen, Matthew Flatt, Jay McCarthy, Jon Rafkind, Sam Tobin-Hochstadt, Robert Bruce Findler. Run Your Research: On the Effectiveness of Lightweight Mechanization. POPL 2012.

more →

02 Aug 2014

Racket v6.1

posted by Ryan Culpepper

PLT Design Inc. announces the release of Racket version 6.1 at


The major innovation concerns local recursive variable definitions. Instead of initializing variables with an undefined value, Racket raises an exception when such a variable is used before its definition. (Thanks to Claire Alvis for adapting Dybvig’s “Fixing Letrec” work.)

Since programs are rarely intended to produce #<undefined>, raising an exception provides early and improved feedback. Module-level variables have always triggered such an exception when used too early, and this change finally gives local bindings — including class fields — the same meaning.

This change is backwards-incompatible with prior releases of Racket. Aside from exposing a few bugs, the change will mainly affect programs that include

(define undefined (letrec ([x x]) x))

to obtain the #<undefined> value. In its stead, Racket provides the same value via the racket/undefined library (which was introduced in the previous release). Programmers are encouraged to use it in place of the pattern above to obtain the “undefined” value.

The release also includes the following small changes:

  • Plumbers generalize the flush-on-exit capability of primitive output ports to enable arbitrary flushing actions and to give programmers control over the timing of flushes (i.e., a composable atexit). New functions include current-plumber, plumber-add-flush!, and plumber-flush-all.

  • Contracts: the contract system’s random testing facility has been strengthened so that it can easily find simple mistakes in contracted data structure implementations (e.g. an accidental reverse of a conditional in a heap invariant check).

  • Redex: the semantics of mis-match patterns (variables followed by _!_) inside ellipses has changed in a backwards-incompatible way. This change simplifies the patterns’ semantics and increases the usefulness of these patterns.

  • Teaching languages: check-random is an addition to the preferred unit testing framework in the teaching languages. It enables the testing of students’ functions that use random-number generation. (Thanks to David Van Horn (UMaryland) for proposing this idea.)

  • Upgraded and normalized versions of graphics libraries and dependencies (Pango, Cairo, GLib, etc.) that are bundled with Racket on Windows and Mac OS X. For example, FreeType support is consistently enabled.

  • Typed Racket: its standard library includes contracted exports from the Racket standard library, such as the formatting combinators of racket/format. It also supports Racket’s asynchronous channels; see the typed/racket/async-channel library.

  • SSL: The openssl library supports forward secrecy via DHE and ECDHE cipher suites (thanks to Edward Lee) and Server Name Indication (thanks to Jay Kominek).

  • The mzlib/class100 library has been removed. Use racket/class instead.

more →

26 Jul 2014

Scheme Workshop 2014

posted by John Clements

DEADLINE: 5 September 2014, (23:59 UTC–12)

WEBSITE: http://homes.soic.indiana.edu/jhemann/scheme–14/

LOCATION: Washington, DC (co-located with Clojure/conj)

DATE: 19 November 2014

The 2014 Scheme and Functional Programming Workshop is calling for submissions. Submissions related to Scheme and functional programming are welcome and encouraged. Topics of interest include but are not limited to:

  • Program-development environments, debugging, testing

  • Implementation (interpreters, compilers, tools, benchmarks, etc)

  • Syntax, macros, and hygiene

  • Distributed computing, concurrency, parallelism

  • Interoperability with other languages, FFIs

  • Continuations, modules, object systems, types

  • Theory, formal semantics, correctness

  • History, evolution and standardization of Scheme

  • Applications, experience and industrial uses of Scheme

  • Education

  • Scheme pearls (elegant, instructive uses of Scheme)

We also welcome papers related to dynamic or multiparadigmatic languages and programming techniques.

Full papers are due 5 September 2014.

Authors will be notified by 10 October 2014.

Camera-ready versions are due 24 Oct 2014.

All deadlines are (23:59 UTC–12), “Anywhere on Earth”.

For more information, please see: http://homes.soic.indiana.edu/jhemann/scheme–14/

See you there!

more →

08 May 2014

Racket v6.0.1

posted by Ryan Culpepper

Racket version 6.0.1 is now available from


  • A new racket/undefined library exports undefined as the value currently produced by

(letrec ([x x]) x) This library anticipates a future where that expression will raise an exception. The racket/undefined library will continue to offer the undefined value as a bridge between versions and as a last resort.

  • The drawing and GUI libraries provide improved support for high-resolution bitmaps and their use on Retina displays. For example, read-bitmap includes a #:[email protected]? option to trigger substitutions through the usual "@2x" naming convention.

  • Check Syntax cooperates with Typed Racket to show arrows and other Check Syntax highlighting even when there is a type error.

  • Functions provided via contract-out that have first-order contracts perform better.

  • The contract boundary between typed/untyped modules is much less expensive. Typed Racket now avoids generating contracts for places where contracts failures cannot happen.

  • Occurrence typing now works better with when/unless. Example:

(let ((x (read)))
  (unless (number? x) (error 'bad-input))
  (add1 x))
  • Types in Typed Racket are now pretty-printed.

  • Function types can now be written in prefix style, which is now preferred and is used for printing. Infix function types are still accepted for backwards compatibility.

  • A new ->* type constructor is used for writing types for functions with optional and keyword arguments. The notation is similar to the matching contract combinator.

  • Typed Racket forms do not have a : suffix by default now. For example, the struct form replaces struct:. The suffixed versions are all provided for backwards compatibility.

  • Typed Racket now has preliminary support for classes and objects. However, it is still experimental and the APIs are subject to change.

  • Type aliases in Typed Racket now support recursion and mutual recursion. For example, (define-type (MyList X) (U Null (Pair X (MyList X)))) is now a valid type alias.

  • Plot correctly renders intersecting 3D graphs and non-grid-aligned 3D rectangles.

  • Elements in plots output in PDF/PS format have the same relative scale as in other formats. In particular, it is not necessary to adjust plot-font-size to make PDF plots look the same as PNG.

more →

24 Apr 2014

Registration for (fourth RacketCon)

posted by Asumu Takikawa

Registration for (fourth RacketCon) is now open. Tickets are 30$.


(fourth RacketCon) will be held on September 20th, and will be co-located with Strange Loop in St. Louis.

RacketCon is a yearly event where members of the Racket community get together, featuring talks and demos about the things you do with Racket.

To get you excited, here’s a sneak peek at the roster of speakers so far:

  • Michael Fogus (keynote)

  • Matthew Butterick

  • Matthew Flatt

  • Jay McCarthy

  • Daniel Prager

  • Neil Toronto

We still have room for more speakers. If you’ve built something cool with Racket, we want to hear about it!

Hope to see you there!

Vincent, for the Racket team

more →

12 Mar 2014

The new Racket home page

posted by Asumu Takikawa

Racket now has a new look for its web presence. You can see it at http://racket-lang.org (modulo DNS propagation).

The new pages improve on the old in several ways:

  • More information is now on the front page.

  • The site works much better small devices, such as phones.

  • It’s easier to find important parts of the site, like the package directory.

  • The download process is more streamlined — now it’s only 2 clicks.

We also wanted to have a new look, and a description that talks about why Racket is exciting.

Finally, we’ve taken this opportunity to shift our web hosting entirely to Amazon S3, for which Greg Hendershott’s aws package has been very helpful.

While the new pages have been under development for quite a while, we’re still happy to take bug reports, suggestions and (especially) patches: all of the site can be found here:


with the framework for building the site here:


The site is built using Eli’s scribble/html library, which is very nice to use.

I’d especially like to thank Eli and Matthew for their help with this — going from my hacked-up HTML prototype to the smooth-building and well-organized code we have now has taken lots of work.


more →

26 Feb 2014

Racket v6.0

posted by Ryan Culpepper

Racket version 6.0 is now available from


Racket 6.0 has a new package system, including a catalog of hundreds of already-available packages. Please visit


for an overview of the packages.

Racket versions 5.3.4 through 5.3.6 included “beta” versions of the package system. Racket version 6.0 incorporates many improvements suggested by preliminary experiences in those versions:

  • A package is treated as a single collection by default, so it is even easier to use a GitHub repository as a package. Get started quickly: http://docs.racket-lang.org/pkg/getting-started.html

  • DrRacket includes a new package manager GUI, available via the File|Package Manager … menu item. The GUI is also available as a stand-alone program via the “gui-pkg-manager” package.

  • The main Racket distribution has been separated into about 200 packages. The Racket installer combines the core system with bundled versions of these packages.

Alternatively, you may now install a Minimal Racket distribution — which is about 1/10 the size of the main distribution — and add only those packages that you need.

  • Package installation supports pre-built packages that include compiled byte code and rendered documentation, meaning packages can be installed quickly when built versions are available. All packages in the main distribution are available in pre-built form.

The recent 5.92 and 5.93 releases served as release candidates for 6.0, and 6.0 includes a few additional repairs related to the package system.

Further improvements to the package system are in the works, notably including package documentation on the package-catalog web site.

COMPATIBILITY NOTE: PLaneT, the previous Racket package system, will remain in place for the foreseeable future, but we expect all package work to shift to the new system.

Beyond the package system, this release brings a number of other changes:

  • Racket’s HTML documentation has a new and improved look, thanks to Matthew Butterick.

  • The documentation includes a style guide, “How to Program Racket”.

  • Racket’s JIT compiler supports the ARM architecture.

  • Racket supports the Mac’s Retina display mode.

  • The performance of the Typed Racket compiler improved by 50% on some typed programs; e.g., see http://bit.ly/1d0Ye4z

  • The profiler provides a new mode that uses the errortrace library to produce fine-grained profiles.

  • A new contract profiler reports how much time programs spend checking contracts, and which contracts are most expensive.

  • The math/flonum library exports fast 105-bit precision operations.

  • Check Syntax handles generated identifiers, especially those introduced by struct (e.g. field selectors) and Redex (e.g., e_1, e_2)

  • 2htdp/batch-io includes functions for dealing with html/xml in files and web sites as X-expressions plus conveniences for web-based graph traversals.

  • The `gen:set’ generic interface extends set operations to work on user-defined types that implement set methods, as well as on other set-like built-in types, such as lists.

  • Picts support conversion to SVG format.

  • Under unix, Racket provides desktop entries (.desktop files) for its graphical executables.

more →

17 Dec 2013

Typed Racket and Classes

posted by Asumu Takikawa

Recently we had our inaugural Racket Salon meetup here in Boston, graciously organized by Dan King. At the meetup I gave a short demo about the upcoming support for classes and object-oriented programming in Typed Racket. In this blog post, I’ll go over the concepts I presented in my demo.


As many readers already know, Typed Racket is a gradually-typed sister language to Racket. That means it’s a statically-typed language that accommodates the idioms of Racket. Programs written in Racket should seamlessly port to Typed Racket with the addition of type annotations here and there. You can even keep some parts of the program dynamically-typed and Typed Racket will make sure those parts won’t break the type invariants via contracts.

Of course, supporting all Racket idioms is quite a lot of work, especially since it’s a constantly evolving language. One of the big pieces missing from Typed Racket right now is support for classes and objects. Since the GUI library heavily uses the class system, it’s important to support the object-oriented subset of Racket.

Supporting classes isn’t trivial though. There are a bunch of issues, but the main ones are: (1) the class system is built as a complex macro and so reconstructing the information needed to type-check is tricky, and (2) we need to make sure that interoperation between Racket and Typed Racket using classes/objects can be done safely.

I won’t go over the technical details about the implementation in this blog post, but contact me if you’re interested. In the rest of the blog post, I’ll show some examples to demonstrate what programming in Typed Racket with classes looks like. Just so you know, these examples won’t work in the current version of Typed Racket but will be supported in a future release.

Side note: If you like to live dangerously, you can track the experimental branch with support for classes here.

Fishes and types

To start out, let’s look at an untyped example from the Racket Guide on classes and objects. The following snippet defines a fish% class. The class has several features: an initialization argument named size, a private field named current-size, three methods get-size, grow, and eat.

(define fish%
  (class object%
    (init size)
    (define current-size size)
    (define/public (get-size)
    (define/public (grow amt)
      (set! current-size (+ amt current-size)))
    (define/public (eat other-fish)
      (grow (send other-fish get-size)))))

The notation should seem mostly familiar if you’ve programmed in Java or other object-oriented languages. The % suffix is a convention for identifiers that are bound to class values. The object% value is the superclass of fish% and is the root class of all class hierarchies. The initialization argument size is used when constructing an instance of a class:

> (new fish% [size 3])
(object:fish% ...)

Note that in Racket, classes are just values that can be passed around like anything else. For example, you can even do silly things like define nested inheriting classes:

> (new (class (class object% (super-new) (displayln "superclass"))
         (displayln "subclass")))
(object:eval:4:0 ...)

In practice, this feature is quite useful since it lets you define mixins easily. That’s a topic for another blog post.

Side note: In other words, Racket has first-class classes. This is a term you might see used in the programming language literature.

Adding types to the fish program is easy. First, we can introduce a type definition for the fish class.

(define-type Fish%
  (Class (init [size Real])
         [get-size (-> Real)]
         [grow (Real -> Void)]
         [draw (-> Pict)]
         [eat ((Instance Fish%) -> Void)]))

This type definition says that Fish% is a class type with the given initialization argument and methods types. Note that the type of the fish class Fish% is not the same as the type of its instances (Instance Fish%). This is an important distinction to make, since both the fish class value and fish object values may appear in the same program.

Side note: If you’re familiar with Typed Racket, you may be surprised that the recursive reference to Fish% in the type definition works. Future versions of Typed Racket will support implicit recursive type definitions.

With the type definition in hand, we can just annotate the class value with the type:

(: fish% : Fish%)
(define fish%
  (class object%
    (init size)
    (: current-size Real)
    (define current-size size)
    (define/public (get-size)
    (define/public (grow amt)
      (set! current-size (+ amt current-size)))
    (define/public (eat other-fish)
      (grow (send other-fish get-size)))))

We do need an extra type annotation on the private field because its type isn’t included in the type definition above. This fish definition is a bit boring, so let’s spice it up a bit by making fishes drawable:

(define-type Fish%
  (Class (init [size Real])
         (get-size (-> Real))
         [grow (Real -> Void)]
         ; a type for the new method
         [draw (-> Pict)]
         [eat ((Instance Fish%) -> Void)]))
(: fish% : Fish%)
(define fish%
  (class object%
    (init size)
    (: current-size Real)
    (define current-size size)
    (define/public (get-size)
    ; new draw method
    (define/public (draw)
      (standard-fish (* current-size 10)
                     (* current-size 5)))
    (define/public (grow amt)
      (set! current-size (+ amt current-size)))
    (define/public (eat other-fish)
      (grow (send other-fish get-size)))))

The new draw method that’s been added to the fish relies on some functions from the pict library. Since that’s not currently included in Typed Racket’s standard libraries, we need to give its exports some types:

; this would go at the top of the file before fish%
(require/typed pict
               [#:opaque Pict pict?]
               [standard-fish (Real Real [#:color String] -> Pict)])

The #:opaque import form in Typed Racket lets you create a new type that corresponds to some predicate, in this case pict?. It’s useful for bringing in datatypes from dynamically-typed Racket libraries. With that new type, we can give a type for the standard-fish function.

In the end, you can interact with some fish and draw them:

> (define dory (new fish% [size 5]))
> dory
- : (Instance Fish%)
(object:fish% ...)
> (send dory draw)
- : Pict
> (send dory eat dory)
> (send dory draw)
- : Pict

Conclusion: At Racket Salon, I talked about a few other things including mixins and converting a slightly larger program that uses the GUI library. They didn’t translate well to a blog setting so I didn’t include them here. Racket Salon was a fun event, so I encourage anyone in the Boston area to attend!

Typed Racket’s support for classes will land in a future version of Racket, possibly the release after v6.0.

more →

Made with Frog, a static-blog generator written in Racket.
Source code for this blog.