../

Successfully Picking Up Racket

2024-02-17

I had nothing to do when I went home for the Chinese New Year, so I solved a few Advent of Code 2023 problems using Racket. My previous attempts to learn Scheme-family languages didn’t go very well; I met with repeated setbacks. However, this time it went very smoothly. Writing code suddenly felt natural, and I even learned how to write hygienic macros. Maybe something just clicked.

Initially, I wanted to use C to do these problems. But sure enough, as the problems got more complex, writing these things in C started to make me lose my sanity; especially string processing, which is the part C is least good at.

Compared to C, Racket is much more relaxed. At least you don’t have to spend half the day flipping through documentation just for a string-to-number function. But at the same time, Racket—or rather, Scheme—is comparable to C in terms of conciseness. Just looking at the number of pages in the standard documentation: ISO C is about 200 pages, while R5RS is only 50-odd pages, and R6RS, which is widely criticized for being “too complex,” is only a little over 100 pages. Racket is a “batteries included” Scheme variant with huge functionality, but the core of the language still maintains the Scheme tradition of being very refined. I have a half-page R5RS cheat sheet on hand, which is enough to handle the vast majority of daily functional requirements.

The “Zen of Python” mentions:

Explicit is better than implicit.
Simple is better than complex.

However, it is quite obvious that today’s Python hasn’t achieved this; instead, it runs in the opposite direction. I’ve never fully understood Python’s Metaclasses, and the various subtle pitfalls in its OOP scare me. Add to that gradual typing, async/await, and other new features, and nowadays I barely dare to say I know how to write Python. JavaScript has similar problems. As for C++, it goes without saying. Racket was born in academia and also pursues new functions and features, but the core part of the language is stable. When using Racket, you have great flexibility: you can stick to the core content similar to R5RS, or you can command the vast standard library and complex features like contracts and Typed Racket.

It’s not that I can’t adapt to complex syntax; I was even once very interested in becoming a C++ language lawyer. But alas, these things are gradually forgotten if not used for a long time, and picking them up again requires a lot of effort, like Sisyphus. Life is short, and nowadays I am even less willing to waste my already precious time on these obscure and useless details.

Racket’s performance is also impressive. Although it naturally can’t compare to the likes of C/C++ or Rust, and is roughly in the same tier as Java and NodeJS (and often slightly slower), it is far superior to scripting languages like Python and Ruby. This probably has to do with the “Racket on ChezScheme” work in recent years.

The last advantage is probably that Racket is very suitable for developing “cold-blooded software.” This concept was proposed in this blog post. The “cold-blooded” here doesn’t refer to a “cold-blooded killer,” but rather the cold-bloodedness of “cold-blooded animals.” Roughly speaking, it means it can hibernate—you can leave it for many years and still pick it up again without the APIs and SDKs having turned upside down, or finding that you can’t even configure the development environment anymore (this is very common in languages like Python and JavaScript; C++’s messy dependencies and build systems also lead to similar problems). But I have reason to believe that Racket is unlikely to experience similar phenomena (because the language core is very refined). I remember once downloading the source code of a Scheme interpreter from the 80s. At that time, the ISO C standard didn’t even exist yet, but with slight modifications, that code could still be successfully compiled and executed on a modern Linux system. This left a deep impression on me.

These are roughly my thoughts on successfully picking up Racket recently. I have some ideas for side projects recently and plan to try them out with Racket.


Mistivia - https://mistivia.com