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

The x-Done Loop

Name: Cudder !MhMRSATORI!fR8duoqGZdD/iE5 2013-01-07 7:58

This is a construct that is very useful to avoid either a redundant check or a goto. This...
for(a;b;c) {
 expr1;
 if(d) {
  expr4;
  goto done;
 }
 expr2;
}
 expr3;
done:

...becomes...
for(a;b;c) {
 expr1;
 if(d) {
  expr4;
  break;
 }
 expr2;
} done {
 expr3;
}


where the intent is for statements in a done clause immediately following a loop to be executed if and only if the loop was exited via its termination condition being reached. This also generalises for while() and do-while, here using some common examples.

while(*j) {
 if(*j++ == k) {
  u = v + k;
  break;
 }
} done
 u = v - k;


do {
 if(f(a[i]))
  break;
} while(a[i++]) done {
 g();
}


Discuss.

Name: Anonymous 2013-01-07 8:00

So, like a finally except for conditional statements? Sounds interesting.

Now let's get Google to implement it in Go.

Name: Anonymous 2013-01-07 8:00

do while considered harmful.

Name: Cudder !MhMRSATORI!fR8duoqGZdD/iE5 2013-01-07 8:01

This is very useful for writing array searches and the like, as otherwise you either have to check for notfound/loop terminated a second time (stupid) or use a goto (which pisses off the purists).

Name: Anonymous 2013-01-07 8:02

>>4
People who don't know the length of an array beforehand should be lined up and shot O(n) times.

Name: Cudder !MhMRSATORI!fR8duoqGZdD/iE5 2013-01-07 8:04

>>2
It's the exact opposite, finally is always executed.

For symmetry I suppose you could have a done-else:

while(*j) {
 if(*j++ == k)
  break;
} done
 u = v - k;
else
 u = v + k;


But this is starting to obscure the flow even more than goto, so...

Name: Anonymous 2013-01-07 8:05

>>1
C/C++: anal acrobatics every day. Proven by Cudder!

Name: Anonymous 2013-01-07 8:08

>>5
What if the problem requires me to append stuff into the array while looping through it.

Name: Anonymous 2013-01-07 8:09

>>1
Any sufficiently complicated C or Fortran program contains an ad hoc informally-specified bug-ridden slow implementation of half of Common Lisp. -- Greenspun's Tenth Rule

Name: Anonymous 2013-01-07 8:10

Any language that doesn't let me define more syntax is shit.

Name: Anonymous 2013-01-07 8:11

>>9
http://www.apl.jhu.edu/~hall/Lisp-Notes/Loop-Summary.html
FINALLY specifies what to do once the LOOP form is done, usually used to specify a return value.

Name: Anonymous 2013-01-07 8:47

TURBO C QUALITY

Name: Anonymous 2013-01-07 9:20

There's nothing wrong with goto as a tool and it's only the shit-eating fuckstains, lapping up every bit of ethos they can and mindlessly spewing the same fetid, putrid nonsense without actually thinking about it, who actually think a language construct (that compiles to the same machine code as everything else) could ever possibly be a problem. Fuck their shitty fucking shit.

Name: Anonymous 2013-01-07 9:26

>>13
I wish C had JMP

Name: Anonymous 2013-01-07 11:19

>>14
C has setjmp(3) and longjmp(3)

Name: Anonymous 2013-01-07 11:47

doesn't matter, by the time someone gets their thumb outta their ass to implement something we'll all be dead

Name: Anonymous 2013-01-07 12:19

Why aren't you using Lisp, Cudder-sama?

Name: Anonymous 2013-01-07 12:27

>>15

But it behaves  more like delimited continuations.

Name: Anonymous 2013-01-07 12:30

>>15
setcontext / getcontext are more useful and more stable across various platforms.

Name: Anonymous 2013-01-07 12:32

>>17
C is faster.

Name: Anonymous 2013-01-07 20:21

>>19
Excluding Windows, of course.

Name: Anonymous 2013-01-08 4:21

Back to the front page you go!

Name: Anonymous 2013-01-08 5:39

>>4
int *searchuranus(const int *A, size_t n, int v)
{
    while (n--)
        if (*A++ == v)
            return (int *) A - 1;
    return 0;
}

Right in the Cudder.

Name: Cudder !MhMRSATORI!fR8duoqGZdD/iE5 2013-01-08 6:07

>>17
Lisp is like a drug; fun to get high on, not good for actual work.

>>23
I don't want to return. I want to continue immediately after that.

Name: Anonymous 2013-01-08 6:14

>>24
not good for actual work.
I.e. your jewish boss disallows you to use it, slave. Go lick another Jewish asshole, shabbos goy.

Name: Anonymous 2013-01-08 6:16

>>20
Just use Lisp-machine, which has hardware for dynamic type dispatch.

Name: Anonymous 2013-01-08 6:40

>>24
do {
    if (!n--)
        return;
} while (*A++ != v);
A--;
lalalalalalala;

Name: Anonymous 2013-01-08 6:43

>>24
if ((ptr = searchuranus(array, sizeof array / sizeof array[0], 69))) lalalalalalala;

Name: Anonymous 2013-01-08 7:20

>>24
not good for actual work.
What does that mean? It's turing-complete isn't it? It's not easy to compose code to achieve complex behaviour that's easy to maintain?

Name: Anonymous 2013-01-08 7:20

>>24
not good for actual work.
What does that mean? It's turing-complete isn't it? It's not easy to compose code to achieve complex behaviour that's easy to maintain?

Name: Anonymous 2013-01-08 8:35

>>30
Cudder, stop double-posting.

Name: Anonymous 2013-01-08 9:07

Cudder is sitting on a dildo

Name: Anonymous 2013-01-08 10:06

Adding a new control flow construct (especially one with shitty semantics) is way worse than using goto.

Name: Anonymous 2013-01-08 12:40

Someone implement this with a Scheme macro that uses call/cc.

Name: Anonymous 2013-01-08 13:25


require 'continuation'

callcc do |goto|
  for i in 0...5
    goto.call if i == 3
    puts i
  end
end
puts 'jevvs'

Name: Anonymous 2013-01-08 14:47

1-space of indentation

Name: Anonymous 2013-01-08 15:53

MARRY ME CUDDER-KUN!

Name: Anonymous 2013-01-08 15:55

>>37
s/KUN/様/

Name: Anonymous 2013-01-09 1:47

>>24
Lisp is like a drug; fun to get high on, not good for actual work.
While lisp is like a drug, and is fun to get high on, it is also good for actual work. Just look at this thread. The implementation of this new syntax is a trivial exercise in lisp. You wouldn't need to convince anyone or any compiler writers to adopt it, you could just implement it as a macro and include it in your project. And it would be portable too, since the macro would work on any compiler that implements the language. But then other people would need to read your new syntax. List itself isn't comparable to C however. There are too many features. It's too challenging to write an optimizing compiler, and C has advantages in enforcing resource use at the language level. A Lisp dialect that is essentially C in an ast would work, but when you throw static types into lisp the parentheses become a little overwhelming, at least more overwhelming than the semi colons and commas in C. If the macros in C could traverse and manipulate C's ast, then you would have no problems. All that's needed for this is a standard grammar for C compilers to follow and it can be exposed.

I don't want to return. I want to continue immediately after that.
An inline function would do the trick.

Name: Anonymous 2013-01-09 2:59

>>37
Cudder or Culver? Pick your poison.

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