Berkeley CSUA MOTD:Entry 53941
Berkeley CSUA MOTD
 
WIKI | FAQ | Tech FAQ
http://csua.com/feed/
2025/05/25 [General] UID:1000 Activity:popular
5/25    

2010/8/29-9/30 [Computer/SW/Languages/C_Cplusplus, Computer/SW/Languages/Java] UID:53941 Activity:nil
8/29    ok i give up; why is this throwing an error?
        #define Lambda(args,ret_type,body) \
        class MakeName(__Lambda___) { \
        public: ret_type operator() args { body; } }
        usage:
        Lambda((int a, int b), int, return a+b) foo;
        >g++ -o foo foo.cpp
        foo.cpp: In function ‘int main(int, char**)’:
        foo.cpp:9: error: expected primary-expression before ‘class’
        foo.cpp:9: error: expected `;' before ‘class’
        foo.cpp:11: error: expected `}' at end of input
        ref: http://okmij.org/ftp/cpp-digest/Lambda-CPP-more.html#Ex1
        \_ works for me.  see /tmp/lambda.cpp
           \_ nod thanks,  i didnt have MakeName defined there. sorry.
2025/05/25 [General] UID:1000 Activity:popular
5/25    

You may also be interested in these entries...
2011/3/7-4/20 [Computer/SW/Languages/C_Cplusplus] UID:54056 Activity:nil
3/7     I have a C question.  I have the following source code in two identical
        files t.c and t.cpp:
                #include <stdlib.h>
                int main(int argc, char *argv[]) {
                  const char * const * p1;
                  const char * * p2;
	...
2004/12/14-15 [Computer/SW/Compilers] UID:35291 Activity:moderate
12/14   If I have a C function like this
        void foo(void) {
          static const char unused1[] = "one";
          const char * const unused2 = "two";
          ...... some code ......
          return;
	...
2004/8/10-11 [Computer/SW/Languages/C_Cplusplus, Computer/SW/Compilers] UID:32805 Activity:high
8/10    C question.  Is there anything wrong with the following?
        const my_struct_t **pp;
        pp = malloc(sizeof(my_struct_t *));
        pp = realloc(pp, sizeof (my_struct_t *) * 2);
        "gcc -Wall" doesn't complain.  But the M$ compiler (cl.exe) complains
        about the realloc line:
	...
2004/5/28-29 [Computer/SW/Compilers] UID:30481 Activity:nil
5/28    I just found out that bison inserts these lines of code
                #ifndef __cplusplus
                #ifndef __STDC__
                #define const
                #endif
                #endif
	...
2003/12/8-9 [Computer/SW/Languages/C_Cplusplus] UID:11356 Activity:nil
12/8    c++ question, how do I overload << in my class so it will handle
        endl? ie: myclass << "some string" << endl
        I know how to do the "some string" part:
                myclass & operator << (const char * s);
        what about endl?
        Thanks!!
	...
2003/9/10 [Computer/SW/Languages/C_Cplusplus] UID:29529 Activity:nil
9/10    Stupid question: how do I reference a C callback?
        int mycompare(void const *a, void const *b) { ... }
        qsort(msg, sizeof(Msg), numMsgs, mycompare);
        I get
        "Type error in argument 4 to `qsort'; calling convention mismatch."
        \_ There's nothing wrong in the you reference it.  However, you should
	...
2003/4/22-23 [Computer/SW/Languages/C_Cplusplus] UID:28189 Activity:insanely high
4/22    Anyone know a good link that explains all of C++'s use of the
        keyword mconst?
        \_ http://www.parashift.com/c++-faq-lite
           Search for const in the text box.
           Search for mconst in the text box.
           \- perfection
	...
2002/7/13-15 [Computer/SW/Languages/C_Cplusplus] UID:25351 Activity:moderate
7/13    how do i pass variables to the system in C ?  e.g.
         system("echo input is %s", argv[1]);
        results in "input is %s" but I want the system to see argv[1]
        (I know i can just use printf, but not for what i really want to do).
        \_ sprintf into array, pass array to system?
        \_ so write your own function that takes a variable number of
	...
2001/9/23-24 [Computer/SW/Languages/C_Cplusplus] UID:22600 Activity:moderate
9/23    let's say there's a C library you want to use called libmdn.so.
        I think to load it you'd go something like:
        static { System.loadLibrary("mdn"); }
        However, let's say that a function call looked like this:
        mdn_result_t mdn_encodename(int actions, const char
        *from, char *to, size_t tolen)
	...
2001/3/17-18 [Computer/SW/Languages/C_Cplusplus] UID:20827 Activity:high
3/16    Why does so much C sample code use #define instead of const?
        \_ because any good C code will use a bunch of preprocessor
           anyways. you can't be a good C programmer and eschew the
           preprocessor. For that, you need a language which fills those
           gaps with other constructs (c++ templates go a long way to
           obviate the need for preprocessor for example). you
	...
2000/6/21-22 [Computer/SW/Languages/C_Cplusplus] UID:18506 Activity:very high
6/20    I have a variable inside a struct.  I want to be able to initialize
        that variable ONCE and not write to it again.  Any subsequent writes
        should not be permitted.  Is there a way to do that in C?  I know
        about "const int foo = 5;" but the value I need to pass in is dynamic
        and happens at runtime.  Declaring a variable as const doesn't let
        me assign anything to it at all.  Thanks.
	...
2013/5/1-18 [Computer/SW/Languages/Java, Computer/Theory] UID:54669 Activity:nil
5/1     What's the difference between CS and Computer Engineering?
        http://holykaw.alltop.com/top-ten-paying-degrees-for-college-graduates
        \_ One is science and the other is engineering.
        \_ From http://en.wikiquote.org/wiki/Computer_science
           'A folkloric quotation ... states that "computer science is no more
           about computers than astronomy is about telescopes."  The design
	...
2012/12/4-18 [Computer/SW/Languages/Java] UID:54544 Activity:nil
12/4    Holy cow, everyone around me in Silicon Valley is way beyond
        middle class according to Chinni's definition:
        http://en.wikipedia.org/wiki/American_middle_class
        \_ Let's set our goals higher:
           http://en.wikipedia.org/wiki/Upper_middle_class_in_the_United_States
           \_ How about this one?
	...
2012/10/29-12/4 [Science/Disaster, Computer/SW/Languages/Java, Politics/Domestic/President/Bush] UID:54516 Activity:nil
10/29   Go Away Sandy.
        \_ Sorry, Coursera is performing preventive maintenance for this
           class site ahead of Hurricane Sandy. Please check back in 15 minutes.
           class site ahead of Hurricane Sandy. Please check back in 15
           minutes.
        \_ Bitch.
	...
2012/1/18-3/3 [Computer/SW/Languages/Java, Finance/Investment] UID:54290 Activity:nil
1/18    I own a bunch of NFLX stocks bought at several different periods
        (from high $200 all the way down to $80). I dumped a few and
        still have a few. Why the hell is Reid Hastings still making
        $500,000/year? How do I join the pending NFLX Class Action
        Lawsuit?
        \_ Why would you buy stock in a company run by a narcissistic
	...
Cache (4507 bytes)
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.