Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are some interesting uses of higher-order functions?

I'm currently doing a Functional Programming course and I'm quite amused by the concept of higher-order functions and functions as first class citizens. However, I can't yet think of many practically useful, conceptually amazing, or just plain interesting higher-order functions. (Besides the typical and rather dull map, filter, etc functions).

Do you know examples of such interesting functions?

Maybe functions that return functions, functions that return lists of functions (?), etc.

I'd appreciate examples in Haskell, which is the language I'm currently learning :)

like image 393
Manuel Araoz Avatar asked Apr 26 '11 15:04

Manuel Araoz


People also ask

What are higher-order functions What are they useful for?

In a nutshell, a Higher-order function is a function that may receive a function as an argument and can even return a function. Higher-order functions are just like regular functions with an added ability of receiving and returning other functions are arguments and output.

What is a higher-order function give any two examples?

Note: Functions such as filter(),map(),reduce(),some() etc, these all are example of Higher-Order Functions.

Why are higher-order functions useful Python?

Decorators. Decorators are the most common use of higher-order functions in Python. It allows programmers to modify the behavior of function or class. Decorators allow us to wrap another function in order to extend the behavior of wrapped function, without permanently modifying it.

What is the use of order function?

order returns a permutation which rearranges its first argument into ascending or descending order, breaking ties by further arguments. sort. list is the same, using only one argument.


1 Answers

Well, you notice that Haskell has no syntax for loops? No while or do or for. Because these are all just higher-order functions:

 map :: (a -> b) -> [a] -> [b]   foldr :: (a -> b -> b) -> b -> [a] -> b   filter :: (a -> Bool) -> [a] -> [a]   unfoldr :: (b -> Maybe (a, b)) -> b -> [a]   iterate :: (a -> a) -> a -> [a] 

Higher-order functions replace the need for baked in syntax in the language for control structures, meaning pretty much every Haskell program uses these functions -- making them quite useful!

They are the first step towards good abstraction because we can now plug custom behavior into a general purpose skeleton function.

In particular, monads are only possible because we can chain together, and manipulate functions, to create programs.

The fact is, life is pretty boring when it is first-order. Programming only gets interesting once you have higher-order.

like image 57
Don Stewart Avatar answered Oct 06 '22 16:10

Don Stewart