Welcome back to another installment of the Hello World in Every Language series. At this point, we’ve covered 12 of the more popular languages like Python and Ruby. Today, we’re going to drift away from the industry languages a bit. In fact, we’re going to start playing with functional programming languages. Up first is Hello World in Lisp!
If you’re interested in learning more about Lisp beyond what you’ll find in this article, I recommend heading over to my Lisp Programming Language article. In it, you’ll learn more about the underlying structure of Lisp including the design of the interpreter. If you’re into that sort of thing, check it out!
Table of Contents
At this point, it’s probably no surprise that I took to Wikipedia to learn some more about Lisp.
According to Wikipedia, Lisp is actually a family of languages. In other words, Lisp has many dialects. For the purposes of this exercise, we’ll be using Common Lisp.
That said, let’s talk about Lisp in general. As it turns out, Lisp, a language developed in 1958, is the second oldest high-level programming language. The only older language is Fortran. Since its inception, the language has split into several dialects. Perhaps some of the most notable dialects are Scheme, Common Lisp, and Clojure.
In terms of features, Lisp differs wildly from the languages we’ve already covered. For example, all data in Lisp is represented with expressions – in particular, symbolic expressions. These expressions are written in prefix notation:
(+ 3 6 11)
In infix notation, the above expression reads:
(3 + 6 + 11)
So, the expected result is 20.
In addition, Lisp is heavily list based. In fact, Lisp is short for List Processor, so it should be no surprise that lists play an important role in the language.
Implementing a list in Lisp is rather simple:
(list 1 5 2 1)
Here. we’ve generated a list of four elements: 1, 5, 2, and 1. In fact, we can even nest lists using the prefix notation:
(list 1 (list 5 2) 1)
The resulting list would look like the following:
(1 (5 2) 1)
Keep this syntax in mind when we get to functions.
Have you ever played with lambda expressions in other languages like Java or Python? Well, Lisp has them too:
(lambda (arg) (* arg 2))
In this lambda expression, we simply multiply an argument by 2. If we wanted to use the expression, we would have to pass a value to it:
((lambda (arg) (* arg 2)) 10)
If this is confusing, remember that everything is in prefix notation, so this code might look something like the following in a language like Python:
foo = lambda n: n * 2 foo(10)
Notice, however, that we have a named function in the Python example and an anonymous function in the Lisp example. Don’t worry though. Lisp has named functions as well.
In Lisp, a named function is essentially a lambda expression that is stored in a symbol:
(defun foo (arg) (* arg 2))
In this example, we’ve created the exact same lambda expression, but we’ve stored it in a function called foo. We can then call foo from anywhere in our program:
How cool is that? I think I’m starting to like Lisp. Of course, we haven’t even gotten to implement Hello World in Lisp, so we should probably get to that.
For the purposes of this exercise, we’ll be using the latest Steel Bank version of Common Lisp.
Hello World in Lisp
Unfortunately, Lisp has many flavors which means the following implementation of Hello World will likely only be applicable to handful of those flavors:
(format t "Hello, World!")
That said, I’m happy to dig into this implementation of Hello World in Lisp.
First things first, we have the format keyword. In Common Lisp, format is basically the equivalent to printf in C. It basically takes some string and outputs it to some destination.
That brings us to this mysterious letter t. According to gigamonkeys, t is actually the destination of the output. More specifically, t indicates standard output. Another option is NIL which causes the string to be returned.
Finally, we have our Hello World string. This is obviously what gets printed to standard output.
How to Run the Solution
If we want to try it ourselves, we can copy the code above into an online Common Lisp compiler. The one I linked is in CLISP, but it gets the job done.
sbcl --script hello-world.lsp
And, that should produce the “Hello, World!” string on the command line.
Sample Programs in Every Language
Looks like that’s the end of another chapter of the Hello World in Every Language series. Once again, I got a little caught up in language features. Though, I figure that’s probably the point of this whole series after all. I’ve learned quite a lot already, and there’s plenty to still cover.
As I mentioned last time, I think I’m going to stick with functional languages for a few days. Up next, I’d like to take a look at Scheme. Then, maybe I’ll start playing with some of the newer functional languages. Stay tuned!
As always, if there’s a language you want to see, drop it below in the comments. If you’re still looking for ways to support my journey, make sure to share this article on social media. Every little bit helps!
I've seen a lot of folks share code on Discord, but some ways are better than others. Let's compare a few of the different ways.
Today, we'll be learning recursion through the lens of from Bloom's Taxonomy, a tool educators use to make curriculum.