Haskell Higher Order Functions
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.