![]() |
|
[Tutorial] Haskell - Part 1 - Functions, If statements and values [LEGENDS] - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Coders Lounge (https://hackforums.net/forumdisplay.php?fid=5) +---- Thread: [Tutorial] Haskell - Part 1 - Functions, If statements and values [LEGENDS] (/showthread.php?tid=4973489) Pages: Pages
1
2
|
Haskell - Part 1 - Functions, If statements and values [LEGENDS] - SIM-2015410 - Sep 3, 2015 [img]http://s9.postimg.org/57pm7cwxr/LEGENDS_2_0_HEADER.png[/img]
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) {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 -> aThis 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 -> aNow, 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 -> aThat 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 -> aCode addTwo' :: (Num a) => a -> a[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 -> IntPretty 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 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 :: IntThere 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 🤝 * - Unknown - Sep 3, 2015 Great thread! I've always wanted to learn Haskell * - scam. - Sep 3, 2015 Nice one mate. I'd like to learn haskell just as a learning experience. Looks really interesting but seems a little confusing. * - g4143 - Sep 3, 2015 One important thing to note. In Haskell the 'if' is an expression which returns a value and that has to be considered when you use it it in your programs. * - SIM-2015410 - Sep 3, 2015 (Sep 3, 2015 10:01 PM)g4143 Wrote: One important thing to note. You are correct, thanks for the clarification :) * - Tyranus. - Sep 3, 2015 Very HQ thread, i'm glad I stumbled upon this because i'm actually learning Haskell right now. Bookmarking for later use. * - g4143 - Sep 3, 2015 (Sep 3, 2015 10:08 PM)ӍeshCollider Wrote: You are correct, thanks for the clarification :) How does Haskell handle the 'if' problem is this pseudo code? Code let ans = if false then 5* - Ʀiven - Sep 3, 2015 Is Nex the number one student? * - SIM-2015410 - Sep 3, 2015 (Sep 3, 2015 10:20 PM)Ʀiven Wrote: Is Nex the number one student? I don't believe Nex intends on learning Haskell sorry. Hope that answers your question. (Sep 3, 2015 10:16 PM)g4143 Wrote: How does Haskell handle the 'if' problem is this pseudo code? You have to have an else statement, there is only if-then-else in haskell, for the reason you have above. if is an expression which has to be converted to a value * - g4143 - Sep 3, 2015 (Sep 3, 2015 10:29 PM)ӍeshCollider Wrote: I don't believe Nex intends on learning Haskell sorry. Hope that answers your question. OK. I come from a bit of OCaml where they allow an 'if - then' but only if the 'then' branch returns a unit type because the implied else will return a unit type. These are the little things which turn off the traditional programmer from a more functional environment. Its always the simple things... * - SIM-2015410 - Sep 3, 2015 (Sep 3, 2015 10:40 PM)g4143 Wrote: OK. I come from a bit of OCaml where they allow an 'if - then' but only if the 'then' branch returns a unit type because the implied else will return a unit type. There are some useful functions in Control.Monad package which you might want to take a look at :) * - g4143 - Sep 3, 2015 (Sep 3, 2015 10:46 PM)ӍeshCollider Wrote: There are some useful functions in Control.Monad package which you might want to take a look at :) No thanks. Haskell is too over the top for me. I'll stick with my brand of hipster languages - OCaml and Clojure. * - SIM-2015410 - Sep 3, 2015 (Sep 3, 2015 10:53 PM)g4143 Wrote: No thanks. Haskell is too over the top for me. I'll stick with my brand of hipster languages - OCaml and Clojure. At least you are well rounded in different paradigms, that is all I ask 🤝 * - g4143 - Sep 4, 2015 (Sep 3, 2015 10:59 PM)ӍeshCollider Wrote: At least you are well rounded in different paradigms, that is all I ask 🤝 I think everyone should expose themselves to a Lisp or ML style language. They'll end up a better programmer for it in the end. * - scam. - Sep 4, 2015 (Sep 4, 2015 12:57 AM)g4143 Wrote: I think everyone should expose themselves to a Lisp or ML style language. They'll end up a better programmer for it in the end. I am going to have to learn lisp because Richard Stallman says it is good. 😉 |