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
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.
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 member. 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:
- Functional Programming, Simplified by Alvin Alexander
- An Introduction to Functional Programming Through Lambda Calculus by Greg Michaelson
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.
Earlier this year, I had an idea to automate the Sample Programs website. Turns out, it wasn't that hard to do. Now, the website is automatically built on every pull request. Let's talk about...
This week, I made a quick article about a few of the things I hear a lot about what you need to know to be a developer. Not everything is true.