anonymous functions in c 
Author Message
 anonymous functions in c

hey all,

I was wondering if there was a way to do anonymous (lambda) functions
in C.. Its frustrating having to use function pointers all of the
time, I'd rather make functions like:

int (*myfunc)(int, int) = function(int a, int b) { return(a + b) }

or somesuch, rather than going through the rigamarole of defining a
function and only using it once...

I know its a longshot, but if *anyone* knows how to do this, it would
be greatly appreciated..

jon



Mon, 26 Sep 2005 03:37:11 GMT  
 anonymous functions in c

Quote:

> I was wondering if there was a way to do anonymous (lambda) functions
> in C.

No, C doesn't have anything like that.  You could use Java, I
guess, which has anonymous classes.
--
"Some people *are* arrogant, and others read the FAQ."
--Chris Dollin


Mon, 26 Sep 2005 04:05:53 GMT  
 anonymous functions in c

Quote:

> hey all,

> I was wondering if there was a way to do anonymous (lambda) functions
> in C.. Its frustrating having to use function pointers all of the
> time, I'd rather make functions like:

> int (*myfunc)(int, int) = function(int a, int b) { return(a + b) }

> or somesuch, rather than going through the rigamarole of defining a
> function and only using it once...

> I know its a longshot, but if *anyone* knows how to do this, it would
> be greatly appreciated..

    No; every C function must have a name.  Furthermore,
C functions do not nest; every C function must be defined
at file scope.

    Sorry -- but if you want LISP or Java, you know where
to find them.

--



Mon, 26 Sep 2005 04:14:17 GMT  
 anonymous functions in c

Quote:


> > I was wondering if there was a way to do
> > anonymous (lambda) functions
> > in C.

> No, C doesn't have anything like that.  You could
> use Java, I guess, which has anonymous classes.

The good people at boost.org have a lambda library for use with C++.
It's less of a paradigm shift than Java, (although not by much).  It's
not nearly as robust as anonymous classes are, but, depending on OP's
needs, it may suffice.  He would at least be able to incorporate his
existing C code without having to port anything.

http://www.boost.org/libs/lambda/doc/index.html

HTH,
Ryan.



Mon, 26 Sep 2005 04:15:20 GMT  
 anonymous functions in c

Quote:

> hey all,

> I was wondering if there was a way to do anonymous (lambda) functions
> in C.. Its frustrating having to use function pointers all of the
> time, I'd rather make functions like:

> int (*myfunc)(int, int) = function(int a, int b) { return(a + b) }

> or somesuch, rather than going through the rigamarole of defining a
> function and only using it once...

You can't. However, I fail to see the problem, at least with your
example. You still have to define a function that's only used once, the
difference being that you put it all on one line, and don't give it a
name.

int function(int a, int b) { return(a + b) }

int (*myfunc)(int, int) = function;

Very little difference, except that you could reuse function() if you
needed.

Perhaps you can restate the problem to show what real situation you are
having trouble with, since I don't see anything significant in the
example you give.

Brian Rodenborn



Mon, 26 Sep 2005 04:57:26 GMT  
 anonymous functions in c

Quote:

>> hey all,

>> I was wondering if there was a way to do anonymous (lambda) functions
>> in C.. Its frustrating having to use function pointers all of the
>> time, I'd rather make functions like:

>> int (*myfunc)(int, int) = function(int a, int b) { return(a + b) }

>> or somesuch, rather than going through the rigamarole of defining a
>> function and only using it once...

>> I know its a longshot, but if *anyone* knows how to do this, it would
>> be greatly appreciated..
>     No; every C function must have a name.  Furthermore,
> C functions do not nest; every C function must be defined
> at file scope.
>     Sorry -- but if you want LISP or Java, you know where
> to find them.

Or Haskell, or ML. I have some very basic experience with Haskell (only
toy programs so far) and it seems quite an interesting language.

--

| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste       W++ B OP+                     |
\----------------------------------------- Finland rules! ------------/
"How can we possibly use sex to get what we want? Sex IS what we want."
   - Dr. Frasier Crane



Mon, 26 Sep 2005 05:26:51 GMT  
 anonymous functions in c

