Hello World Haskell
2009/10/27 § Leave a comment
After hearing so many good things about haskell finally i got chance to learn it, for the first time it looked different then the OOP syntax but it looks really cool. As most of the things are totally new, getting used to its environment and compiler may be little confusing so here i will try to demonstrate simple ‘Hello World!’ program on Haskell
First you need to get Haskell compiler, there are many implementations of the Haskell so you can download any one you prefer but as GHC (the Glasgow Haskell Compiler) is claimed to be one of the stable and widely used one so i am using GHC for this. You can get latest version of GHC from this page, choose the appropriate one and install on your system, if you are using windows you should see the GHC>GHCi on your ‘All Programs’ and clicking on this should open the console like this
Now we are done with setting up the compiler, lets go to the simple program. I want to demonstrate a simple conditional statement here. Say we have a shop which sells potatoes 2$ per kg and it gives 10% discount on total purchase if you have bought the potatoes worth more than 50$, otherwise no discount . we can represent this simple program in Haskell as follows
potatoRate = 2 price :: Double -> Double price potatoKg | (potatoKg*potatoRate) <50 = potatoKg*potatoRate | otherwise = potatoKg*potatoRate - ((potatoKg*potatoRate)*10)/100
so thats it, this simple 3 lines gives you all you needed, now save this program to some location say in 'c:\haskell' and i named this file as 'hello.hs'
now we need locate this file from GHci console and Load the function, to do so in the console you open last time type ":cd c:\haskell" (without parenthesis) in your console then type ":l hello.hs" lo load the program we saved in hello.hs, if there is no error in your code it should show some thing like this
*main> :l hello.hs
[1 of 1] Compiling Main <hello.hs, interpreted >
Ok, modules loaded: Main.
now every thing in done lets check how our program runs, we want to check what will be the price for 20 kg of potatoes
*main> price 20
it should display 40.0
*main> price 30
it should display 54.0
yeah, we have run our simple program in haskell.
[ you can get many Haskell video tutorial at this page