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

Pages: 1-4041-

Common Lisp in C

Name: Anonymous 2012-10-03 17:23

Lets write Common Lisp functions in generic C code.


/* (map #'func array) => map(array,length_of_array,sizeof(array),function_pointer) */
void *map(void *base,size_t len,size_t elemnsize, void (*func)(void *,void *)) {
  void *b = malloc(elemsize*len);
  size_t i;
  for(i=0;i<len;++i){
    func(b+(elemsize*1),base);
    base += elemsize;
  }
  return b;
}

Name: Anonymous 2012-10-03 18:33

What a waste of time.

Name: Anonymous 2012-10-03 18:40

>>2

2mins stolen from my SICP reading time

Name: Anonymous 2012-10-03 18:48


#define lambda(l_ret_type, l_arguments, l_body)         \
  ({                                                    \
    l_ret_type l_anonymous_functions_name l_arguments   \
      l_body                                            \
    &l_anonymous_functions_name;                        \
  })

Name: Anonymous 2012-10-03 18:59

>>1
You should never malloc on one level and have the user free on another, terrible design.

Name: Anonymous 2012-10-03 19:17

>>1,5
Here is a version that accepts any type of array provided by the user, if the user wants to use malloc it can and more importantly the user may do malloc and free on the same level also the user may use stack arrays like a normal human being, you also avoid casting away type information through void pointers and look, you may even use different types of arrays in it.

#include <stdio.h>

#define unary(arg, exp)                         \
  __extension__                                 \
  ({                                            \
    arg;                                        \
    __typeof__(exp) _unary (arg) {              \
      return exp;                               \
    } _unary;                                   \
  })

#define map(in, size, func, out)                \
  do {                                          \
    size_t _i;                                  \
                                                \
    for (_i = 0; _i < (size); _i++)             \
      (out)[_i] = func((in)[_i]);               \
  } while (0)

#define N 10

int main (void) {
  int i;
  int a[N];
  double b[N];

  for (i = 0; i < N; i++)
    a[i] = i;

  map(a, N, unary(int n, n*n), b);

  for (i = 0; i < N; i++)
    printf("%d -> %f\n", a[i], b[i]);

  return 0;
}

Name: Anonymous 2012-10-03 19:21

>>3
You may be joking, but I'm halfway through chapter 3 again

Name: Anonymous 2012-10-03 19:27

>>7
May the Sussman bless you.

Name: Anonymous 2012-10-03 19:57

>>6
You can do a fold implementation with the same style by the way, but first I want to let it be known that I do not endorse actually writing code in this style.

Obviously these macros need sanitation when it comes to side-effects etc. but that is pretty trivial when we're already using extensions and is left as an exercise for the reader.

#include <stdio.h>

#define binary(arg1, arg2, exp)                 \
  __extension__                                 \
  ({                                            \
    arg1; arg2;                                 \
    __typeof__ (exp) _binary (arg1, arg2) {     \
      return exp;                               \
    } _binary;                                  \
  })

#define foldl(a, size, func)                    \
  __extension__                                 \
  ({                                            \
    size_t _i;                                  \
    __typeof__(func((a[0]), a[0])) _acc = a[0]; \
                                                \
    for (_i = 1; _i < (size); _i++)             \
      _acc = func(_acc, a[_i]);                 \
                                                \
    _acc;                                       \
  })

#define N 10

int main (void) {
  int i;
  int a[N];

  for (i = 0; i < N; i++)
    a[i] = i+1;

  printf (
    "%d\n",
    foldl (
      a, N,
      binary(int m, int n, m+n)
    )
  );

  return 0;
}

Name: Anonymous 2012-10-03 22:05

>>5
back to /g/

Name: Anonymous 2012-10-03 23:29

>>10
So are you really arguing for stupid design? I'd love to hear your shitty arguments, I bet they're pretty amusing.

/backplate getgoes/ newfriend

Name: Anonymous 2012-10-03 23:45

