Friday, February 28, 2014

Haskell Week 5


This week I have not made as must progress in the past with Haskell due mainly to studying for finals.

However, I did take the time to investigate some of the real world applications of the language.

It seems from my research that applications of  Haskell are mainly academic based. But some large companies such as Facebook and Google report that some of their in house tools are written in Haskell. Also a large banking company uses Haskell for some of their security features.

I was very interested in the security applications of Haskell but I was unable to find very much detail, perhaps unsurprisingly.

Friday, February 21, 2014

Haskell week 4


This week in Haskell I have started to look at two new aspects of Haskell programming that relate to list manipulation (surprise, surprise). These functions are map and filter.

Map:

Maps are very interesting because they are a quick and useful way to apply a function to each element of any list. For example if we had a simple list such as [1,3,5,7,9] and we wanted to add 1 to each of these values instead of looping through the list with a for loop, adding one to each value (i+1) and returning the list with the new values we can do this very quickly in Haskell (with one simple line).

For example:


This simple statement will give us the desired output of:


In this example I used a list of all Integers and mapped a function that added the Integer 1 to each element. Mapping can also be done to lists of type String. If we had a list of Strings such as ["Name", "Age","Sex",Social Security Number"] and I decided after the fact that I wanted at add a colon at the end of each element. Using a map this is how that could be done:


The syntax is done by using the map operation and then the "++" characters (which is the String concatenation operand in Haskell) and then placing the character we wish to append to the end in quotes (in our case ":"). We can see that we now have the output:



Filter:

Filters while similar to maps work by checking each element in list independently and seeing if it fits the criteria of the specified function. If the element falls into the criteria (the boolean True is assigned) it is added to a new list that will be returned once every element has been tested If an element does not fit the criteria of the function, it is not added to the final list that is returned. And it can be said to have been "filtered" out.

For example:

Say we have a list of elements [1,10,2,11,3,12,4,13,5,14] and we want to filter through this list and only return all the integers that are greater than or equal to 10. This can be done using a filter like so:


This would print out the desired output of a list that contains only elements that are 10 or greater. Output:

This is a very useful feature in Haskell since most things that we will be aiming to do involve lists in some way or another.

Stay tuned for more updates on my progress with Haskell!


Thursday, February 13, 2014

Haskell Week 3


Functions:

This week I have been focusing on looking at functions in Haskell. Since Haskell is a functional language, functions are unsurprisingly a very important cornerstone of the language. Functions are different in Haskell than in Java, C#, VB and Python. However, in my opinion they are a lot more intuitive and follow a very easy to understand pattern and logic. For example let's take a look at a simple function: the factorial operation.



In Haskell we start by giving the function a name followed by the :: characters and then a definition of the type of parameters that will be given to the function and the type of output that will be returned.

For the above example the function name was "factorial" (very creative I know) and then we used the :: character and defined that the function would take an Integer as a parameter and then return (->) another "Integer".

Once the function is defined we can create an instance of it and assign a variable to represent the Integer parameter, in this case I called it "n". Then I used the built-in product function and said calculate the product of each number starting with number 1 and going through n represented as [1..n].

I enjoy this logic based syntax very much. I think it is not only easier to write but also easier to read and understand. Once the function is defined and then an instance is created we can print out the result, like so:



Once this program is compiled and ran we can see that we get the correct output:

This idea of defining a function will become especially important to me as I begin to work on my Mid-range sorting program.

Stay tuned for more updates on my progress with Haskell!

Saturday, February 8, 2014

Haskell Week 2


This week I have been continuing my progress in Haskell. I enjoy experimenting with the language and I am happy to say that I have succeed in my goal from last week and I have found a plugin for eclipse that allows me to write Haskell code with all the power of eclipse.

Download link:
http://eclipsefp.github.io/install.html

This has been the most helpful step in my learning of the language because ever since I have been programming I have been using eclipse, now I am able to use the tools built into eclipse such as the quick complete and the quick fix features.

This week I have been focusing on the topic of Pattern Matching. The reason I am interested in this is because we have been working on the Pattern and Matcher classes in Java in my CS242 course. Pattern matching in Haskell looks different than most things I have seen before but it also very powerful and intuitive once you get started with it.

Here is an example I have been working with. We can start by defining the name of a function here's an example:

numToString (Integral x) => x -> String

This line defines a function named "numToString" and then defines an integer called x "Integral x" then we use the => characters to state the pattern that we will be using. The next section of the line "x -> String" defines that we will be looking for an integer x and then returning a corresponding String value. For example:

numToString 150 = "One Hundred and Fifty"

Then we can run this function in our compiler:

ghci>numToString 150
and this will return:
"One Hundred and Fifty"

However if we try to run this function on an integer that is not defined we will get an Non-exhuastive pattern exception stating that not all integers are defined on this pattern:

demo: main.hs:4:1-41: Non-exhaustive patterns in function numToString

Patterns can also be applied to lists and Tuples which is a very powerful feature that I am interested in exploring!

Stay tuned for more updates on my progress with Haskell!