One way to do it is to simulate the C++-way of working with functors
(on g++ undex linux, see files "function.h" or "functional" from STL
library). It may get very awkward, and, probably, not general
enough. To my knowledge, there exists no built-in way to do it as in
SML, for example.
Best regards,
sasha.mal

--
Posted via http://dbforums.com



Mon, 26 Sep 2005 04:55:18 GMT  
 anonymous functions in c
Quote:


>> (...) I'd rather make functions like:

>> int (*myfunc)(int, int) = function(int a, int b) { return(a + b) }

>> (...)

> int function(int a, int b) { return(a + b) }

  ^^^

static int is better in this case.  No name clashes with other C files.

Quote:
> int (*myfunc)(int, int) = function;

Anyway, if it really bothers you, you could do this:

#define DEF_FNVAR(rettype, name, params) \
        static rettype name##__function params; \
        rettype (*name) params = name##__function; \
        static rettype name##__function params

DEF_FNVAR(int, myfunc, (int a, int b)) { return a + b; }

It only works for functions whose prototype can be written
`returntype name (params)', and I think it's ugly, but tastes
differ.  Maybe you'd prefer it.

--
Hallvard



Mon, 26 Sep 2005 05:40:55 GMT  
 anonymous functions in c

Quote:

> hey all,

> I was wondering if there was a way to do anonymous (lambda) functions
> in C.

Not natively. You could write an interpreter for, say, scheme in C, then
do lambdas there, but you can't do it in plain C.


Mon, 26 Sep 2005 07:10:23 GMT  
 anonymous functions in c

Quote:



>> > I was wondering if there was a way to do
>> > anonymous (lambda) functions
>> > in C.

>> No, C doesn't have anything like that.  You could
>> use Java, I guess, which has anonymous classes.

> The good people at boost.org have a lambda library for use with C++.
> It's less of a paradigm shift than Java, (although not by much).  It's
> not nearly as robust as anonymous classes are, but, depending on OP's
> needs, it may suffice.  He would at least be able to incorporate his
> existing C code without having to port anything.

Wrong. Legal C code is sometimes illegal C++. Example:

#include <stdlib.h>

int main(void){
  int *x = malloc(sizeof *x); /* Syntax error in C++, good sytle in C */
  /* ... */
  free(x);
  return 0;

- Show quoted text -

Quote:
}



Mon, 26 Sep 2005 08:20:17 GMT  
 anonymous functions in c
Quote:


> > hey all,

> > I was wondering if there was a way to do anonymous (lambda) functions
> > in C.. Its frustrating having to use function pointers all of the
> > time, I'd rather make functions like:

> > int (*myfunc)(int, int) = function(int a, int b) { return(a + b) }

> > or somesuch, rather than going through the rigamarole of defining a
> > function and only using it once...

> You can't. However, I fail to see the problem, at least with your
> example. You still have to define a function that's only used once, the
> difference being that you put it all on one line, and don't give it a
> name.

> int function(int a, int b) { return(a + b) }

> int (*myfunc)(int, int) = function;

> Very little difference, except that you could reuse function() if you
> needed.

> Perhaps you can restate the problem to show what real situation you are
> having trouble with, since I don't see anything significant in the
> example you give.

well, the more I think about it, the more I'm asking the wrong
question. What I really want to do is have an 'intelligent print
statement', something like:

do_user_defined_action(variable, "if ( _1 < 500) printf(\"%d\", _1);"
);

which could, if necessary, take the string defining what the function
was going to do from a text file. So, maybe I'm not looking for a
compilable anonymous
function, but something that can *interpret* a subset of C and act on
it in a lambda-like way. (sort of like what fprintf does, but a lot
more general)

Ultimately, I want to be able to be able to instrument C code with
these statements, and be able to debug on the fly without needing to
recompile or use a de{*filter*}.

I know this is at least possible in C++ - which has the boost lambda
library ( http://www.*-*-*.com/ ) although to tell the truth I'm not sure
how well it supports runtime operation (as far as I know, only
compile-time operation is supported).

- Show quoted text -

Quote:

> Brian Rodenborn



Mon, 26 Sep 2005 10:01:25 GMT  
 anonymous functions in c

Quote:

> hey all,

> I was wondering if there was a way to do anonymous (lambda) functions
> in C.. Its frustrating having to use function pointers all of the
> time, I'd rather make functions like:

> int (*myfunc)(int, int) = function(int a, int b) { return(a + b) }

That's not a lambda function as you've given it a name. If they were
possible in C, which they're not, a lambda function would look something
like.

qsort (a, l, r, {<});

Or whatever...

Regards, Steve



Mon, 26 Sep 2005 17:03:03 GMT  
 anonymous functions in c

Quote:

> well, the more I think about it, the more I'm asking the wrong
> question. What I really want to do is have an 'intelligent print
> statement', something like:

> do_user_defined_action(variable, "if ( _1 < 500) printf(\"%d\", _1);"
> );

Not possible in C.  You'll have to write or find a C interpreter which
can be linked with a C program.  Though maybe it's good enough for you
to use som _other_ C-like language which is already meant to be linked
with C.  Lua, for example.  See <http://www.lua.org/>.

--
Hallvard



Mon, 26 Sep 2005 18:10:43 GMT  
 anonymous functions in c

Quote:


>> I was wondering if there was a way to do anonymous (lambda) functions
>> in C.. Its frustrating having to use function pointers all of the
>> time, I'd rather make functions like:

>> int (*myfunc)(int, int) = function(int a, int b) { return(a + b) }

> That's not a lambda function as you've given it a name.

I think that he's suggesting `function' as a lambda operator.

