Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Lambdas in GNU-C

Name: Anonymous 2011-09-07 23:03

So while watching the Baywatch that takes place on Hawaii I came up with this thing in GNU-C.
I don't know any Lisp or any Lambda calculus but I was wondering if these qualify for ``closures'' and/or ``anonymous functions''?

#include <stdlib.h>
#include <stdio.h>

#define lambda(type, ...)            \
  ({                        \
    type (*__lambda_fptr)(__VA_ARGS__);        \
    type __lambda(__VA_ARGS__)

#define lambda_end                \
    __lambda_fptr = __lambda;            \
  })

int main(void) {
  int i, * array;
  const int size = 10;

  array = malloc(size * sizeof(int));

  for (i = 0; i < size; i++)
    array[i] = -i;

  qsort(
    array,
    size,
    sizeof(int),
    lambda(int, const void * _a, const void * _b) {
      int * a = (int *) _a;
      int * b = (int *) _b;
      return *a - *b;
    } lambda_end );

  for (i = 0; i < size; i++)
    printf("%d: %d\n", i, array[i]);

  free(array);

  return 0;
}


If it helps you may also nest the definitions and call the functions directly like this:

#include <stdio.h>

#define lambda(type, ...)                       \
  ({                                            \
    type (*__lambda_fptr)(__VA_ARGS__);         \
    type __lambda(__VA_ARGS__)

#define lambda_end                              \
    __lambda_fptr = __lambda;                   \
  })

int main(void) {
  printf("%f\n",
         lambda(double, double x) {
           return 2.0 * lambda(double, double x) {
             return 3.0 * x;
           } lambda_end (x);
         } lambda_end (5.0) );

  return 0;
}


No doubt the syntax is horrendous, there's probably some way to prettify it.

The Baywatch that takes place in Hawaii is pretty good in my opinion, it has David Hasselhoff as its executive producer I think. I just watched an episode where a stalker starts liking, dating and stalking one of the Baywatch women and she wears really suggestive clothing, I wasn't really paying attention but I think he (the stalker) earlier in the episode shot some other dude under the water in the hand (I think it was the stalkers friend but I'm not sure) with a harpoon and he was all like ``it was an accident!'' but people were like meh. Later on he knew the Baywatch ladies preference (it was that she liked soy milk in her coffee) without her ever telling him to which she was like wow this is weird. He later showed up at her house and he had turned the gas on and was acting all crazy and knocked the Baywatch lady out, she was like ``oh my god!'' and he was like ``I'M GOOD FOR YOU!'' with a really angry face but then he got knocked out by one of the Baywatch dudes and they saved the chick. Some other chick was dating a dude that was about to enter the witness protection program I don't know what the deal was but I think it was the Mafia that was after him.

Name: Anonymous 2011-09-07 23:06

By the way I don't really know what closures or anonymous functions are.

Name: Anonymous 2011-09-07 23:09

The way the stalker found out that she liked soy milk in her coffee was by calling all of her colleagues, why would they tell him that doesn't make sense?

Name: Anonymous 2011-09-08 1:07

that's an anonymous function, but not a closure. Closures capture their enclosing scope and take it with them on the heap.

Name: Anonymous 2011-09-08 11:59

>>4

So if it were a closure it would be able to handle stuff like this?

#include <stdio.h>

#define lambda(type, ...)            \
  ({                        \
    type (*__lambda_fptr)(__VA_ARGS__);        \
    type __lambda(__VA_ARGS__)

#define lambda_end                \
      __lambda_fptr = __lambda;            \
  })

typedef int (*intfun)(void);

intfun outer(void) {
  int y = 0;
  return lambda(int) {
    return y++;
  } lambda_end;
}

int main(void) {
  int i;
  intfun f = outer();

  for (i = 0; i < 3; i++)
    printf("%d\n", f());

  return 0;
}


That is a major setback, I don't know how to do that.

Name: Anonymous 2011-09-08 14:10

>>5
Switch to sepples0x, or Clang.

Name: Anonymous 2011-09-08 14:41

>Baywatch
Its like we live in the 90's and its the only porn replacement available.

Name: Anonymous 2011-09-08 15:10

neat

was on reddit few months ago though.

Name: Anonymous 2011-09-08 21:03

>>5
exactly. You even rediscovered Paul Graham's "accgen"
http://www.paulgraham.com/accgen.html

but sepples can sort of do it with auto_pointer or whatever. Kind of.

Don't change these.
Name: Email:
Entire Thread Thread List