Hello World in Racket

Hello World in Racket Featured Image

Welcome back to another tutorial from the Hello World in Every Language series. This time, we’re looking to keep our functional streak going with Hello World in Racket, a language which first appeared in 1994.

Table of Contents

Racket Background

Well, at this point, Wikipedia has yet to fail meOpens in a new tab.. As usual, I referenced it to learn a little bit more about today’s language.

Like Python and Java, Racket is a general-purpose programming language. Unfortunately, that’s sort of where the similarities stop. After all, Racket comes from the Lisp-Scheme family, so it resembles a typical functional programming language. In other words, expect plenty of parentheses.

What makes Racket different from its Lisp counterparts is its extensibility. In other words, the language can be easily modified using macros. Remember when we learned about macros in Rust? Same idea. We can use these macros to control the syntax of Racket.

Macros alone aren’t interesting enough to warrant any excitement. However, mix macros with a module system, and we get an extremely versatile language. These modules allow us to import various macros that can be used to control the dialect of Racket. For instance, if we wanted a statically typed version of Racket, there’s a module for that: typed/racket.

Hello World in Racket

Alright, let’s go ahead and dig into our implementation of Hello World in Racket:

#lang racket/base
"Hello, World!"

Up first, we have this peculiar line that looks kind of like a comment in PythonOpens in a new tab. or an import in COpens in a new tab.. As it turns out, the lang line specifies the language used by the interpreterOpens in a new tab.. In fact, I already mentioned that there’s a module which provides syntax for static typing in Racket.

In this case, the language we have chosen is racket/base. This only provides us the core Racket functionality. As an alternative, we could have easily specified racket alone.

Finally, we have our print line. To be honest, we could have used the print functionality:

#lang racket/base
(print "Hello, World!)

However, I wanted to show that you can implement Hello World without the mess of parentheses. That’s because Racket automatically prints constants. If we had a slightly more complicated expression:

#lang racket
+ 2 2

We would see the three constants returned to us in their stack order:

#<procedure:+>
2
2

We would need parentheses to actually evaluate this expression.

How to Run the Solution

At any rate, I think we’re done here. If we want to try to run the solution, we can plug some of this code into an online Racket interpreterOpens in a new tab..

Alternatively, we can download the latest version of RacketOpens in a new tab. and get a copy of the solutionOpens in a new tab.. Assuming Racket is now in the path, we can just run the following to execute Hello World in Racket:

racket hello-world.rkt

And, that’s it. If successful, the “Hello, World!” string should print to the console.

Sample Programs in Every Language

As always thanks for stopping by! If you enjoyed this article, make sure to share it with your friends. Also, check out the complete alphabetized archive and be sure to recommend the next language in the comments.

While you’re hear, I’d love it if you became a memberOpens in a new tab.. Your membership goes a long way to ensuring projects like these stay active. Otherwise, I wouldn’t have enough resources to maintain them.

If you can’t support the site directly, you can always do your normal purchasing through Amazon using one of my affiliate links. To get you started, the following links are a few books I found that you may like:

So, what’s next? Just a few days ago, I started toying with functional programming languages. So far, I’ve implemented Hello World in Lisp and Scheme. After Racket, I think I want to toy with just a couple more functional languages. In particular, I’d like to try out Haskell, Perl, and Scala. Then, I might do one more article on a newer functional language like Elixir.

Sample Programs in Every Language (44 Articles)—Series Navigation

For 100 Days of Code, I’ve decided to implement a few sample programs in as many languages as possible. Each implementation details a brief history of the language and a description of the code.

The plan for the series is to explore the major general-purpose language like Java, Python, C, C++, and C#. From there, we’ll take a look at some sample programs in web development languages like Ruby, PHP, and JavaScript. As we continue, we’ll cover proprietary languages like Swift and Objective-C. Eventually, we’ll start to tackle less popular languages like Rust, x86, and Verilog. Finally, we’ll play around with some of the esoteric languages like Brainf*ck and LOLCODE.

Who knows? Maybe the Sample Programs in Every Language series will become so popular it’ll never end. To help this series grow, consider sharing it on social media with your friends. Or, if you have a language you want to see, drop your suggestion in the comments.

Jeremy Grifski

Jeremy grew up in a small town where he enjoyed playing soccer and video games, practicing taekwondo, and trading Pokémon cards. Once out of the nest, he pursued a Bachelors in Computer Engineering with a minor in Game Design. After college, he spent about two years writing software for a major engineering company. Then, he earned a master's in Computer Science and Engineering. Today, he pursues a PhD in Engineering Education in order to ultimately land a teaching gig. In his spare time, Jeremy enjoys spending time with his wife and kid, playing Overwatch 2, Lethal Company, and Baldur's Gate 3, reading manga, watching Penguins hockey, and traveling the world.

Recent Posts