News for hackers. Stuff that matters.
Re:Lambdas in C by eps on Friday July 01, 2011 @10:27AM (#2718450)
In other words - he discovered gcc's ({ }) construct and decided to write a macros named lambda around it.

http://gcc.gnu.org/onlinedocs/gcc-2.95.3/gcc_4.html#SEC62

Re:Lambdas in C by wingo on Friday July 01, 2011 @10:47AM (#2718528)
To me it's a bit more surprising: it's a statement expression, containing a nested function, then we take the address of that nested function.

Having that series of operations produce something useful is unexpected, to say the least.

It's nothing I'd want to see in code that I maintain, but I think you have to give it a bit more credit. Besides that, it's cute :)

Re:Lambdas in C by tjr on Friday July 01, 2011 @10:50AM (#2718538)
Any idea if this is now officially supported in GCC? The blog post is from nearly a year ago.
Re:Lambdas in C by ori_b on Friday July 01, 2011 @11:02AM (#2718580)
It's been officially supported in GCC since about a decade ago, I believe. It's not part of the C language, so if you're writing this code, it's no longer C. It also requires executable stacks.
Re:Lambdas in C by xerampelin on Friday July 01, 2011 @12:47PM (#2719061)
The article indicates its not officially supported, although it could be.

     Ian Lance Taylor replied that the behavior is 
     undefined, but so far stable. His suggestion was 
     to open a bug and request that this desirable (!!!)
     behavior be explicitly permitted.
Re:Lambdas in C by ori_b on Friday July 01, 2011 @02:32PM (#2719570)
Oh. Right, because of the new scope that the anonymous block creates. My mistake.
Re:Lambdas in C by xilun0 on Friday July 01, 2011 @12:20PM (#2718932)
why does it requires executable stacks?
Re:Lambdas in C by fmap on Friday July 01, 2011 @01:45PM (#2719331)
Because of the way nested functions are implemented in GCC.

If you call a nested function it has to know the frame address of the containing function in order to refer to its local variables. So if you take the address of such a function the program actually receives the address of a "trampoline": a small sequence of instructions which first loads the frame address and then calls the real function.

The trampoline itself is generated dynamically and putting it on the stack turns out to be cheaper than heap allocating it.

Re:Lambdas in C by ori_b on Friday July 01, 2011 @02:14PM (#2719481)
> and putting it on the stack turns out to be cheaper than heap allocating it.

Well, more so that C has no concept of the heap. That's entirely contained within the C libraries. So, the language implementation itself can't do heap allocations (at least, not without sacrificing any chance of using it in embedded or kernel dev situations where you don't have heap allocation, or where you need to use custom allocators)

Re:Lambdas in C by swdunlop on Friday July 01, 2011 @08:10PM (#2720654)
If you're using closures in an embedded environment with no heap, you've got bigger problems than executable stacks to worry about..
Re:Lambdas in C by apgwoz on Friday July 01, 2011 @04:56PM (#2720125)
Right. And of course the whole idea is prone to insane amounts of fail.

    > Thus, if you access the enclosing environment, you
    > must ensure that the lifetime of this lambda is bound by the
    > lifetime of the enclosing environment (i.e., until the enclosing
    > function returns).  This means that if you access local
    > variables, bad things will happen.  If you don't access local
    > variables, you're fine.
Might as well avoid the complications, hoist the function and pass a function pointer with some context (assuming it's needed).
[deleted]
Re:Lambdas in C by ori_b on Friday July 01, 2011 @11:03AM (#2718584)
You're not passing a compound statement expression as a function pointer. You are passing the result of the evaluation of that expression --which is a function pointer -- as a function pointer.
Re:Lambdas in C by tjr on Friday July 01, 2011 @11:05AM (#2718593)
Oops, sorry, I just deleted my comment because I thought that wingo's comment covered my thoughts and more. For posterity, I had previously written:

I do not find it obvious from that documentation that you can pass a compound statement expression as a function pointer.

And touche', I just glanced at the contents of the macro before replying.

Re:Lambdas in C by jensnockert on Friday July 01, 2011 @10:18AM (#2718420)
Blocks, http://clang.llvm.org/docs/BlockLanguageSpec.txt

Much better solution to the problem, hopefully coming to standard GCC soon(tm).

Re:Lambdas in C by Rexxar on Saturday July 02, 2011 @08:56AM (#2721553)
They should try to make something syntactically compatible with C++ lambdas. It's annoying that the two languages are more and more incompatible for cosmetic reasons.
Re:Lambdas in C by brohee on Friday July 01, 2011 @10:11AM (#2718382)
Am I really out of my mind or is this not a lambda at all but just an anonymous function?
Re:Lambdas in C by StrawberryFrog on Friday July 01, 2011 @11:14AM (#2718638)
If an anonymous function can capture state from the enclosing code block, and outlive that code block, then it's a lambda.

So this probably isn't really a lambda.

Re:Lambdas in C by philwelch on Friday July 01, 2011 @04:56PM (#2720124)
That's the definition of a closure. I'm not sure if "lambda" is necessarily synonymous with "closure" or just synonymous with "anonymous function", but lambdas are closures in many languages.
Re:Lambdas in C by StrawberryFrog on Sunday July 03, 2011 @10:39AM (#2723755)
You're right, my terms are sloppy. Are there rigorous definitions, or does it vary between languages?.

I think of "lambda" as a language construct that creates closures. "anonymous functions" are of course just a simpler subset of closures.

Re:Lambdas in C by Kototama on Friday July 01, 2011 @09:59AM (#2718335)
Note: unlike lambdas in functional languages, this lambda does not capture the containing environment.
Re:Lambdas in C by ori_b on Friday July 01, 2011 @10:18AM (#2718415)
That's slightly sloppy phrasing. It captures it, but it does not duplicate it, so it's lifetime is tied to the stack frame of the function you declared it in.

In short, you can only do downwards funargs.

Re:Lambdas in C by Jach on Friday July 01, 2011 @10:00AM (#2718343)
Bah, beat me to it. It's still kind of cool though.

    qsort (array, sizeof (array) / sizeof (array[0]), sizeof (array[0]),
         lambda (int, (const void *a, const void *b),
                 {
                   dump ();
                   printf ("Comparison %d: %d and %d\n",
                           ++ comparison, *(const int *) a, *(const int *) b);
                   return *(const int *) a - *(const int *) b;
                 }));
Re:Lambdas in C by athom on Friday July 01, 2011 @06:00PM (#2720315)
My favorite part: l_anonymous_functions_name

Sorry, I just like the irony.

Re:Lambdas in C by snorkel on Friday July 01, 2011 @04:15PM (#2719985)
Cute, but functions pointers suit me just fine.
Re:Lambdas in C by huhtenberg on Friday July 01, 2011 @06:14PM (#2720353)
Anonymous functions are very handy, and I think they should really be taken a step further. There's really no reason why it shouldn't be possible to pass an actual function body where a function pointer is required:

  int v[...];

  qsort( v, ..., (a, b) { return *(int*)a - *(int*)b } );
Such usage would also have an added benefit of letting the compiler do the type inference for a, b and the return value and not needing to explicitly specify them in qsort() call.
Re:Lambdas in C by CamperBob on Saturday July 02, 2011 @12:52AM (#2721031)
Local functions would be a nice compromise between the unreadable glop that was posted and the need to declare an entirely separate function to execute localized actions.
Re:Lambdas in C by wbhart on Saturday July 02, 2011 @08:07AM (#2721506)
I have not been able to work out two things here:

1) If I define a lambda inside a function, I cannot return the lambda from the function and use it right?

2) Recursive lambdas are not possible on account of the lack of recursion in C macros (not sure if that even makes sense without 1).

Re:Lambdas in C by chrisjsmith on Friday July 01, 2011 @12:59PM (#2719116)
Isn't this a step closer to Greenspuns Tenth Rule?

"Any sufficiently complicated C or Fortran program contains an ad-hoc, informally-specified, bug-ridden, slow implementation of half of CommonLisp"

(bar the environment/scope issue it avoids that is)

Re:Lambdas in C by jpr on Friday July 01, 2011 @02:50PM (#2719644)
More accurate title would be "Something like, but not as useful or generic as, lambdas in gcc's dialect of C".
Re:Lambdas in C by madmoose on Friday July 01, 2011 @04:26PM (#2720018)
One might say, to paraphrase Douglas Adams, that it's almost, but not quite, entirely unlike lambdas.
Re:Lambdas in C by CPlatypus on Friday July 01, 2011 @10:07PM (#2720829)
If I worked on static code analysis tools for a living, this would make me cry.
Re:Lambdas in C by demallien on Friday July 01, 2011 @10:54AM (#2718550)

  printf("I am bad and evil %p\n", ({int bad_and_evil(void){return 0;};&bad_and_evil;}));
  printf("I am bad and evil2 %d\n", ({0x4FFFFFF;}));
  
  void* result = ({void* bad_and_evil(int dummy){return 0;};&bad_and_evil;});
  printf("bad and evil 3 = %p\n", result);
* shudder * Getting a return value from a block, by inserting it in parantheses.
Re:Lambdas in C by juiceandjuice on Friday July 01, 2011 @04:22PM (#2720004)
This is just too clumsy, and this is exactly why I think go will take off.

  package main
  
  import "fmt"
  
  func main() {
      add := func(x int, y int) int{
        return x+y
      }
      max := func(x int, y int) int{
        if x>y {return x}
        return y
      }
    fmt.Printf("lambda: add:%d\n",add(2,3))
    fmt.Printf("lambda: max:%d\n",max(2,3))
  }
Although it'd be nice if you could have a ternary or something like

  return {x} if x>y else {y}
Re:Lambdas in C by gok on Friday July 01, 2011 @05:24PM (#2720215)
Is that really that much better than C blocks?

    #include <stdio.h>
    int main (int argc, char const *argv[]) {
        int (^add)(int, int) = ^(int x, int y) {
            return x + y;
        };
        int (^max)(int, int) = ^(int x, int y) {
            return (x > y) ? x : y;
        };
        printf("block: add:%d\n", add(2,3));
        printf("block: max:%d\n", max(2,3));
        return 0;
    }
Re:Lambdas in C by juiceandjuice on Friday July 01, 2011 @05:36PM (#2720249)
I personally think so, as I think blocks in C are a clumsy way around the issue (probably because C doesn't natively have garbage collection) and it feels natural in go, but I know others will disagree.

Also, I don't think you can make it anonymous, can you?

  func(x int, y int){fmt.Printf("%d\n",x+y)}(2,4)
Re:Lambdas in C by gok on Friday July 01, 2011 @06:44PM (#2720449)
You sure can.

   ^(int x, int y){printf("%d\n",x+y);}(2,4);
Automatic memory management is interesting, but orthogonal.
Re:Lambdas in C by juiceandjuice on Friday July 01, 2011 @04:27PM (#2720020)
also, this works too:

  val := func(x int, y int) int{return x+y}(2,4)
Re:Lambdas in C by huhtenberg on Friday July 01, 2011 @06:02PM (#2720324)
Nice, but how is this useful though?