"delta function" at a - C

This is a discussion on "delta function" at a - C ; For a while today, I thought I needed a function delta(int p) that would return a function of int t whose value is 1 if p equals t and 0 if not. I figured out how not to need it, ...

+ Reply to Thread
Results 1 to 4 of 4

"delta function" at a

  1. Default "delta function" at a

    For a while today, I thought I needed a function delta(int p) that
    would return a function of int t whose value is 1 if p equals t and
    0 if not. I figured out how not to need it, since I was having a lot
    of trouble writing it. But I'm still interested in knowing how it would
    be done. I tried declaring it

    int (delta(int p))(int t)

    and hopefully that is right. The reason I was trying to do this was that
    I wanted to be able to use the function delta(p) of t as an argument to
    another function. If you know how to do this, please let me know. Thanks.
    --
    Ignorantly,
    Allan Adler <ara@zurich.csail.mit.edu>
    * Disclaimer: I am a guest and *not* a member of the MIT CSAIL. My actions and
    * comments do not reflect in any way on MIT. Also, I am nowhere near Boston.
    --
    comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
    have an appropriate newsgroups line in your header for your mail to be seen,
    or the newsgroup name in square brackets in the subject line. Sorry.

  2. Default Re: "delta function" at a

    "Allan Adler" <ara@nestle.csail.mit.edu> wrote in message
    news:clcm-20060917-0056@plethora.net...
    > For a while today, I thought I needed a function delta(int p) that
    > would return a function of int t whose value is 1 if p equals t and
    > 0 if not. I figured out how not to need it, since I was having a lot
    > of trouble writing it. But I'm still interested in knowing how it would
    > be done. I tried declaring it
    > int (delta(int p))(int t)
    > and hopefully that is right. The reason I was trying to do this was that
    > I wanted to be able to use the function delta(p) of t as an argument to
    > another function. If you know how to do this, please let me know. Thanks.


    Actually in C one has to return a pointer to the function, not a function.

    However, you seem to be confusing C with mathematics. There is no
    such thing in C as a "function of int t". Parameter names are merely
    placeholders and have no persistent meaning. So if there is some
    function delta taking a single argument of type int, that argument
    cannot be compared to some other argument that is not declared
    as an argument to the same function. The closest you can get is to
    set aside a static object to hold the value of p, which the delta
    function could then refer to when passed a value of t.
    A more conventional approach is to use a 2-argument function
    int delta(int p, int t).
    --
    comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
    have an appropriate newsgroups line in your header for your mail to be seen,
    or the newsgroup name in square brackets in the subject line. Sorry.

  3. Default Re: "delta function" at a

    Allan Adler <ara@nestle.csail.mit.edu> wrote:
    > For a while today, I thought I needed a function delta(int p) that
    > would return a function of int t whose value is 1 if p equals t and
    > 0 if not.


    No way. C functions can return functions, only function pointers.
    And those have to be compiled before you can take their address.
    Generally, what you're trying can not be done in any statically
    linked, compiled language like C.

    --
    Hans-Bernhard Broeker (broeker@physik.rwth-aachen.de)
    Even if all the snow were burnt, ashes would remain.
    --
    comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
    have an appropriate newsgroups line in your header for your mail to be seen,
    or the newsgroup name in square brackets in the subject line. Sorry.

  4. Default Re: "delta function" at a

    Allan Adler wrote:
    > For a while today, I thought I needed a function delta(int p) that
    > would return a function of int t whose value is 1 if p equals t and
    > 0 if not. I figured out how not to need it, since I was having a lot
    > of trouble writing it. But I'm still interested in knowing how it would
    > be done. I tried declaring it
    >
    > int (delta(int p))(int t)
    >
    > and hopefully that is right. The reason I was trying to do this was that
    > I wanted to be able to use the function delta(p) of t as an argument to
    > another function. If you know how to do this, please let me know. Thanks.


    You can do this in standard C only for special cases.
    1. At any given time only one such function will be used - set a global
    static variable delta_t, and always pass the same 'delta' function.
    2. These functions use only a small finite number of possible values of
    't'. Write down all these functions and return the right function ptr
    searching in an array of these possible 't's.
    3. A combination of 1 and 2 - at any given time only a small finite
    number of values of 't' is used. Keep a bi-directional map between
    these 't's and their function ptrs. Every function will find its 't'
    using this map, and for every 't' you'll allways find the corresponding
    function.

    The problem is that in standard C all the functions that you use are
    known in advance, and if you have only 50 functions, you cannot use 200
    different 'delta' functions in your program at the same time.

    Some non-standard or non-C solutions:
    1. Use nested functions of GCC. Remember that you cannot use the value
    of the nested function after return from the enclosing function.
    2. Use function objects of C++. This is the best solution.
    3. Create these functions as data, wih a 't' inside, and return a ptr
    to them. This is what some functional languages do. This is not
    portable, but for every machine and calling convention you can easily
    find out the layout of these simple functions. This can be made almost
    portable compiling a sample function, ****yzing its assembly output,
    and creating a .c file containing the data and the offset of 't' in the
    function using a script. For a process where the stack/data pages are
    not executable this wouldn't work.
    4. Create a small .c file with this function (using fprintf),
    compile+link it into a shared library, load this library and get a
    function ptr. Don't forget to erase these libraries on exit.

    P.S. I don't know if your prototype is right. I always do typedef for
    every function ptr I use, this is much more readable for me and most
    other readers:

    typedef int int2int_f(int);
    int2int_f * delta_func(int);

    Michael
    --
    comp.lang.c.moderated - moderation address: clcm@plethora.net -- you must
    have an appropriate newsgroups line in your header for your mail to be seen,
    or the newsgroup name in square brackets in the subject line. Sorry.

+ Reply to Thread

Similar Threads

  1. Replies: 5
    Last Post: 12-10-2007, 02:44 PM
  2. email-display: "N", "D", " ": yeah; but "O"? How to MARK "O"?
    By Application Development in forum Mutt
    Replies: 0
    Last Post: 10-30-2007, 07:13 AM
  3. Why function TYPE returns "C" not "M" on memo fields in SQLRDD ?
    By Application Development in forum xharbour
    Replies: 6
    Last Post: 10-23-2007, 04:31 AM
  4. Replies: 0
    Last Post: 03-21-2007, 01:26 PM
  5. """""""""""""""""""""Visual C++ 2005 Express"""""""""""""""""
    By Application Development in forum DOTNET
    Replies: 0
    Last Post: 03-12-2006, 03:55 AM