Haskell Beginner

I am a beginner here. I used Haskell back in 2015, started “getting it”, then left the language as other priorities came up. I don’t do any functional programming at work so I am back to square one. This post is about getting my machine back up to speed, and firing off the first tutorial.

This series will start off slow, I am learning, so bear with me. You are welcome to wait awhile and come back later. Some of these posts may be basic, and possibly wrong, to begin with.

My setup

I opted in to Manuel Chakravarthy’s application on the Mac called Haskell for Mac. Although I use Visual Studio Code for just about everything, I selected this as it looked beginner friendly. The application has a bunch of starter projects, which I will go through. If you want to follow along with me, you may need to opt in to this application as well since I am not sure if the starter projects are available anywhere else; they could well be though (check the Haskell introduction, it appears to be a great resource).

Firing up Haskell for Mac

Launching the Haskell application will provide you with the usual welcome dialog. Here’s why I chose this, note the “Learn Haskell & Libraries” option:

alt text

From this option I can go through the learning process effectively. Here’s a rundown of what’s available there:

alt text

Clicking any of the items on the left will open up relevant Web pages. The sample projects on the right will open those projects in the application. This is where I’ll head as I am impatient and excited. Let’s fire up the first one, “FirstSteps”. You should see this:

alt text

From here you need to expand the FirstSteps under Modules to see the included Haskell files:

alt text

And there you have it, the first bit of Haskell code! I’ll be honest here and say that reading this made sense to me. If you are looking at that code and wondering what’s going on, you are not alone. It’s just fortunate that this first project is about as simple as it can be in functional land. If my previous foray in to functional programming is anything to go by, things will get hard, fast. So let’s rather dwell on this program for a bit and truly understand what is going on.

The very first line, as the comment says, is a type signature. We are defining a function signature here for inc with Int -> Int. This latter part is a bit confusing at first, however you are defining the input type/s and return type of the function. It takes an Int, and returns an Int. We don’t know what the function does yet, but we know it exists.

The very next line defines what the function does. Functions in Haskell (or functional languages) are equations essentially. But don’t run away if your maths is as rusty as mine! You don’t need to be a mathematician to program here. The beauty of functional programming is that you compose equations of many things, like increments, averages, means, divisions, and string them together to make more complex equations. But I digress, this line is an incrementer. Take an Int value and increase it by 1. Pretty simple. The function also declares the variable name/s of the input variable/s to that you can refer to them in the function. In this case x is the name of the variable we will increment. Reading Manuel’s site, they define the function with a head (left of the equals) and body (right of the equals).

Move over to the pane to the right of the code and you will see a playground. This allows you to run arbitrary code against your module and updates as you edit, giving you real time feedback. This is probably the second most valuable thing about this application:

alt text

You can get this in Visual Studio Code. Figure out how if you wish. Come back once you get that going.

Understanding the code

Let’s be real, that first line is hardly rocket science right? There are two more type definitions further down. The only complexity comes in with the average. Let’s list the code so far:

inc :: Int -> Int     -- type signature
inc x = x + 1         -- function equation

exclaim :: String -> String
exclaim sentence = sentence ++ "!"

average :: Float -> Float -> Float
average a b = (a + b) / 2

You’ll see that average contains three Float items. What does this mean? It means that average takes 2 parameters of type Float, and returns a Float. That’s it. Interesting question, can we return more than one value from a function? The answer which we will hopefully see later, is no, but also yes, sort of, because tuples.

Manuel has a great write up on the above sample on his application’s site. He will do more justice to the detail of what is happening. Since I am impatient, I am purely expressing this in my way, and this may not be the way you learn. That is okay. If you want the detail, please head over to Manuel’s site.

The rest of this sample code is reasonably straightforward. I like that it’s a few common ideas and how to express them in Haskell. At its simplest, functional programming is easy. Later on is where we go cross-eyed; or at least, I do.

The playground

Let’s head on over to the playground on the right. Here’s the code as it’s initially set up:

inc 41

exclaim "Haskell"

average 10 20

let sum :: Num a => [a] -> a
    sum = foldl (+) 0 
sum [1..10] :: Int

let incList = map (+1)

-- Whatever you print goes
-- to the console below.
putStrLn (exclaim "Welcome to Haskell")

The first three “calls” are directly related to our code on the far left. Here we are calling the inc, exclaim and average functions we defined. Mostly the results on the right should make sense. Incrementing 41 by 1 will give us 42. Exclaiming “Haskell” results in “Haskell!”. Wait, I didn’t go over this one! Here’s the definition:

exclaim :: String -> String
exclaim sentence = sentence ++ "!"

The ++ is the only interesting bit; it’s a built-in string concatenation function. The rest should fall in to place.

I got confused with some other syntax introduced in the playground. The sum function was a little more complex, and since it was not in the module code, I thought I’d missed something:

let sum :: Num a => [a] -> a
    sum = foldl (+) 0
sum [1..10] :: Int

The syntax is slightly different here. They declared the other function definitions through a type declaration and then a function definition. Here, they’re one. We are defining the function sum and its definition together. I think the let keyword allows this. You know what? This will be in a future post. I will also include the exercise results from the First Steps tutorial on Manuel’s site, along with the process I go through to solve it.

I also do not understand what foldl does yet, but it seems like we’re adding 0 to each element and adding each element to each other somehow. Hopefully, I’ll figure it out in a future post.

Haskell Introduction

I cannot learn the entire language from this one snippet alone. There is a lot going on in that example. How do I know what else there is to know? Here’s where Manuel’s site comes in handy. I’ll head over to the first page and start working through it as it’s related to this first step example. This will be in my next post.

If there’s anything else you want me to touch on, let me know via email.


1308 Words

2019-03-05 03:10 +0000