Friday, March 14, 2014

Haskell week 6


This week my main focus was on exploring some of the more useful libraries in Haskell I was amazed at how many cool features there are in some of these libraries. I explored a couple of different libraries so this post is going to be somewhat varied topic wise, but I think it is worth knowing some of these odds and ends of Haskell.

 Data.List

I researched some Haskell documentation and decided to test a couple of the functions that fall under the Data.List library.

Let's take a look at a little section of code I wrote:


The first thing that we have to do is import the Data.List library (this is done in a similar way to java). Once we have the library imported we can begin to use the methods it contains. The next line of code defines the main function of the program and the "do" command lets us erm, well... it let's us do things (like printing). The first method is the concat method. It takes the given strings and concatenate them together in the order they were given. Next we use the intersperse method which takes two parameters a character and then a string. The first character is placed between each character in the second parameter. Next we use the sort feature (pay special attention to this!). Lastly, we use the sub-sequences method which takes a string and returns a list of the possible substrings.

Let's take a look at the outputs:


Are these the results we would expect? I think so!

 System.CPUTime

The CPUTime library has two methods that are associated with it. The first is getCPUTime which returns the number of  picoseconds of CPU time that are currently being used by the program. I think this is a very interesting feature of Haskell. The second method is the cpuTimePrecision which is the smallest measurable difference in CPU time that the implementation can record. 


Let's take a look at the implementation:


And now the results:


Very cool!

Data.Char
The next Library that I looked at was Data.Char this library has some pretty standard methods but they still can be very useful. It has the standard methods such as isAlpha which returns a Boolean, isDigit, also a Boolean and some other built in ones such as digitToInt and intToDigit.

Let's take a look:


And the results:


Although this might seem somewhat trivial it will become important when we move on to other concepts.

Data.Tree

I decided to take a look at the Data.Tree library because in many of my CS classes I keep hearing about Trees so I wanted to do a little bit of research and make a couple of trees in Haskell.


and here you can see a visual:


Just a surface level example of trees.

That is it for now. Stay tuned for my progress with Haskell.

No comments:

Post a Comment