All you need to know about Programming

Haskell 105: Ifs and other expressions

Before we get into recursion, we need to see some basic expressions first, so that you can increase your portfolio.

The first one we will see is “if then else”. This is a logical expression, basically it means: If something then something or else other thing.

Example:

When you have if something then True else False you can delete if then True else False. So we can write isOne as:

If you realize, the function (==) returns a bool ((==) :: Eq => a -> a -> Bool), the function isOne returns the result of (==)

The second expression is something called guards. These are a simpler way of writing:

With guards you can write this in this way:

As you can see, this is a simpler way to write one2or3 and easier to understand your writing.

Next we will cover recursion, farewell! :)

Haskell 104: Lists and their representation

We have already seen how you declare types on lesson 101, however, there’s a type that might be one the most fundamental of Haskell, Lists.

A list is not an actual type, like we have seen before, but we will get to that in a bit, first we will see the concept of what is actually a list.

Let’s imagine that instead of having an Int, you want several Ints to be one Argument, for example, the student IDs of your class:

  1. Andrew – 64332
  2. Charles – 99299
  3. Daisy – 74821
  4. Mary – 13882
  5. Tom – 48651

Haskell provides a way to arrange several Arguments into one big Argument which we call a List, and we represent it like this:

In this case we would have:

Now you can pass this as an Argument and work on it more easily. I’ll talk about this later because it is related with recursion, which will be our next topic. To finalize I want to show you diferent ways to write a List:

A list can be defined in the following ways (let’s assume that the List we want to represent is class)

However, if our Argument is a String, it is the same thing as saying [Char]:

P.S.: Don’t try to compile the code I wrote above because, naturally, it will give errors.

Haskell 103: Operators

You can’t do anything without operators, they are fundamental for comparing things, add them together, etc. I’ll explain some of them but you can get a big list of them over here:

Operators

The most basic and those you will use mostly are:

  • >,<,==,>=,<= These are very straight forward, they can be used for comparing things, for example

  • +,*,-,/ Again, very simple:

  •  &&, ||, not These are logical operators, && means And, || means Or:

You need to have some attention on these logical operators, because they operate differently. With an &&, if one of the operations fails, it returns False, like this:

0 = False | 1 = True

I’ll talk later about one operator, ++, which concatenates lists and strings, but since we haven’t talked about lists, I’m going to leave it for when I do.

Farewell

Haskel 102: Expressions and Functions

In Haskell, things are defined in the following way:

e :: T

Where e stands for expression and T for Type. What this means is that whenever you want to define something, you need to have those two components.

A function is constituted by Arguments and Definition. What this means is that without these two things, your program will not work (we will later know that this is not entirely true), but lets get practical.

You define a Type or in this case, an Argument by writing this:

And we now know that type1 is something of type String. This is important to understand, because this is how we define things.
Now if we write this:

We know that type1 is “Hello”, which is a String.

For now all of this doesn’t seem very useful, but lets talk about functions.
A function is defined in the following manner:

What we are saying is that the function sum is receiving 2 Int (Int->Int) and delivering as an Output another Int. The first line of the code are the Arguments of the function, and the second code is the DefinitionArguments are composed of one line only, but the Definition may (and it will) have more than one line of coding.

In this case, it is straight forward to understand what sum does:
It takes 2 Ints, x and y, and adds them together (x+y)

So if you write that in your .hs file and open it with ghci, it will give you an error because sum is already defined in the Prelude, so if you want to try this on your own write sum1, or whatever you want to call it. Now if you try sum 1 2, ghci will show you 3, which is the result of (1+2).

Haskell 101: Types

Alright, let’s start learning Haskell. I find Haskell to be a good first language as it is easier to program than other languages, like C. Haskell is different than most usual languages as it is a Functional language rather than a imperative language. I find this quote (from Learn Haskell for a Greater Good) to summarize very well this aspect:

Haskell is a purely functional programming language. In imperative languages you get things done by giving the computer a sequence of tasks and then it executes them. While executing them, it can change state. For instance, you set variable a to 5 and then do some stuff and then set it to something else. You have control flow structures for doing some action several times. In purely functional programming you don’t tell the computer what to do as such but rather you tell it what stuff is.

Now, as any language out there, Haskell as pre-defined types which let you describe what you want to show, for example:

  • “Hello World” is considered a String
  • 48 is an Int
  • 48.5 is a Float
  • is a Char
  • True is a Bool

Understanding this is fundamental because it is the basics of Haskell.

Haskell: Tools

For programming in Haskell, you’ll need some simple tools, yet necessary for compiling and actually seing the results of what you just wrote. For now we will work on these tools, but later I’ll introduce you to IDE’s… they save up time and make programming an easier job, as they complete identation and such.

For actual writing the code, I recommend Notepad++, and you can get it here:
Notepad++ 6.2.3

If you use mac, the best alternative I found out is TextWrangler:
TextWrangler

For compiling and testing your code, you’ll need GHCI:
Haskell Platform for Windows
Haskell Platform for Mac

Both are very simple to install, so I’ll give no instructions eheh.

A good book you can read online is:
Learn Haskell for a Greater Good

Introduction

I found out on my freshman year that people who never programmed in their lives, found out that it was very difficult to start programming all of a sudden, with great pressure from teachers to learn more about the language in question, without having in mind that most of students never programmed before.

Because of this, since I’m in Informatics Engineering and many pre-IT students start programming when they reach University, I’m creating this to help you guys on various programming languages, such as:

  • Haskell
  • C
  • Java

And others that I may learn on my College Life.
Farewell