Tuesday, May 22, 2012

My 1st Haskell Program

From past few days I was thinking to do something in Haskell. Gone though few blogs and also tried my hands on tryhaskell only Haskell interactive tutorial. It was way different then my bread & butter Java. So wasn't able to understand much. And now was feeling to write a factorial program in Haskell. My first experiment with Haskell. 


Downloaded Haskell from here
Now Install Haskell similar the way you installed millions of software.
Haskell itself update %PATH% variable so you are not ready to rock and roll in Haskell.


Haskell comes with ghci, ghc and runhaskell utilities to interpret, compile or run haskell programs.



  • ghci provide haskell prompt where you can execute haskell statements on the fly.
  • ghc is haskell compiler which compile haskell program and produce an exe on successful compilation.
  • runhaskell utility run haskell program on the fly.
Below is mine 1st real haskell program



  1. Line 1 Prototype of factorial function.
  2. Line 2 Body of factorial program, used recursion
  3. Line 4 Here we need to execute multiple statements in main so we are using do syntax here
  4. Line 5 Give a message to user
  5. Line 6 It takes value from IO and store it in numS variable and you cant use = instead of <- As using = will give IO operation another name rather then reading value from IO
  6. Line 7 read function convert String to Integer and then give the result to factorial function and factorial function computes the result and print function display the result.

This is my 1st real haskell program, I am not fully aware of haskell or haskell terminology so there may be many mistakes.


Code
1
2
3
4
5
6
7
factorial :: Integer -> Integer
factorial n = if n > 0 then n * factorial (n-1) else 1

main = do
    print "Enter a Number" 
    numS <- getLine
    print (factorial (read numS))


After detailed review of above program by fellow reddit haskellers I come to know that there is lots of improvement needed in this program and that also helped me to dig deeper in haskell.

Instead of IF ELSE + Recursion now factorial method uses Pattern Matching. 
Pattern Matching is similar to Switch Statements of High Level Languages. When you call a method haskell run time scan the program from top to down order and go to the 1st match same to switch statement.
And if you reverse the order of factorial function declaration like below compiler will always go to 1st statement.


factorial n = n * factorial (n-1)

factorial 0 = 1


Print function is used for displaying non-string type as String on I/O so using print to display "Enter a Number" message results in extra double quotes in message so I changed it to putStrLn as suggested by dave4420 on reddit.


And instead of using parentheses I changed last line to haskell way which is much clear and haskelly. 


Updated Code


1
2
3
4
5
6
7
8
factorial :: Integer -> Integer
factorial 0 = 1
factorial n = n * factorial (n-1)

main = do
	   putStrLn "Enter a Number"	
	   numS <- getLine
	   print $ factorial $ read numS

4 comments:

  1. How do you get code syntax highlighting for the dynamic view blog template?

    ReplyDelete
    Replies
    1. Used http://hilite.me/ to generate html.

      Delete
  2. You might want to check out these versions of the factorial function: http://www.willamette.edu/~fruehr/haskell/evolution.html .

    ReplyDelete
    Replies
    1. Thanks for above link. Great link.

      Delete