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

Pages: 1-4041-

Recursion is stupid and wasteful

Name: Anonymous 2013-05-23 4:36

There is nothing that recursion can do that can't be done simpler and more elegantly with a plain for loop.

Name: for loops are stupid 2013-05-23 4:45

and wasteful

There is nothing that for loops can do that can't be done simpler and more elegantly with a plain turing machine transition function specification.

Name: my anus is stupid 2013-05-23 4:47

and wasteful

Name: my dick is stupid 2013-05-23 4:53

and wasting your anus

Name: Anonymous 2013-05-23 7:05

waste my anus

Name: Anonymous 2013-05-23 10:24

Those who don't know how to recurse, will curse and curse again.

Name: Anonymous 2013-05-23 12:14

Recursion provides a simpleer solution than anything else.

Name: Anonymous 2013-05-23 12:36

>>1
I guess you have never heard about trees.

Name: Anonymous 2013-05-23 13:56

aaaanus

Name: Anonymous 2013-05-23 14:09

>>1
I guess you have never heard about tail call optimization.

Name: Anonymous 2013-05-23 14:22

>>1
I guess you have never checked my dubs.

Name: Anonymous 2013-05-23 14:40

ok, show me the solution for tower of Hanoi for N amount of disks that is simpler than the recursive solution.

Name: Anonymous 2013-05-23 14:52

>>12
Hire some slaves to move the disks around, seems simpler to me.

Name: Anonymous 2013-05-23 15:20

For most problems you can use dynamic programming and recursion is not optimal

Name: Anonymous 2013-05-23 15:34

There is nothing that plain turing machine transition function specifications can do that can't be done simpler and more elegantly with a Y combinator.

Name: Anonymous 2013-05-23 15:37

>>14
I've been wondering, what exactly does the ``dynamic'' part in ``dynamic programming'' mean?

Name: Anonymous 2013-05-23 15:39

>>16
You don't belong in /prog/.

Go back to le fa/g/got imageboard

Name: Anonymous 2013-05-23 15:50

>>16
It means you're constantly changing the code so you can trick people into buying new versions.

Name: Anonymous 2013-05-23 15:58

>>18
Isn't it just plain ITERATIVELY EVOLVING DYNAMIC CLOUD-ENABLED CODEBASE ?

Name: Anonymous 2013-05-23 17:13

>>13
Hire some slaves to move the disks around
ENTERPRISE SOLUTIONS

Name: Anonymous 2013-05-23 17:24

EXTENDING THE WIVES OF DICKS AND KIKES

Name: Anonymous 2013-05-24 3:07

>>14
What about when the sub problems are sparse and difficult to anticipate in advance? Mind you, with overlapping or not, recursion can be used in tandem with memoization.

Name: Anonymous 2013-05-24 3:56

>>13
Hire some slaves
Why would you hire a slave? Wouldn't you just buy one? Or do you mean rent one?

Also, where can I buy a slave?

Name: Anonymous 2013-05-24 4:09

>>23
Also, where can I buy a slave?
Didn't we discuss this before? The market rate for African lolies is, like, $25. I'm not sure if I should guess that a fully grown slave would cost more or less, but it would probably still be pretty cheap.

Name: Anonymous 2013-05-24 6:27

>>23
Hire, rent, same thing, point is you don't need them long term.

Name: Cudder !MhMRSATORI!fR8duoqGZdD/iE5 2013-05-24 6:54

IE's HTML parser very likely doesn't use recursion: http://ln.hixie.ch/?start=1037910467&count=1

Name: Anonymous 2013-05-24 7:35

Why would you accept invalid code?

That's the right thing do do!

Name: loop is stupid and wasteful 2013-05-24 7:54

There is nothing that a plain for loop can do that can't be done simpler and more elegantly with recursion.

Name: Anonymous 2013-05-24 8:20

def R(t, f)
  if t
    f[t]
    R(t.l, f)
    R(t.r, f)
  end
end

recurse root, lambda {|t| print t}

L = [root]
while not L.empty?
  t = L.pop
  if t
    f(t)
    L.push(t.r)
    L.push(t.l)
  end
end


No way in hell.

Name: Anonymous 2013-05-24 9:04

Ruby
No way in hell

YOU GOT THAT RIGHT, /prog/RIDER!

Name: Anonymous 2013-05-24 14:49

>>12
Fine, now show me a program that's actually useful and not just ivory tower circlejerking and can't be done better with a loop.

Name: Anonymous 2013-05-24 17:10

>>31
qsort

Name: Anonymous 2013-05-24 17:38

>>31
fibonacci buttsort

Name: Anonymous 2013-05-24 17:54

ok, show me the solution for tower of Hanoi for N amount of dicks that is simpler than the recursive solution.

