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!



No comments:

Post a Comment