Hello World in Lisp

Hello World in Lisp Featured Image

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

Lisp Background

At this point, it’s probably no surprise that I took to Wikipedia to learn some more about LispOpens in a new tab..

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.

Expressions

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.

Lists

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.

Lambda Expressions

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.

Functions

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:

(foo 10)

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 LispOpens in a new tab..

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 gigamonkeysOpens in a new tab.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 compilerOpens in a new tab.. The one I linked is in CLISP, but it gets the job done.

Alternatively, as mentioned before, we can download a copy of Steel Bank Common LispOpens in a new tab. as well as a copy of the solutionOpens in a new tab.. Assuming SBCL is in the path, we can run a lisp file like a script as follows:

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!

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, playing Overwatch and Phantasy Star Online 2, practicing trombone, watching Penguins hockey, and traveling the world.

Recent Posts