![]() |
|
[Tutorial] Haskell - Part 2 - Recursion, comments, lists and tuples [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 2 - Recursion, comments, lists and tuples [LEGENDS] (/showthread.php?tid=4973493) |
Haskell - Part 2 - Recursion, comments, lists and tuples [LEGENDS] - SIM-2015410 - Sep 5, 2015 [img]http://s9.postimg.org/57pm7cwxr/LEGENDS_2_0_HEADER.png[/img]
Haskell - Tutorial 2 Recursion, comments, lists and tuples In this tutorial, I will go over recursion in Haskell, show how comments are written, and give an introduction to another major component of the language: lists. [img]http://s9.postimg.org/jzubyp2v3/LEGENDS_2_0_DELIMETER.png[/img]
Recursion In the previous tutorial, I mentioned that you cannot change the value of something once you have defined it. Those of you who come from OOP or imperative language backgrounds may think of this as a roadblock at first, but I will demonstrate the fact that this isn't necessary in Haskell. Let's begin with an example. Here is a basic factorial function in Haskell: Code factorial :: Int -> IntI will point out a few new things:
Code factorial 3 --not 0, not 1 so we use the third function body.[img]http://s9.postimg.org/jzubyp2v3/LEGENDS_2_0_DELIMETER.png[/img]
Comments Single line comments in Haskell look like this: Code -- this is a commentmultiline comments in Haskell look like this: Code {- this[img]http://s9.postimg.org/jzubyp2v3/LEGENDS_2_0_DELIMETER.png[/img]
Lists Lists are a very important part of Haskell programs, so I will cover them here before we get on to list comprehensions in the next tutorial. A list is similar to what is called an 'array' in Java, all elements in the list have to be the same type (e.g. Int, Char, Bool, or even lists of lists, lists of functions - the functions must all have the same type, etc.). This is what a list looks like in Haskell: Code coolList = [1,2,5,73,2]This is an empty list: Code emptyList = []To prepend elements to a list, we use the : operator like this: Code 1:[2,3,4] == [1,2,3,4]To combine two lists into one, we use the ++ operator like this: Code [1,2,3] ++ [4,5,6] == [1,2,3,4,5,6]An example of a list of functions, all of which have type (a -> a): Code anotherList = [(+ 3), addTwo, (addTogether 5)A string is just a list of characters, so these two declarations are the same: Code listOne = ['h','e','l','l','o']A list of list of lists: Code [ [ [1,2], [3] ],[ [4,5,6,7], [8,9], [10,11] ] ]We can retrieve the nth element of a list like this: Code aList = [2,5,2,1,3,3,2,5][img]http://s9.postimg.org/jzubyp2v3/LEGENDS_2_0_DELIMETER.png[/img]
Tuples Tuples are groups of values, similar to lists, but the values inside them can be of different types. Here is an example of a 2-tuple, better known as a pair: Code aTuple = ("Hello", 45)We can also have longer tuples, here is an example of a 3-tuple or a triple: Code aTriple = (1,True, 700)We can also have lists of tuples, for example: Code [(1,"Apples"), (2, "Oranges"), (3, "Milk")][img]http://s9.postimg.org/jzubyp2v3/LEGENDS_2_0_DELIMETER.png[/img]
This tutorial was written by MeshCollider š¤ * - Nutar - Sep 5, 2015 Great tutorial you've got there bud. Keep it up. You really got me into Haskell. * - g4143 - Sep 5, 2015 Important note for list and recursion. List are recursive by their nature. * - Remix - Sep 5, 2015 Nice tutorial Keep going š¤ * - csrk - Sep 5, 2015 (Sep 5, 2015 12:34 AM)ÓeshCollider Wrote: Lists are a very important part of Haskell programs, so I will cover them here before we get on to list comprehensions in the next tutorial. A list is similar to what is called an 'array' in JavaI'd like to expand your list explanation a bit! Lists are actually not that similar to arrays in java, they are more similar to LinkedLists in java. Lists in haskell are defined something like this Code -- Nil = empty node/list, Cons a (List a) = element a followed by the rest of the list.In java you can index an array and retrieve any value in O(1) time (Big-O notation), meaning that its a constant time. In haskell, when you are asking for an item in a list, you will have O(n) worst time complexity (where n is the number of items in the list). You have to first check if the first item is the one you want, and if it's not, check the rest of the list recursively. In java a LinkedList is defined as a head followed by the next item, so you have to loop through them in a similar fashion. This definition has both advantages and disadvantages, the most obvious disadvantage being the one i mentioned above. One advantage is that it's very easy to map functions to every element in the list. the function map is defined like this. Code map :: (a -> b) -> [a] -> [b]Code map (+2) [2,3,4,5]* - Nex+ - Sep 5, 2015 Very nice tutorial you got here Mesh |