Haskell – Functional Programming Language
I had the luxury of learning Haskell today.
Seems that all this while I’ve been using imperative programming language. If you know Ruby, Java, C++, C#, C Fortran or Paskal, all those belongs to the imperative group. That being said, the easiest explanation for imperative programming is that it uses state changes as well as arguments to facilitate module processing.
Erm.. something like passing values as arguments or doing state changes like n=2.
Haskell on the other hand, is an entire different kettle of fish. Being a FPL, it’s build up entirely of functions. Those familiar with C / C++ should know what they are.
In fact in Haskell, the variables are functions, the arguments are functions, the functions call another functions or even themselves (in a recursive sort of way).
That means that Haskell programs are essentially very short and simple to code and understand. It also means that a Haskell program consists of one or 2 functions, and within them several smaller functions and so on.
For example there might be a main() function. Then within it there are 3 more functions called welcome(), work() and end().
Welcome() accepts 2 functions (as arguments) mainly male() or female().
Work() would accept 2 more functions such as email(), makeCall().
End() would just end the program.
If you wanted a visual representation of that, it’s:
main(welcome(male(), female()), work(email(), makeCall()), end())
Confusing? Hahaha.. good. Cause I was too thinking it would be too hard to maintain codes like these. But then after several classes of Haskell things starts to make sense to me. And after that, complex problems became easier to solve compared to using C.
Sample of a factorial program in Haskell:
fac :: Int -> Int
| n == 0 = 1
| n > 0 = fac(n-1)*n
Finished. Above is the complete code. To use the function, just type in..
..and it will return 120. Now I dare you to write a factorial function using recursive in C within 3 lines!
Notice that there are no need for semi-colons, no need to import any working enviroment and such things. The compiler will even perform garbage collection just like Java. No frills programming.
To explain the code:
fac :: Int -> Int //this is the function prototype. It tells us that the function name is “fac” and it takes and integer and returns an integer.
| n == 0 = 1 //this means that if n is equals to 0, return value of 1
| n > 0 = fac(n-1)*n //this means that if n is larger than 0, then returns fac(n-1)*n which will then loop back and so on until n-1=0 which the recursive loop will terminate.
It’s really simple.
Another simple example of finding the largest of 2 user input values.
max :: Int -> Int -> Int
max x y
| x >= y = x
| otherwise = y //otherwise is a constant in Prelude.hs not a pseudocode by me ;P
Do you understand how it works now?
Some friends of mine still do not understand how the code above works. Well..
max :: Int > Int -> Int
..means the function called “max” will accept 2 different “Int” arguments and returns an “Int” result.
max x y
..means the 2 arguments will be assigned a temp variable of x and y.
| x >= y = x
| otherwise = y
..means if variable x is larger or equals to y, function max will return x. Otherwise, return y. The last 3 lines of code can be written in a single line like..
max x y |x >=y =x | otherwise =y;
Some other friends asks how to start learning, coding in Haskell.
Here’s some steps.
#1 Install WinHugs from Haskell.org’s download page . WinHugs is an interpreter. Meaning it process codes line by line. Like HTML.
#2 Once installation is done, you just start writing codes in a notepad (if whole block of functions) and name it <whatever>.hs. Double click the *.hs file to load into Winhugs and you can use the function you just made. Likewise if you fancy coding directly into the interpreter, by all means start up WinHugs and code away!
#3 Visit the Haskell.org website more often. It’s a valuable source of infomation and coding techniques.