Sep 3, 2015 02:20 PM
(This post was last modified: Nov 18, 2015 07:54 PM by SIM-2015410.)
[img]http://s9.postimg.org/57pm7cwxr/LEGENDS_2_0_HEADER.png[/img]
Haskell - Tutorial 1
Functions and If statments
Haskell - Tutorial 1
Functions and If statments
I think it appropriate to begin this tutorial series with a tutorial on functions - the basis for functional languages.
[img]http://s9.postimg.org/jzubyp2v3/LEGENDS_2_0_DELIMETER.png[/img]
How functions are defined
In Java or C# style syntax, we would define a function like this:
Code
returnType functionName (parameters) {
//function body
return someValue;
}In Haskell, we define a function like this:
Code
functionName parameters = someValueHere is a really basic example program to start with, it takes one parameter (x) and 'returns' (x + 2)
Code
addTwo x = x + 2It is usually a good idea to write explicit types for all your functions to make sure they work as expected, so here is how we do that:
Code
addTwo :: a -> a
addTwo x = x + 2This now says that we have a function which takes a parameter of type a and returns a value of the same type. But a could be anything - a character, an integer, a boolean, even an IO action. Our function obviously only works with numbers because it doesn't make sense to add 2 to false. So we can add in a Type Constraint, to say that a has to be a member of the typeclass Num (all numeric types are a part of the Num typeclass).
Code
addTwo :: (Num a) => a -> a
addTwo x = x + 2Now, let's define a function which takes 2 parameters. This will introduce a new concept, called 'currying', but don't worry if you don't fully understand it yet. We will define a function which takes 2 variables and adds them together:
Code
addTogether :: (Num a) => a -> a -> a
addTogether x y = x + yThat seems pretty straightforward, except for the type definition. Why so many arrows? For now you can think about this as just taking a parameter, then taking another parameter, then returning a value. Because, if you want to get into the picky details, all Haskell functions only take one parameter! In reality, this addTogether function takes a paramater, and returns a new function which takes a parameter, and returns a value. This is known as currying. Lets work through an example. Let's say we want to add 5 and 6 together using this function. We would do that like this:
Code
Prelude> addTogether 5 6Code
addTogether :: (Num a) => a -> a
addTogether y = 5 + yCode
addTwo' :: (Num a) => a -> a
addTwo x = addTogether 2 x[img]http://s9.postimg.org/jzubyp2v3/LEGENDS_2_0_DELIMETER.png[/img]
If statements
Let's look now at If statements, a vital part of programming logic.
Code
eatHowMany :: Int -> Int
eatHowMany hunger = if (hunger > 5)
then 2
else 1Pretty self explanatory, it is just a simple if-then-else statement. The function takes a 'hunger' amount (which is just an integer), and if it is more than 5 we return 2 otherwise we return 1. A couple of things to note:
- We have used Int instead of a in the type definition of the function, because we only want to accept Int values
- Indentation is important in Haskell, and it improves readibility if the then and else clauses line up. Unfortunately, HackForums code boxes aren't the best for indentation and formatting.
We can also put the entire if statement on one line like this:
Code
eatHowMany hunger = if (hunger > 5) then 2 else 1[img]http://s9.postimg.org/jzubyp2v3/LEGENDS_2_0_DELIMETER.png[/img]
Constants
In a functional languages, constants are just functions which don't take any paramaters. So they are defined in the same way. We usually don't bother with a type definition, but it is perfectly fine if you do:
Code
numberTwo :: Int
numberTwo = 2There is no such thing as a 'variable' in Haskell, because once you declare something with a certain value, you cannot change it. If you could change the value of a variable inside a function, that function would then have side effects and wouldn't be pure. You might see this as a limitation at the moment if you come from OOP or imperative languages, but this isn't a limitation - it is just an example of the differences between the 2 paradigms. We will see later how this plays out in functional languages when we come to recursion.
[img]http://s9.postimg.org/jzubyp2v3/LEGENDS_2_0_DELIMETER.png[/img]
This tutorial was written by MeshCollider 🤝
This tutorial was written by MeshCollider 🤝



![[Image: Xiuymkc.png]](https://i.imgur.com/Xiuymkc.png)