Hello World in Haskell

Hello World in Haskell Featured Image

Thanks for swinging by for another installment of the Hello World in Every Language series. Today, we’re looking to tackle Hello World in Haskell, a general-purpose functional programming language.

Table of Contents

Haskell Background

With sixteen of these tutorials already out of the way, it will probably come as no surprise that I took to Wikipedia to learn more about HaskellOpens in a new tab..

As stated already, Haskell is a general-purpose functional programming language that first appeared in 1990. Despite its relatively old age, Haskell is still maintained. In fact, the latest standard was developed back in 2010. By 2020, a new update should be available.

In terms of features, Haskell supports strong, static typing, pattern matching, lazy evaluation, and even list comprehensions. To be honest, this is the first time I’m hearing about lazy evaluation, so I figured I’d dig into that a bit.

As it turns out, lazy evaluation is a technique which defers computation of a functions input until the value is needed. In other words, expressions are only evaluated when they are needed. If my explanation is bad, take a look at the article I linked above. It covers the concept in great depth.

At any rate, I think that’s plenty of background for now.

Hello World in Haskell

As usual, let’s get right to our implementation of Hello World in Haskell:

module Main where

main = putStrLn "Hello, World!"

And, that’s it!

First thing we will probably notice is that Haskell syntax is very different from Lisp and Scheme. Despite all three of those languages being functional, Haskell seems to have ditched the parentheses. In fact, even function calls lack parentheses in Haskell. That’s a new one for me.

After the syntax, the next thing we should probably look at is that first line. As usual, we have a module declaration which basically declares this file as the main file. In other words, execution begins with this module. We saw something similar in our Hello World in Go article.

Finally, we have our main function. For someone who has never played with anything like Haskell, this syntax is a bit bizarre. In fact, the main function doesn’t look like a function definition at all. At least, it doesn’t look like what we’ve come to expect from this series.

That said, the main function does make a lot of sense if we think about it in terms of mathematics. After all, math functions follow the exact same form: f(x) = x

At any rate, let’s get back to the code. In this final line, we have the main keyword which indicates the entry point to the program. From there, we compute the expression on the other side of the equals sign. In this case, we have a print function and our string, and that’s it. Pretty simple!

How to Run the Solution

If we want to run the snippets above, we can use an online Haskell compilerOpens in a new tab.. All we have to do is drop the code into the editor and hit run.

Of course, we can also run the code locally if we just grab a copy of the latest Glasgow Haskell CompilerOpens in a new tab.. While we’re downloading stuff, we should probably get a copy of the solutionOpens in a new tab..

Assuming Haskell is now in the path, we can compile and run our solution using the following commands:

ghc hello-world.hs
hello-world.exe  # Windows
./hello-world  # Unix/Linux/Mac

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

Sample Programs in Every Language

Well, once again, we’ve implemented Hello World but this time in Haskell. I have to admit that this one was another challenging one for me. After all, I have no background in functional programming. That said, I’m having a lot fun.

Up next, we’re going to take a look at Perl. After that, I’m looking to take on Scala and Elixir. Then, I’ll be doing my first user-requested language, Pascal. If you have a language you want to see, let me know in the comments below.

As usual, if you liked what you saw today, considered giving it a share. This helps the series reach new people who would probably benefit from exploring it. Thanks in advance!

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