Name: Anonymous 2012-10-04 6:46

>>5
What are these 'levels' you are ranting about?

Name: Anonymous 2012-10-04 8:28

>>12
Consider using a garbage collector that doesn't leak memory and isn't full of unportable hacks, instead.

Name: Anonymous 2012-10-04 9:05


>>11
Go home kid

Name: Anonymous 2012-10-04 11:38

>>13
You don't open/malloc on a higher level (inside a scope) and then let that bubble out to the user, design like that is extremely error prone. If you absolutely have to do it you should always provide an equivalent close/free function. If you can you should always let the user provide the allocated memory, since then they are free to allocate and deallocate however they wish.

Mandating malloc in your program through your API like this (you tell the end user to free the result, at least I hope you do) is also bad, since you can't ever change the function to not use malloc, this is another reason for providing an equivalent close/free function.

Not to mention that malloc typically performs bad in multi-threaded programs and can also fail (which you don't even account for in your map function).

Either way, you can program a map function, or better, a function-like macro that can't fail, won't use dynamic memory allocation and the user won't have to free the result unless they called malloc themselves in less and clearer code than what you wrote, I think the advantages are obvious.

Name: Anonymous 2012-10-04 12:40

Or you can just write a destructive version of map and stop being autistic .

I highly doubt a single map function would act as an API hidden from the 'user' to begin with.

In the end your issue come down to a codemonkey that didnt read the api docs or source code to realize he needs to free the reault.result with that being said i doubt he would have even remembered to free his own allocations within the same scooe.scope

Name: Anonymous 2012-10-04 12:54

>>17
Or you can just write a destructive version of map and stop being autistic .
What if you need another array? I'm all for just writing a god damned for loop anyway.

I highly doubt a single map function would act as an API hidden from the 'user' to begin with.
Normally it would be bundled with other functional stuff, seems like OP is trying to implement some. And obviously it wouldn't be hidden, it would be part of the API.

In the end your issue come down to a codemonkey that didnt read the api docs or source code to realize he needs to free the reault.
This isn't the issue, people who know you have to use free with malloc sometimes forget to use free because humans make mistakes also when large bodies of code create something complex it might be difficult to trace where you should free anyway, it's just a good principle to never let pointers to things you have to clean up escape from your control.

Look, it's pretty clear, one implementation of this function can fail through use of malloc and easily leads to errors, it's also less flexible in terms of control for the end user and it has more code, whilst the other can't fail and the user controls the allocation and has less code, as I said, the advantages are obvious.

Name: Anonymous 2012-10-04 13:06

>>18
What if you need another array?
Make a copy of the original and act on that instead.

Name: Anonymous 2012-10-04 13:14

>>19
So now you pass twice over the array instead of just once?

Name: Anonymous 2012-10-04 13:23

>>22
nice dubs bro

Name: Anonymous 2012-10-04 13:35

>>21
Thanks.

Name: Anonymous 2012-10-04 13:54

>>20
You don't even need to copy the results,  just transverse over both the old and new , storing results acted on the old in the new

Name: Anonymous 2012-10-04 13:57

>>6
Can i use this in C89/99?

How does gcc handle scope names if i named another variable _i when using the that?

Name: Anonymous 2012-10-04 14:42

Probably can compile most useful features of Symta directly into C/C++, but it requires `eval`, due to macros. Also, there would be no closures, because C/C++ doesnt support generating functions of fly. There is http://www.gnu.org/software/lightning/ but I hate Stallman because he is Jewish hypocrite.

Name: Anonymous 2012-10-04 14:43

>>20
Copying data isn't slow.

Name: Anonymous 2012-10-04 14:44

>>14
Boehm is the best you can get with C/C++.

Name: Anonymous 2012-10-04 14:53

Ultimate real-time garbage collector:

#define MAX_LSTS 1024
#define MAX_ELTS 1024
int _Pool[MAX_LSTS][MAX_ELTS+1];
int _PoolP=0;

int *newList(int N) {
  _Pool[_PoolP%MAX_LSTS][MAX_ELTS] = N;
  return _Pool[_PoolP++%MAX_LSTS];
}

int len(int *Xs) {return Xs[MAX_ELTS];}

int main(int argc, char **argv) {
  int *Xs = newList(100);
  printf("%d\n", len(Xs));
  return 0;
}


Very fast and robust. Can be easily distributed.

Name: Anonymous 2012-10-04 15:06


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

#define lambda(l_ret_type, l_arguments, l_body)         \
  ({                                                    \
    l_ret_type l_anonymous_functions_name l_arguments   \
      l_body                                            \
    &l_anonymous_functions_name;                        \
  })

#define MAX_LSTS 1024
#define MAX_ELTS 1024
int _Pool[MAX_LSTS][MAX_ELTS+1];
int _PoolP=0;

int *newList(int N) {
  _Pool[_PoolP%MAX_LSTS][MAX_ELTS] = N;
  return _Pool[_PoolP++%MAX_LSTS];
}

int len(int *Xs) {return Xs[MAX_ELTS];}

int *rng(int S, int E) {
  int I = S<E ? 1 : -1;
  int *Xs = newList(abs(E-S)+1);
  int *P = Xs;
  int *T = P + len(Xs);
  while (P<T) {
    *P++ = S;
    S += I;
  }
  return Xs;
}

int *each(int *Xs, void (*f)(int)) {
  int I, N = len(Xs);
  for (I=0; I<N; I++) f(Xs[I]);
  return Xs;
}

int main(int argc, char **argv) {
  each(rng(5,55), lambda(void,(int V), {printf("%d\n", V);}));
  return 0;
}

Name: Anonymous 2012-10-04 15:14

>>29
Rock solid C++ code!

Name: Anonymous 2012-10-04 16:01

>>29

int *new_list(int N) {
   static int Pool[MAX_LSTS][MAX_ELTS+1];
   static int PoolP=0;
  Pool[PoolP%MAX_LSTS][MAX_ELTS] = N;
  return Pool[PoolP++%MAX_LSTS];
}

Name: Anonymous 2012-10-04 17:33

"Common Lisp" sounds like "Commu Nism"

Name: Anonymous 2012-10-04 17:37

>>32
and "Scheme" sounds like Stalin.

Name: Anonymous 2012-10-04 17:50

>>33
And Rob Pike sounds like Gold kike

Name: Anonymous 2012-10-04 19:36

>>34
And you sounds like a retard.

Name: Anonymous 2012-10-04 20:03

>>35
Thank god someone else is mad at the jewspammer.

Name: Anonymous 2012-10-04 20:10

>>36
If only he kept all his idiotic bullshit in just one thread.  But no, he has to shit up the entire fucking board.  At least if he used a tripcode or name I could just filter out his posts.

Name: Anonymous 2012-10-04 20:24

>>35
>>36
>>37
Samejew!
Shalom, Hymie!

Name: Anonymous 2012-10-04 20:27

>>38
You use unoptimized quotes on purpose, right? You want me to tell you to optimize your quotes, Ahmed?

I'm flattered, Ahmed-chan.

Name: Anonymous 2012-10-04 21:10

>>37
Just filter ``Jews'' and ``Shalom'' and you should be good.

Name: Anonymous 2012-10-04 22:12

When did people start hating anti-jew posts?

Name: Anonymous 2012-10-04 22:43

>>41
Since when they became "people"?

Name: Anonymous 2012-10-04 22:45

>>40
Shåløm! Unicode is BIG.

Name: Anonymous 2012-10-04 23:15

>>40
>>39
Shalom!

Name: Anonymous 2012-10-05 19:49

>>23
This isn't what he's suggesting, that is what I am suggesting.

>>24
Can i use this in C89/99?
You can use the map macro, but you won't really gain any thing over just a for loop. You obviously can't use the nested function thing, that's GCC specific.

How does gcc handle scope names if i named another variable _i when using the that?
Just like it would any other scope, it works fine.

>>26
It's O(n) slower than not doing anything at all, and this is noticeable  when you don't have to do it.

Name: Anonymous 2013-09-01 10:49



                rァ=-、_     ,.rァ‐-、
        rr-‐rァ    「/::::::::::ヽ!、    i/:::::::/7
        Y::::::〈!     '、!'⌒ヽ、_y'ー、,/::::::/!7
        ヽ;::::ヽ     ,!>-ァ'ヽr-ヘト、ン/:::::!'
    /    !}::::!{ ,.. '"´く::::/}      `y^i'ー'^ヽ.   目が回るぅ~
   .,'      ヽゝ/    Y-'      /:::!{  、 ヽ.
   i  , '"´   'y'  ./  /!   ,'  ,  く:;__」}   ':,  '、
          ;'  ./   /ー!--! /'!   ハ  ',   ',ヽ、',
     i´`ヽ.,  L__;'  _/,.',ニ、ヽレ'  ! ァ'ー!-'、! i  i:::::: `'' ー-‐=ァ
    ,.、_!,   ヽ.  `>、,ハ.! !,_ー' ノ  レ' ,.',ニ、ヽ.! !  i`ヽ、:::::::::::::/
    ヽ、,___   `'ァ'   7' `'ー‐'      ! !,_ー' ノr┴-、!  ,' `ー='
       `ヽ、, '   .イ⊃ ,.-_、.,,__   `'ー‐' /   !   /
          /   .ノゝ、i´     ̄`ヽ. ⊂⊃ .,'  /
         ,'   ,.イ   /!>.、.,__  __,!,. イ   ,:' イ
         !___/_ノ!__,,...'-‐''"´   ̄    ./ヽ、ノ    ',    .!
   (     }i::::::`r、_,,. -‐rァ、.,,________,,,,... イ _,rメ、!7、   !   ,'
    `"'' ー }レ'7へ:::::::::::!{><:::::::i    rン'::::::::::ヾ!  ノ   ノ
         (.  ン^ヽ、」}ニ<:::::::::i   r/!::::_r'" ̄ヽ!_    ´
      rァー_r)'":::::ヽ;ヽ、::::::::::::::;:イ!__,/::!ヤ、__'、   7'iーァ、,__,,.. -‐ァ
    _rァ''"´:::::::::::::::/`ーヘ、__二ン::::::::::::::::::`'く >.、.,!/::/ `ヽ:::::/」
   ノ」:::::::::::::::::::::'´:::::::::::/::::::::::::::!::::::,.-‐‐-::::::: `ヽ.   `ヽ、)、___ノ ̄
    !ヘ;:::::::::::::::::::::::::::::::::::::::::::::::::::γ::: y'⌒ヽ:::::::::::::':,         __
    ヽ,へ:::::::::::::::::::::::::::::::::::::::::::::::i:::::::!::::ー- ':::!:::::::::::::',      - 、 `ヽ.
    /;:-X::>、:;__::::::::::::::::::::::::::::::メ::::::::ゝ、;__ ノ::::::::::::::::〉、   ___,,..ノ  ノ
    ,'::::::::::/`'' ー、,'^ヽ、:;____:::::::メ::::::::___::::______:::_;;:::イ ン'
 ( ( `' ー'        ̄`"'ー-、.,_二ン'-r'"ー-‐'"    ̄
                  |::::X:::7
                  !:::::::::;'
                  `'ー-'

Name: Anonymous 2013-09-01 11:02

>>45
It's about O(n/c + k) where k is at most 1/500 the time it takes a neuron to fire and c in the thousands. You won't—can't—notice it unless n is astronomical.

What's slow is managing that memory. Which is waaaaaaaay more than O(n) if it contains pointers or lives for any appreciable amount of time.

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