This will be a skeptical, shallow-dive into Haskell. I just want to take a single-day look at the Haskell language and see what it’s all about. If you have spent a decade using Haskell in production and know all of its deep secrets, you will probably hate me for how much of your beautiful temple I am missing out on.
The first time I encounter a new language, I tend to bang out a few trivial examples just to get the flavor of things.
Trivial Example 1: Hello World
Okay, in Python, this is beautiful and short:
It is somewhat less short in Java:
Let’s see what this looks like Haskell:
Not bad: simple and lightweight, though
putStrLn lacks elegance.
Trivial Example 2: Fibonacci Function
Okay, the first function I usually try to build in a new language calculates the Nth term in the Fibonacci series. Since I am just trying to get the flavor of the language I don’t worry about overflow, performance, memoization, etcetera.
Here it is in Stupid Python:
Here it is in slow, recursive C:
And actually, my usually trivial example in Haskell wasn’t as quick to construct as I was expecting. But it is highly readable:
Okay, but what’s a Haskell?
- a programming language designed by an academic committee
- purely functional
- statically typed (with automatic type inference)
- compiled (though there is an interpreter)
- no side effects! (Is that really possible?)
Okay, Haskell has lists, tuples, and even whiffy hash maps. But it’s a list-first language. So let’s take a look at these lists that want to rule the world.
Okay, let’s write a function that takes a string (which is just a list of
chars in Haskell, and makes it lower case:
Okay, Haskell’s lists appear to be singly-linked lists. But they have a ton of functions built in to make them useful. Cool.
Haskell’s type system is broad and deep. Not only does Haskell have the usual types for numbers and booleans and the like, it also (very literally) considers functions to have types. For whatever reason, we use
:t to find the type of a thing:
So, not only can you have types like
Char, you can have types like
Ord which return a Boolean. And, of course, you can create your own types. And ensure that functions you create have the types that you want. I feel like types are doing double duty in Haskell, but I feel like I’d need to use the language for longer to be able to tell.
So Far I Hate
ghci is the GNU interpretter for Haskell. Okay, you have my attention, I like all of those words.
Oh wait, no, I hate it.
You see, in most interpretters you write code in your language of choice. For instance in Python you can ask for help in the interpreter by doing:
Easy, right? You did that by using Python code. The
help statement is just a Python function that you are calling. Okay, now let’s try this in
Okay, wait, but
:? isn’t Haskell code. It is it’s own special syntax that you have to learn just for the
hgci. So as a newbie learning Haskell you have to learn all the language syntax and another whole collection of syntax just for the
Also, you can’t do multi-line programs in the
hgci. Here is what our simple
factorial function looks like in the
Also, it says
prelude> all over the place for now reason. Which gets old.
No thanks. Hard pass.
And That’s Everything
That’s it, I covered the entire language.
I don’t even know what a Monad is yet! An Applicative Functor? What?
Okay, I thought that since Lisp (and MIT-Scheme) had such little syntax to learn, Haskell would be fast to spin up on. And so far Haskell seems more like a fully fledge modern language (Java, not C++ or Scala), with a lot of syntax to learn.
References for Further Reading
I don’t know if I will be reading more about Haskell, it didn’t endear itself to me immediately. But here are some references I found early on: