Abstract Confusions

Complexity is not a cause of confusion. It is a result of it.

Haskell – A Functional Programming Language

Functions in mathematics are like building blocks in many fields. Functions are also widely used in physics and other branches of engineering. In computer science, functions play a major role.  When you have a mathematical function and want to write a program for that, most of the time you do it line by line, with the program constructs like looping, conditional looping and other control structures. This could run to several lines of code.

Functional Programming

Unlike the programming languages like Pascal, C, C++ and all other higher programming languages thereof, are known as imperative programming or structured programming languages. Imperative program is executed using statements described steps after steps. Functional programming is programming executed by evaluating expressions (or functions).  There are no variable assignments, on the higher side, functional program contains no side effect at all.Why Functional Programming

In a 1984 paper – “Why functional programming matters” by John Hughes, listed down the following points:

  • Software becomes increasingly complex to handle with imperative programming style.
  • Software should be easy to write, easy to debug, easy to maintain, and should provide modules that can be reused.
  • Functional languages can push these limits.

But is it possible to just write functions as they look like into computers? Or simply put, is it possible for computers to understand functions in their native form – mathematical form?

Haskell

For example, assume the following set:

S = { (a, b, c) | a <11, b <11, c  <11,  a^2 + b^2 = c^2}

Now, if you want to find all elements of S, you would write a small little program to print out them, like below.

>>> def getelem():
   for a in range(1,11):
      for b in range(1,11):
          for c in range(1,11):
              if (a*a + b*b == c*c):
                  print a, b, c
>>> print getelem()
3 4 5
4 3 5
6 8 10
8 6 10
None

This is the way you tell the computer how the set elements need to be calculated, step by step, condition after condition.

Haskell is an advanced functional programming language. Now, lets see how this is done in Haskell.

ghci> let S = [(a,b,c) | a<-[1..10], b <-[1..10], c<-[1..10], a*a+b*b==c*c ]
ghci> S
[(3,4,5),(4,3,5),(6,8,10),(8,6,10)]

Haskell can understand functional notations. Isn’t that amazing? Just the set definition and you can see the compiler spitting out the elements for you.

Power of Haskell

Power of Haskell lies in the fact that it can understand mathematical expressions, no side effects, and finally what they call as lazy programming (this is a killer feature, will discuss this in a separate post).

Now, to demonstrate the power of Haskell, consider the quick sort technique. It is the efficient sorting algorithm, easy to define and understand. But when it comes to implement, it becomes kind of complex.

The algorithm is…

  1. Pick an element, use this as pivot.
  2. Reorder the list with values lesser than the pivot element before and values greater than pivot element after. The position of the pivot element is fixed and left with two sub-lists now.
  3. Now, repeat the same step for the sub lists.

What if Haskell can understand the quick sort algorithm?

<a class="zem_slink" title="Qsort" href="http://en.wikipedia.org/wiki/Qsort" rel="wikipedia">qsort</a> []     = []
qsort (x:xs) = qsort (filter (= x) xs)
*Main> qsort [12, 34 , 34,3 ,23 , 45, 23, 4,23, 34,34]

[3,4,12,23,23,23,34,34,34,34,45]

*Main> qsort "the quick brown fox jumps over the lazy dog"

"        abcdeeefghhijklmnoooopqrrsttuuvwxyz"

The meaning of the code:
qsort [] = [] : there is no need to sort empty list.
qsort(x:xs) = qsort(filter (=x)xs): (x:xs) –  is start with the first element in the element, call it as ‘x’ and remaining list as ‘xs’. filter(implementation in C++ or Java would run to several lines and could be erroneous.

Look at the fact that qsort works for the characters too.

Finally…

Now, this all being said, I should tell you the fact that, Haskell is still not exploited monetarily. There are no big systems built on Haskell. You should write programs on C, C++ if you need the system performance to be fast. Programmer should also need to have a fresh approach to Haskell.

But the advantages of Haskell weigh over all its demerits.

You can try Haskell online.

4 responses to “Haskell – A Functional Programming Language

  1. Pingback: Tweets that mention Haskell – A Functional Programming Language « Abstract Confusions -- Topsy.com

  2. Gumb June 10, 2010 at 7:44 PM

    P thanks to your ideas , i’d adore to adhere to your weblog as usually as i can.possess a good day

  3. Pingback: Abstract Confusions: Year 2010 in Review « Abstract Confusions

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: