aka '''HOF'''. A function that takes a function as an argument, or returns a function as a result; this latter use is just as important as functions-which-take-functional-arguments such as "foreach" or "filter" or "reduce". The latter requires insightful cleverness in languages such as CeePlusPlus [c.f., BoostBind which relies on using functional programming concepts as a by-product of C++'s templates]. [See also FunctoidsInCpp.] It is just a LayerOfIndirection for function references. * You may be thinking of LexicalClosure''''''s, top, but never mind. Function pointers are ''not'' an adequate substitute, otherwise CeeLanguage would be a FunctionalProgrammingLanguage. ''I've always thought that '''qsort''' and the like (in C/C++) which uses pointers to functions as arguments were higher order functions.'' '''Nope. See the next line as a better example of HOFs.''' * How, in C++, can one define a function '''f''' that takes an integer '''n''' and returns a function '''g''' that takes an integer '''x''' and returns '''x+n'''? Something like?: std::binder1st > f(int n) { return std::bind1st(std::plus(), n); } This is standard C++, though some people regard it as ugly. Not so much the use of 'std::', but the code itself, especially the syntactically significant white space between the nested >'s. That is why some people prefer languages with true first class functions. ''FunctoidsInCpp discusses FC++ which is a library implementing this and much more.'' -- JohnFletcher Some languages have built-in convenience mechanisms to do this. There are some examples in everyone's favorite languages below, as well as on the CommonHigherOrderFunctions page. It is rather simple to accomplish this in the PythonLanguage: def f(n): def g(x): return x + n return g BlocksInRuby lists some great usages of higher order functions. In Ruby, they're called "blocks", they are a special language construct, and you cannot pass more than one to any routine. ''Incorrect, blocks are anonymous functions, not higher order functions. The higher order functions would be the function to which you pass the block. The combination of anonymous functions and higher order functions together are where you get the power from, the anonymous functions essentially specialize the higher order function. Any language that supports passing functions as parameters can support higher order functions, but without anonymous functions, they won't get used too often, CsharpLanguage is such a beast. -- RamonLeon'' [Although not for much longer - version 2 of the language will feature anonymous functions (which are fully-fledged LexicalClosure''''''s), along with a standard set of HigherOrderFunction-style methods on the collection classes (usual map/filter/forall/iter/etc, but with different names (presumably they don't want to "confuse" people who haven't seen them before... at the expense of confusing those of us who ''have'' seen them before...)). See http://blogs.msdn.com/kcwalina/archive/2004/06/22/162533.aspx for some preliminary details of the collection methods. -- MikeRoome] ''Not suprising, it is standard Microsoft practice to rename and reimplement and then pretend they invented something new. They'll be nice to have though. -- rl'' No, it's standard Microsoft practice to take something, rename it, '''patent''' it and pretend they invented something new. ''No, it's everyone else's practice to say "look at all these languages with feature x - isn't it great that we all build on each other like that?" unless someone at Microsoft does exactly the same thing.'' (But you didn't grasp the anti-Microsoft complain. Patenting means to take control and to stop everyone else to "build on that" without consent, so the "great we build on each other" will become "look, they have built on Microsoft's", plus "let's see if we can sue them" when applicable... Microsoft does not want you to say "we all build on each other", but "we all build on Microsoft's great original ideas", though they have just build on the other. See the difference? Of course, it does not happen to be always like this, but the complain is about this MS general attitude, which tears MS away from a community behaviour where you, like everyone else, can say happily "great that we build on each other") ---- Sure. I program in CommonLisp. Using HigherOrderFunction''''''s is so common that you don't even notice it. The built-in ones are: mapcar (and friends), apply, funcall, sort (it takes the comparison function object as argument), count-if, remove-if, delete-if, and a ''whole'' heap of functions which take the :test optional key argument (e.g. find). The way you use these is in expressions like (dolist (func (find-all-if #'suitable-p *my-objs-containing-a-func)) (funcall (object-function-slot func))) Without dolist it looks much better. ;-) (mapc #'funcall (find-all-if #'suitable-p *my-objs-containing-a-func)) But, as I say, that's just the built-ins. The '''real''' power comes from using this naturally in your own algorithms. For example, the StrategyPattern totally disappears in such a language: you just pass the function to use instead of creating classes for the strategy. (As a side note, many, if not most, of the patterns in the patterns book are unnecessary or considerably simpler in languages which support functions as first class objects. See PeterNorvig's DesignPatternsInDynamicProgramming. -- AlainPicard (but feel free to RefactorMe) * This isn't much better than the CeePlusPlus version of a HigherOrderFunction. They're both ugly, and which is less ugly just depends on which you're more used to looking at. ---- Here are three simple examples of InternalIterator''''''s (using the MapFunction). PerlLanguage example: sub double { return 2 * shift; } @a = ( 1, 2, 3, 4, 5 ); @b = map { double( $_ ) } @a; ... or perl's "list-comprehension-ish" 'gather' clause use Perl6::Gather; @b = gather { take $_ * 2 } foreach 1..5; RubyLanguage example: def double ( num ) return 2 * num end a = [ 1, 2, 3, 4, 5 ] b = a.collect { |value| double( value ) } Alternative Ruby version(s): double = lambda { |num| 2 * num } b = a.map(&double) or even b = a.map { |num| 2 * num } SmalltalkLanguage example: (very similar to the RubyLanguage example) Number>>double ^ 2 * self a := #(1 2 3 4 5). b := a collect: [ :value | value double]. or (1 to: 5) collect: [ :i | 2 * i ] PythonLanguage example: ... with explicit function declaration def doubleit( num ): return 2*num a = ( 1, 2, 3, 4, 5 ) b = map( doubleit, a ) ... or with lambda b = map(lambda num: 2 * num, a) ... or with ListComprehension: b = [ 2 * num for num in a ] SchemeLanguage example: (define (double num) (* 2 num)) (define a '(1 2 3 4 5)) (define b (map double a)) or (define b (map (lambda (x) (* 2 x)) a)) CommonLisp example: (defun double (num) (* 2 num)) (defvar *a* (list 1 2 3 4 5)) (defvar *b* (mapcar #'double *a*)) or (defvar *b* (mapcar #'(lambda (x) (* 2 x)) *a*) ObjectiveCaml example: let b = let a = [1;2;3;4;5] in List.map (( * ) 2) a;; HaskellLanguage example: b = map (*2) [1..5] In all cases, array b equals [ 2, 4, 6, 8, 10 ]. ErlangLanguage example: ... with higher-order map and a LambdaExpression lists:map(fun(X) -> 2*X end, lists:seq(1,5)). ... or with a ListComprehension: [ 2*X || X <- lists:seq(1,5) ]. CeePlusPlus example: vector a, b; // set a values to whatever transform ( a.begin(), a.end(), // take this range back_inserter(b), // append to b bind1st(multiplies(), 2)); // after multiplying by 2. or with boost::lambda ''(BoostLambdaLibrary)'': transform (a.begin(), a.end(), back_inserter(b), _1 * 2); You can use a template function that uses one or more of its arguments as a FunctorObject in CeePlusPlus to make a Higher Order Function. GroovyLanguage example: (basically the same as RubyLanguage and SmalltalkLanguage) a = [1,2,3,4,5] b = a.collect { 2 * it} CsharpLanguage v2 example: int[] a = { 1, 2, 3, 4, 5 }; int[] b = Array.Convert''''''All(a, delegate(int x) { return 2 * x; }); or using the List collection class: List a = new List(new int[] { 1, 2, 3, 4, 5 }); List b = a.Convert''''''All(delegate(int x) { return 2 * x; }); ---- Also, JavaScript'''''''s treatment of functions as first class objects makes it very simple to write HigherOrderFunction''''''s, to use the above example we have to write map, but it's pretty simple: Array.prototype.map=function(aBlock){ var result=new Array(); for(var index=0;index