Quote:
> If they were
> possible in C, which they're not, a lambda function would look something
> like.

> qsort (a, l, r, {<});

I doubt it.  They'd probably follow the syntax for compound literals:

  qsort(a, l, r, (int (const void *a, const void *b)) {
                     return strcmp(*(const char **)a, *(const char **)b); } );

(I'm actually surprised that this isn't a GNU C extension.)

IMO there's not really any point in adding anonymous functions to C
without including closures as well, which would be difficult (if not
impossible) to do within C's execution model.

Jeremy.



Mon, 26 Sep 2005 19:42:36 GMT  
 anonymous functions in c

Quote:



>>> I was wondering if there was a way to do anonymous (lambda) functions
>>> in C.. Its frustrating having to use function pointers all of the
>>> time, I'd rather make functions like:

>>> int (*myfunc)(int, int) = function(int a, int b) { return(a + b) }

>> That's not a lambda function as you've given it a name.

> I think that he's suggesting `function' as a lambda operator.

I know that, I was referring to the "myFunc". Although I now see that there
is some value in what Jonathan is suggesting.

Quote:
>> If they were
>> possible in C, which they're not, a lambda function would look something
>> like.

>> qsort (a, l, r, {<});

> I doubt it.  They'd probably follow the syntax for compound literals:

>   qsort(a, l, r, (int (const void *a, const void *b)) {
>                      return strcmp(*(const char **)a, *(const char **)b);
>                      } );

I didn't think too hard about the syntax, I was just trying to get across
the idea of the definition of a function and the naming of the function
being seperate. Thinking about it now however, I can see your example is how
it could be done.

Quote:
> (I'm actually surprised that this isn't a GNU C extension.)

Now you mention it, me too.

Quote:
> IMO there's not really any point in adding anonymous functions to C
> without including closures as well, which would be difficult (if not
> impossible) to do within C's execution model.

I wouldn't say there's no point. If nothing else the above qsort() example
shows how it can prevent the name space from becoming too cluttered, which
is a big plus IMO.

When you say closures would be difficult in C due to C's execution model,
are you referring to the fact that C has dynamic scope rather than lexical
scope?

Regards, Steve



Mon, 26 Sep 2005 21:01:34 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

1. anonymous functions

2. Anonymous object ??

3. Anonymous union in VC++ 7

4. about "anonymous union" problem

5. anonymous struct

6. anonymous methods

7. A Jazzy NULL, and ctors for Anonymous Classes

8. Anonymous sub-structures and GCC

9. tricky casting question with anonymous structs

10. anonymous structs and unions : legal?

11. dereferencing anonymous pointers

12. Anonymous unions

 

 
Powered by phpBB® Forum Software