2009/12/18 § Leave a comment
Haskell has been quite interesting to learn. Many different and new concepts compared to the imperative languages to name few, lazy evaluation, Higher order functions, lambda expressions, clean and minimal syntax and whole different style of programming. Here i am to talk few things about the higher order functions. Haskell functions are about to take other functions as input or return function as output, so these functions are called higher order functions. Higher order functions and one of the important features of functional programming which distinguishes this from other functions and also it is claimed that it is heart and soul of the Functional programming. Here i am going to illustrate with a simple example, given a string it counts the repetition of particular world on that string, the few lines of code below does it and notice now functions are glued together.
wordsCount :: String -> String wordsCount = unlines .map (\x -> head x ++ ": " ++ show (length x)) .groupBy (==) .sort .words outPut :: String -> IO () outPut = putStr.wordsCount
this is just one example showing the application of higher order function we can see many standard prelude functions like foldr, foldl, map which are so much reusable because of the higher order functions.
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
2009/10/26 § Leave a comment
wow, it has become almost 3 months i have setup the worldpress and i have already forgotten the path to the admin login page so after googleing and searching for my admin password in my password saver page finally i have been able to made this post, so now on i will at least try to post one blog a week let see how it goes..