Name: Anonymous 2013-05-25 7:53

>>31
I don't understand what you mean by ``can't be done better with a loop''. Loop by itself is terrible.

Name: Anonymous 2013-05-25 13:51

Ring > Spiral > Loop

Name: Anonymous 2013-05-25 17:05

>>36
Star > Ring.  Ethernet beat FDDI, deal with it.

Name: Anonymous 2013-05-25 20:07

>>32
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#define LEN 200
#define SUB_LIMIT 8

static void print_arr(const int *data, const int n)
{
  int i;
  printf("[%d", data[0]);
  for (i = 1; i < n; ++i)
    printf(", %d", data[i]);
  printf("]\n");
}

/* explicit stack */
static int *explicit_stack;
static int stacklen = 0, stackpos = 0;

static void push(int i)
{
  if (stackpos >= stacklen) {
    stacklen *= 2;
    explicit_stack = realloc(explicit_stack, stacklen * sizeof (int));
  }
  explicit_stack[stackpos++] = i;
}

static int pop(){
  if (stackpos >= 0)
    return explicit_stack[--stackpos];
  return -1;
}
/* explicit stack */

/* like hell will I implement qsort down to width 1 */
static void subsort(int *data, int from, int to)
{
  int *curr, temp_val, *temp, *min;

  if (from < 0) from = 0;
  if (to >= LEN) to = LEN - 1;
  if (from >= to) return;

  for (curr = data + from; curr <= data + to; ++curr) {
    for (min = curr, temp = min + 1; temp <= data + to; ++temp)
      if (*temp < *min)
        min = temp;

    temp_val = *curr;
    *curr = *min;
    *min = temp_val;
  }
}

static int mid(const int a, const int b, const int c)
{
  if (a < b)
    return (b < c) ? b : c;
  return (a < c) ? a : c;
}

/******************************************************************************
 * vvv THIS IS THE FUNCTION YOU ASKED FOR vvv                                 *
 ******************************************************************************/
void nonrec_qsort(int *data, const int o_from, const int o_to, int *holding) {
  int midval, *lside, *rside, *curr, from, to;
  from = o_from;
  to = o_to;

 i_bet_youd_rather_i_used_while_well_fuck_YOU:
  if ((to - from) <= SUB_LIMIT) {
    /* if set is small enough, use an n^2 sort */
    subsort(data, from, to);

    /* qsort always ends with subsorting the last sub-interval */
    if (to == o_to) {
      return;
    }

    from = to + 1;
    to = pop();
  } else {
    /* partition and resize to leftmost side, storing rightmost index */
    lside = holding + from;
    rside = holding + to;
    curr = data + from;

    midval = mid(data[(from + to)/2], data[from], data[to]);

    while (curr <= data + to) {
      if (*curr < midval)
        *lside++ = *curr;
      else if (*curr > midval)
        *rside-- = *curr;
      curr++;
    }

    /* all values >, < midval have been taken care of, = must now be handled */
    for (curr = lside; curr <= rside; ++curr)
      *curr = midval;

    memcpy(data + from, holding + from, (to - from + 1) * sizeof(int));

    push(to);
    to = curr - holding - 1;
  }
  goto i_bet_youd_rather_i_used_while_well_fuck_YOU;
}
/******************************************************************************
 * ^^^ THIS IS THE FUNCTION YOU ASKED FOR ^^^                                 *
 ******************************************************************************/

int main()
{
  int i, *data_to_sort, *holding;

  /* this should be set up inside nonrec_qsort, but here for clarity */
  stacklen = 1;
  explicit_stack = (int *)calloc(stacklen, sizeof(int));

  data_to_sort = (int *)calloc(LEN, sizeof(int));
  holding = (int *)calloc(LEN, sizeof(int));

  srand(time(NULL));
  for (i = 0; i < LEN; ++i)
    data_to_sort[i] = (rand() % 900) + 100;

  print_arr(data_to_sort, LEN);
  nonrec_qsort(data_to_sort, 0, LEN - 1, holding);
  print_arr(data_to_sort, LEN);

  free(data_to_sort);
  free(holding);
  free(explicit_stack);
  return 0;
}


That's so you can run it and see if it works.  If you remove the explicit partition code (which everyone always does when they try to show off how sexy and ELEGANT qsort is), it's


void nonrec_qsort(int *data, const int o_from, const int o_to, int *holding) {
  int from = o_from, to = o_to;
  stack *explicit_stack = make_stack();

  while(true) {
    if ((to - from) <= SUB_LIMIT) {
      subsort(data, from, to);

      if (to == o_to) {
        free_stack(explicit_stack);
        return;
      }

      from = to + 1;
      to = pop(explicit_stack);
    } else {
      push(explicit_stack, to);
      to = partition(data, from, to);
    }
  }
}


