Tuesday, November 18, 2014

What's Haskell?

I am currently enrolled in a computer science course which requires the learning of a new programming language--one that I have never heard of--Haskell.
Haskell is a functional programming language. This means that it does not take the popular object-oriented approach to programming. In order to properly describe the language, I'll be using a few examples.
{--Haskell--}
whichIsGreater :: Integer -> Integer -> Integer
whichIsGreater x y = | x > y = x
                     | y >= x = y
/*Java*/
public int whichIsGreater(int x, int y){
     if(x > y)
          return x
     return y
}

Looking at the code here, we can see that Haskell does look neater. I've used Haskell whenever I wanted to find a simple calculations. When it comes to doing more complicated things, it takes a greater amount of thinking compared to the linear thought process provided by most programming languages.
{--Haskell--}
rectanglePos :: Integer -> Integer -> (Float, Float) -- creates a list of points for rectangles in a grid
rectanglePos width height = [(x,y) | x <- createRectx(width), y <- createRecty(height)]

createRectx :: Float -> [Float]
createRectx n = [(x * 40) | x <- [0 .. n-1]]

createRecty :: Float -> [Float]
createRecty n = [(y * 18) | y <- [0 .. n-1]]
/*Java*/
public Point[] rectanglePos(int width, int height){
     Point[] re = new Point[width*height];
     int counter = 0;
     for(int x = 0; x < width; x++){
          for(int y = 0; y <; height;y++){
               re[counter] = new Point(x*40,y*18);
               counter++;
          }
     }
     return re;
}
This is a part of a program that I was using while testing out Haskell's Gloss library. Let me say that compared to the java version...it's much cleaner. But more difficult to think about.

I started out this blog post with a great distaste for Haskell in my mouth, and didn't completely think about the reason I didn't like it. It's not the fact that it's slow, or that it's not a portable language (it's quite fast and portable). It's due to the fact that it was a completely different way of writing a program that was unlike anything I had ever seen before. This wasn't like scripting in python or ruby; and it certainly wasn't like writing the many lines of code needed for C++ or Java.

Haskell is a programming language that is capable of achieving the same things as these programming languages, but just with a different approach. With foreign syntax and the tidiness that pretentious programmers loath, Haskell isn't all that bad. In fact, I look forward to waking in a sweat at night yelling "PRELUUUUUDE!" (No, seriously, I do).

No comments:

Post a Comment