okmij.org/ftp/cpp-digest/Lambda-CPP-more.html#Ex1
Scheme This article is to exhibit lambda abstractions in C++ in comparison with those of traditional functional languages (eg, Scheme). The article will try to demonstrate that "applicable values" in C++ not only look similar to their functional cousins. This article will not prove that C++ and Scheme are identical, as they are clearly not. Yet the extent and depth of similarity in lambda expressions is uncanny. I humbly submit that the functional side of C++ is not a well-publicized feature of that language.
Article headers 1 Simple lambda-expressions In Scheme: (define foo (lambda (a b) (+ a b))) The lambda-expression when evaluated produces the value of a procedural type. Evaluation of an expression (foo 1 2) looks up the procedural value bound to this identifier, and applies it. In C++, precisely the same idea is expressed as Lambda((int a, int b), int, return a+b) foo; where #define Lambda(args,ret_type,body) \ class MakeName(__Lambda___) { \ public: ret_type operator() args { body; The instantiation operator -- called 'declaration' in plain C -- "binds" this apply-able type to an instance foo. This sounds better in reverse: foo is bound to a value of a procedural type. Or, foo is instantiated as an object of a class that implements a callable interface. When the compiler processes an application: cout << "foo(1,2) is " << foo(1,2) << endl; it looks up the type of foo, finds the procedural class and invokes the appropriate method. The similarity between the two expressions -- in Scheme and C++ -- is almost complete. n)) 1 (* n (fact (- n 1)))))) Note when the lambda expression itself is being evaluated -- to a procedural value -- an identifier fact it mentions is not bound to anything. The binding occurs afterwards, after the resulting procedural value is used by define. Yet this does not pose any problem: the lambda expression is not being applied yet at this point, it is merely "compiled". When the value of fact is indeed required, it will certainly have been bound. The above expression takes advantage of the fact that the "current object" can be referred by this within the body of a method. It explores the same kind of a 'lookup delay' as the Scheme expression above. When the body of a method is being compiled, there is no "current object" yet: this is only potentially bound. Later on, when the compiler sees an application of the method, it knows to which object the method is being applied to. It makes use of a cache to speed up computation of the n-th Fibonacci number. fib-n-1 v-1))) v))) fib)) 1 2 5 13 34 89 This prints every other Fibonacci number. The caching of previous results really speeds up the computation. Here's the equivalent C++ code: template<class T> struct pair { T fst, snd; return do_cache(xv(x-1,vp), xv(x,vp+(*this)(x-2))) ) fib; template<class T> void print_every_other(const int n) { T closure; What is passed to Scheme's print-every-other is a procedural value, which becomes bound to proc inside that second-order function. What is passed to C++ print_every_other is a procedural type, which becomes instantiated inside that second-order procedure, and can then be applied. public: Curry_add(const T _a) : a(_a) {} Curry_add<T> operator () (const T b) const { return a + b; It makes the calls to the constructor CurryAdd<T> explicit: template<typename T> class CurryAdd { T a; When the eventual result is finally fed to the operator <<, the latter causes the conversion method CurryAdd<int>::int(void) to be applied, yielding the integer to print. p) x (curry-add (+ x (car p)))))) ((curry-add 5)) ==> 5 (((curry-add 5) 7)) ==> 12 ((((curry-add 5) 7) -1)) ==> 11 The type of this function can be informally expressed as type Curry_add = Curry_add -> Message_app -> (Int -> Object) \/ Curry_add -> Message_conv -> Int where the union \/ models overloading. This is an equi-recursive (aka, infinite'') type, as typical of OO systems.
Functions with the variable number of (variously typed) arguments in Haskell. In his example, the comma operator indeed acts as an infix apply. int main( void ) { (display, ((((max , 20), 30), 40), 50) ); The comparable Scheme code will be (define (foldf fn) (define (ffn arg1) (lambda opt-arg (if (null?
Functional Style in C++: Closures, Late Binding, and Lambda Abstractions A poster presented at the 1998 International Conference on Functional Programming (ICFP'98) Acknowledgment Andy Gaynor has asked the right question. com Sun Jan 24 15:17:31 1999 Subject: Lambda abstractions in C++ vs.
|