-={ The world through a looking glass }=-

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

fac n

| n == 0 = 1

| n > 0 = fac(n-1)*n

Finished. Above is the complete code. To use the function, just type in..

fac 5

..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?

[UPDATE #1]

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;

[Update #2]

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.

About these ads

7 Responses

  1. Kirby54925

    You know, you could write fac in one line:

    fac n = product [1..n]

    Why make yourself work more than you need?

    25 August, 2008 at 3:14 AM

  2. Please join for discussions on topics about The Virtual Reality.
    Technology development of The Virtual Reality and its perspectives.
    Gnolet.com

    14 April, 2007 at 5:09 AM

  3. ChristianS

    No need for #includes (and I think it wouldn’t be very fair to compare such lines), and

    main(){ printf(“%d”,fac(5)); }

    (okay, one would usually use three lines for that) is not so much worse than

    main = print $ fac 5

    To impress C fans I’d really use something else, hamming numbers for example, or just

    factorials = scanl (*) 1 [1..]

    20 March, 2007 at 8:55 AM

  4. Corrected the code. Thanks for the function.

    But in order to execute it, you’ll still have to run this in a C environment with all the main header files and such.

    20 March, 2007 at 6:57 AM

  5. ChristianS

    Your factorial codes needs a line two with “fac n”.

    While I prefer Haskell, there is no problem to define a recursive factorial function in C:

    int fac(int n){
    return n==0 ? 1 : n*fac(n-1);
    }

    Three lines!

    20 March, 2007 at 2:10 AM

  6. Thanks for the correction! :)

    28 February, 2007 at 6:34 AM

  7. otherwise isn’t a keyword, its just a constant in Prelude.hs:

    otherwise :: Bool
    otherwise = True

    28 February, 2007 at 4:56 AM

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 )

Connecting to %s

Follow

Get every new post delivered to your Inbox.