Higher-order functionIn mathematics and computer science, higher-order functions are functions which can take other functions as arguments, and may also return functions as results. The derivative in calculus is a common example of a higher-order function, since it maps a function to another function. Higher-order functions were studied long before the notion of functional programming existed, in the lambda calculus, a formalism which has influenced the design of several functional programming languages, especially the Haskell programming language. Higher order functions in Haskell implement tremendous power in very few lines of code. For example, the following Haskell functions will square each number in a given list -- without higher order functions squareListNoHof = squareListNoHof list = ((head list)^2):(squareListNoHof (tail list)) -- with higher order functions squareList list = map (^2) list In the above example, The above was an example of a higher-order function that takes in a function as an argument, but does not return a function of sorts as an output. However there are standard higher order functions that do, such as the -- without higher order functions doFunctionNoHof x = cos (log (sqrt (3x+2)) -- with higher order functions doFunction x = (cos.log.sqrt) (3x+2) In the above example, the Alternatives Programming languages which can dynamically execute code (sometimes called "Eval" or "Execute" operations) in the scope of evaluation and have functions which can optionally inherit the caller's variable scope can be used to do much of what higher-order functions do in programming languages. However, because most of it happens at run-time instead of compile time, these approaches will usually execute slower. Some also consider dynamic evaluation a security risk. But, some feel that they keep the language simpler and/or easier-to-learn. See also: functional analysis, combinatory logic |
|
This article is licensed under the GNU Free Documentation License. It uses material from Wikipedia article. Browse Wikipedia for more information. |