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 me. 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 Python or an import in C. As it turns out, the lang line specifies the language used by the interpreter. 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 interpreter.

Alternatively, we can download the latest version of Racket and get a copy of the solution. 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.

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.

Series Navigation← Hello World in SchemeHello World in Haskell →
Advertisements

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.