I think it's a little less elegant than recursive qsort (I'm not OP), but it's not a horrible monster.

Name: Anonymous 2013-05-25 20:12

>>38
Oh, and remove the int *holding parameter from the elegant version, of course, that was only used by the not-in-place implementation of partition().

Name: ^_____________________^ 2013-05-25 20:37


                        +------------+
                        |  グループ  |
                        +------------+

グループ                        アドレス             サイズ
=====                           =======              ====

DGROUP                          00403000             0000a14d



                        +--------------+
                        |  セグメント  |
                        +--------------+

セグメント             クラス         グループ      アドレス        サイズ
=========              ======         =========      ========        ======

_TEXT                  CODE           AUTO           00401000        000017e6
CONST                  DATA           DGROUP         00403000        000000cf
CONST2                 DATA           DGROUP         004030d0        00000000
_DATA                  DATA           DGROUP         004030d0        00000042
_BSS                   BSS            DGROUP         00403114        0000a039


                        +----------------+
                        | メモリ・マップ |
                        +----------------+

* = 参照されないシンボル
+ = シンボルはローカルでの参照のみです

Name: Anonymous 2013-05-25 20:50

>>36
Are you talking about the books? If so, than you are retarded. They all suck except Ring.

Name: Anonymous 2013-05-25 20:58

>>40
PC-98 is so kawaii.

Name: Anonymous 2013-05-25 21:11

/* like hell will I implement qsort down to width 1 */
why the fuck not faggot

Name: Anonymous 2013-05-25 22:09

>>43
Do it yourself.  The non-recursive version doesn't really require any added complexity to switch off sorting methods, because checking for completion is the same as checking for small problem size, and it would be suboptimal anyway, I'm pretty sure

As a side note, the stack used in the nonrecursive version requires only one argument pushed, whereas traditional recursive qsort requires much more (instruction pointer, data, length).  If the partition() method is sufficient, the maximum depth of the stack can be calculated beforehand to remove the irritating realloc() calls (I think my version bounds it as (to-from)/2).  Also, the touted multithreading of qsort can still be used, especially since the explicit stack allows forking/threading at a predetermined arbitrary level. So the non recursive version pretty much uses better than tail-recursion for both calls, and the only overhead is moving the stack from implicit to explicit representation.

Name: Anonymous 2013-05-26 8:44

>>44
WAT R U, A FUCKIN STACK BOY OR SUM SHIT?

Name: Anonymous 2013-05-26 10:11

>>45
HOW BOUT I POP U INNA MOUTH THEN PUSH YOU ON UR ASS!

Name: Anonymous 2013-05-26 15:43

Enjoy your stack overflows, recursive fagshits.

Name: Anonymous 2013-05-26 15:54

>>47
Tail call optimization ``in Lithp''

Name: Anonymous 2013-05-26 15:56

>>48
Any place a Tail Call optimization is used, it can be implemented just as well or better by a goto.  Furthermore, these gotos don't have to be at the very end of the function, so you don't have to rely on a magic position to make your code not suck.

Name: Anonymous 2013-05-27 15:32

>>1
(defmemo fast-fib (x) (if (< x 2) x (+ (fib (- x 2)) (fib (- x 1)))))

;; definition of defmemo cut out for simplicity (though it isn't hard to write)

Name: Anonymous 2013-05-27 15:43

Any place a goto is used, it can be implemented just as well or better by a jump instruction.  Furthermore, these jumps don't have to be local to a function, so you don't have to rely on a magic position to make your code not suck.

Name: Anonymous 2013-05-27 15:50

>>50

*(defmemo fast-fib (x) (if (< x 2) x (+ (fast-fib (- x 2)) (fast-fib (- x 1)))))

Name: Anonymous 2013-05-27 16:01

nested for loops are at the top of elegance.

Name: Anonymous 2013-05-27 16:57

>>50
that's not fast, that's just not horrendously slow.

you went from exponential to linear but you can still go logarithmic - if you knew how..

Name: Anonymous 2013-05-27 19:45

that's not hot, that's just not horrendously plain.

you went from my mouth to my vagina, but you can still go anal - if you knew how..

Name: Anonymous 2013-05-27 22:00

>>54
I wasn't saying it was very fast, but it is probably simpler and more elegant than an iterative solution.

Name: Anonymous 2013-05-27 22:00

>>56
nevermind, i did call it fast-fib.

Name: Anonymous 2013-05-28 3:31

>>44
This optimization is easy for any function that only makes calls to itself. It might be possible in more general scenarios too but I need to think about it.

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