471,071 Members | 1,188 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,071 software developers and data experts.

Program without conditional Statements - Possible ?

hie..

Do any one knows how to write a C program without using the
conditional statements if, for, while, do, switch, goto and even
condotional statements ? It would be a great help for me if someone
helps me...

Urgent - Please reply soon !

Thanks, Raghu
Nov 14 '05 #1
92 9518
ra*********@vit.ac.in (Raghavendra R A V, CSS India) writes:
Do any one knows how to write a C program without using the
conditional statements if, for, while, do, switch, goto and even
condotional statements ? It would be a great help for me if someone
helps me...


int main (void) { return 0; }

Martin
Nov 14 '05 #2
Raghavendra R A V, CSS India <ra*********@vit.ac.in> scribbled the following:
hie.. Do any one knows how to write a C program without using the
conditional statements if, for, while, do, switch, goto and even
condotional statements ? It would be a great help for me if someone
helps me... Urgent - Please reply soon ! Thanks, Raghu


Yes, I know how to do this. It's surprisingly easy. Here's one such
program:

/* CODE STARTS HERE */
int main(void) {
return 0;
}
/* CODE ENDS HERE */

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"The day Microsoft makes something that doesn't suck is probably the day they
start making vacuum cleaners."
- Ernst Jan Plugge
Nov 14 '05 #3
Raghavendra R A V, CSS India wrote:
Do any one knows how to write a C program without using the
conditional statements if, for, while, do, switch, goto and even
condotional statements ? It would be a great help for me if someone
helps me...


void main() { puts("hello world"); }

Any questions?

The point is that yes it's possible, but you have to write it yourself
because only you know what the program is supposed to do...

Maybe you're asking "how do I make a decision in a program without actually
making a decision"... But if that's what you're asking for, then ask...

--
gabriel
Nov 14 '05 #4
Raghavendra R A V, CSS India wrote:
Do any one knows how to write a C program without using the
conditional statements if, for, while, do, switch, goto and even
condotional statements ?
Use an array of function pointers. For example, here's a program
(with no "conditional" statements) that accepts a number of integers
as command line arguments and prints their sum.

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

long ifthenelse(int testval,
long (*true)(char **, long),
long (*false)(char **, long),
char **p,
long c) {
long (*next[2])(char **, long);
next[0] = false;
next[1] = true;
return next[!!testval](p, c);
}

long end(char **p, long c) { return c; }

long acc(char **p, long c) {
return ifthenelse(p[1] != 0,
acc,
end,
p + 1,
c + strtol(*p, NULL, 0));
}

int main(int argc, char **argv) {
printf("Total: %ld\n", ifthenelse(argc > 1, acc, end, argv, 0));
return 0;
}
It would be a great help for me if someone helps me...

Urgent - Please reply soon !


You might want to look into upgrading your C compiler as well.

Jeremy.
Nov 14 '05 #5
> > Do any one knows how to write a C program without using the
conditional statements if, for, while, do, switch, goto and even
condotional statements ? It would be a great help for me if someone
helps me...


int main (void) { return 0; }


On a more serious note:

/* Set up some condition: */
int cond_a = (some_function_call() > 0) % 2;
int cond_b = (some_other_function() > 0) % 2;
/* Note: you can remove '> 0' and '% 2' if you make sure that the return
value of the functions is either 0 or 1 only */
/* Boolean logic: */
int cond_and = cond_a * cond_b;
int cond_or = (cond_a + cond_b) % 2;
/* Conditional assignment: */
int Value = value_true * (cond_and) + value_false * (1 - cond_and);

This takes care of conditional expressions.

You can use recursion to simulate loops.

--
Regards,
Andras Tantos
<http://andras.tantos.homedns.org>
Nov 14 '05 #6
Raghavendra R A V, CSS India wrote:
Does any one know how to write a C program
without using the conditional statements
if, for, while, do, switch, goto and even conditional statements?
Yes.
It would be a great help for me if someone helps me...


Do you have any experience with an *applicative*
(sometimes called *functional*) computer programming language
such as the LISt Processing (LISP) computer programming language?

The idea is to write programs containing *only* constant
(because C supports numerous different types) and function definitions.
Functions return values of expressions:

int min(int i, int j) {
return (i < j)? i: j;
}

which can be used to define constants:

const int k = min(i, j);

or used in expressions to define other functions.
You define and apply recursive functions
instead of iteration in loops.

Nov 14 '05 #7
Jeremy Yallop wrote:
printf("Total: %ld\n", ifthenelse(argc > 1, acc, end, argv, 0));


Bah, you just disguised an if-then-else statement as a function call, no?

--
gabriel
Nov 14 '05 #8
gabriel wrote:
Jeremy Yallop wrote:
printf("Total: %ld\n", ifthenelse(argc > 1, acc, end, argv, 0));


Bah, you just disguised an if-then-else statement as a function call, no?


No.
Nov 14 '05 #9
gabriel <no@no--spam.com> spoke thus:
void main() { puts("hello world"); }

^^^^

I do hope that was for effect.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #10
Raghavendra R A V, CSS India wrote:
condotional statements ? It would be a great help for me if someone
helps me...


Just out of curiosity... How did you come up with this question? School
assignment, or just something you thought up, perhaps?

It seems like sending someone on a quest to find a really complicated way
of solving a simple problem...

--
gabriel
Nov 14 '05 #11
Christopher Benson-Manica wrote:
void main() { puts("hello world"); }

^^^^
I do hope that was for effect.


Yes, the question (to me) was not worth the effort of answering it
"properly."

--
gabriel
Nov 14 '05 #12
gabriel wrote:
void main() { puts("hello world"); }

Any questions?


Just one. How did you manage to get a one-line C program *wrong*???

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #13
Richard Heathfield wrote:
gabriel wrote:

void main() { puts("hello world"); }

Any questions?

Just one. How did you manage to get a one-line C program *wrong*???


That's easy. /Any/ one-line C program is wrong by the standards of c.l.c
if it is to have a discernable side-effect.

Best regards, Sidney

Nov 14 '05 #14
Sidney Cadot wrote:
Richard Heathfield wrote:

Just one. How did you manage to get a one-line C program *wrong*???

That's easy. /Any/ one-line C program is wrong by the standards of c.l.c
if it is to have a discernable side-effect.


....Unless of course you count something like

int main(){for(;;)}

which could be argued to be correct, and have a side effect.

-- Sidney

Nov 14 '05 #15
ra*********@vit.ac.in (Raghavendra R A V, CSS India) wrote:
# hie..
#
# Do any one knows how to write a C program without using the
# conditional statements if, for, while, do, switch, goto and even
# condotional statements ? It would be a great help for me if someone
# helps me...

It should be coverred in the part of your text devoted lambda notation or
combinator. The notation in C is clumsy, since you have to recreate lambdas
and continuations. The basic trick is to recognise
if (p) q; else r;
can be rewritten
If = \p,q,r. (Lambda choice[2] = {\.q, \.r}, choice[p()]())

While requires recursion and so you need to embed the environment.
while (p) q;
then becomes
While = \p,q. (If(p,\.While(p,q),Skip))

With those two out of the way,
for (p; q; r) s
For = \p,q,r,s. (p,While(q,\.(s,r)))
do p while (q)
Do = \p,q. (p, While(q,p))
switch (p) {case q: r; break; case t: u; break;...}
Switch = \p,q,r,s. (If(\.p==q,\.r,s))
Switch(p,q,\.r,\.Switch(p,t,\.u,\. ...))

For gotos and labels, there's an old article on converting
Algol 60 to ISWIM.

--
Derk Gwen http://derkgwen.250free.com/html/index.html
I love the smell of commerce in the morning.
Nov 14 '05 #16
"Raghavendra R A V, CSS India" wrote:

hie..

Do any one knows how to write a C program without using the
conditional statements if, for, while, do, switch, goto and even
condotional statements ? It would be a great help for me if someone
helps me...

Urgent - Please reply soon !

Thanks, Raghu


He may want to do logical arithmetic with bitwise logical operators.
For example,

int my_max1( int a, int b)
{
int c;
c = (a<b);
return a * (!c) + b * (c);
}

int my_max2( int a, int b)
{
int c;
c = (a<b);
return (a & (-(!c))) + (b & (-c)); // this supposes -1 has all bits set!
}

The C convention, that "true" is 1, is very inconcenient for this.

--
Julian V. Noble
Professor Emeritus of Physics
jv*@lessspamformother.virginia.edu
^^^^^^^^^^^^^^^^^^
http://galileo.phys.virginia.edu/~jvn/

"God is not willing to do everything, and thereby take away our free wiil
and that share of glory that rightfully belongs to ourselves."
-- N. Machiavelli, "The Prince".
Nov 14 '05 #17
Sidney Cadot wrote:
Sidney Cadot wrote:
Richard Heathfield wrote:
Just one. How did you manage to get a one-line C program *wrong*???

That's easy. /Any/ one-line C program is wrong by the standards of c.l.c
if it is to have a discernable side-effect.


...Unless of course you count something like

int main(){for(;;)}

which could be argued to be correct,


Er, no. :-)

$ cat foo.c
int main(){for(;;)}
$ make
gcc -g -W -Wall -ansi -pedantic -o foo foo.c
foo.c: In function `main':
foo.c:1: parse error before `}'
foo.c:1: warning: control reaches end of non-void function
make: *** [foo] Error 1
and have a side effect.


A C90 solution is easy enough.

int main(){return 0*puts("Hello world");}

For C99, the line is beginning to stretch a little:

int puts(const char *);int main(){return 0*puts("Hello world");}

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #18
Richard Heathfield wrote:
void main() { puts("hello world"); }

Just one. How did you manage to get a one-line C program *wrong*???


I was trying to make a point, not please the c-std religious feelings of
others here (feel free to "correct" it if you want). Anyway, I'm putting
much more thought on this than on writing the one-liner program, so that
gives you an idea...

--
gabriel
Nov 14 '05 #19
Richard Heathfield wrote:
Sidney Cadot wrote:

Sidney Cadot wrote:

Richard Heathfield wrote:
Just one. How did you manage to get a one-line C program *wrong*???
That's easy. /Any/ one-line C program is wrong by the standards of c.l.c
if it is to have a discernable side-effect.


...Unless of course you count something like

int main(){for(;;)}

which could be argued to be correct,

Er, no. :-)

$ cat foo.c
int main(){for(;;)}
$ make
gcc -g -W -Wall -ansi -pedantic -o foo foo.c
foo.c: In function `main':
foo.c:1: parse error before `}'
foo.c:1: warning: control reaches end of non-void function
make: *** [foo] Error 1


Well I'll be <insert expletive here> with a pitchfork and a pair of
sheep. Grumble.
and have a side effect.

A C90 solution is easy enough.

int main(){return 0*puts("Hello world");}


I would think this program actually /is/ wrong "by the standards of c.l.c".
For C99, the line is beginning to stretch a little:

int puts(const char *);int main(){return 0*puts("Hello world");}


Hmmm. I guess I failed to stipulate that providing your own prototypes
doesn't count.

"Providing your own prototypes doesn't count."

....So there :-p

Best regards, Sidney

Nov 14 '05 #20
gabriel wrote:
Richard Heathfield wrote:
How did you manage to get a one-line C program *wrong*???

int main(int argc, char* argv[]) { puts("hello world\n"); }
I was trying to make a point,
not please the c-std religious feelings of others here
(feel free to "correct" it if you want).
Anyway, I'm putting much more thought on this
than on writing the one-liner program
so that gives you an idea...


Please just ignore Richard Heathfield.

Nov 14 '05 #21
"Sidney Cadot" <si****@jigsaw.nl> wrote:
Sidney Cadot wrote:
[...] /Any/ one-line C program is wrong by the standards of c.l.c
if it is to have a discernable side-effect.


...Unless of course you count something like

int main(){for(;;)}

which could be argued to be correct, and have a side effect.


It does? Pardon my ignorance, where?
Nov 14 '05 #22
"E. Robert Tisdale" <E.**************@jpl.nasa.gov> wrote:
Richard Heathfield wrote:
How did you manage to get a one-line C program *wrong*???

int main(int argc, char* argv[]) { puts("hello world\n"); }


Err, no Mister. It is _still_ wrong.
Please just ignore Richard Heathfield.


I think it would be a great relief to everyone if _you_ did, Mr E.
Nov 14 '05 #23
"Andras Tantos" <an***********@yahoo.com> wrote in message
news:bv************@ID-201774.news.uni-berlin.de...
/* Set up some condition: */
int cond_a = (some_function_call() > 0) % 2;
int cond_b = (some_other_function() > 0) % 2;
/* Note: you can remove '> 0' and '% 2' if you make sure that the return
value of the functions is either 0 or 1 only */
Seriously, why do you need % 2 anyway? And I would go for == 0, not > 0.
/* Boolean logic: */
int cond_and = cond_a * cond_b;
int cond_or = (cond_a + cond_b) % 2;
/* Conditional assignment: */
int Value = value_true * (cond_and) + value_false * (1 - cond_and);

This takes care of conditional expressions.
And things like

if (x)
foo();
else
bar();

could be written as ((x) && foo()) || (bar()), given some restrictions on
foo() and bar(). Ugly.
You can use recursion to simulate loops.

--
Regards,
Andras Tantos
<http://andras.tantos.homedns.org>

Nov 14 '05 #24
Peter Pichler wrote:
"Sidney Cadot" <si****@jigsaw.nl> wrote:
Sidney Cadot wrote:

[...] /Any/ one-line C program is wrong by the standards of c.l.c
if it is to have a discernable side-effect.


...Unless of course you count something like

int main(){for(;;)}

which could be argued to be correct, and have a side effect.

It does? Pardon my ignorance, where?


Its "side effect" (after adding a semicolon to get it to compile) is to
take up resources. If you disagree, you would have a point.

However, I feel that it takes a weird definition of "side effect" indeed
to define away what happens if you execute 10,000 simultaneous instances
of this program on your computer.
Best regards,

Sideny

Nov 14 '05 #25
nrk
Peter Pichler wrote:
"Andras Tantos" <an***********@yahoo.com> wrote in message
news:bv************@ID-201774.news.uni-berlin.de...
/* Set up some condition: */
int cond_a = (some_function_call() > 0) % 2;
int cond_b = (some_other_function() > 0) % 2;
/* Note: you can remove '> 0' and '% 2' if you make sure that the return
value of the functions is either 0 or 1 only */


Seriously, why do you need % 2 anyway? And I would go for == 0, not > 0.
/* Boolean logic: */
int cond_and = cond_a * cond_b;
int cond_or = (cond_a + cond_b) % 2;
/* Conditional assignment: */
int Value = value_true * (cond_and) + value_false * (1 - cond_and);

This takes care of conditional expressions.


And things like

if (x)
foo();
else
bar();

could be written as ((x) && foo()) || (bar()), given some restrictions on
foo() and bar(). Ugly.


((x) && (foo(), 1)) || (bar(), 1);

Look mama, creative abuse of the comma operator :-)

-nrk.
You can use recursion to simulate loops.

--
Regards,
Andras Tantos
<http://andras.tantos.homedns.org>


--
Remove devnull for email
Nov 14 '05 #26
gabriel <no@no--spam.com> writes:
Christopher Benson-Manica wrote:
void main() { puts("hello world"); }

^^^^
I do hope that was for effect.


Yes, the question (to me) was not worth the effort of answering it
"properly."


So typing "void main" requires a greater effort than typing "int main"?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 14 '05 #27
E. Robert Tisdale wrote:
Functions return values of expressions:

int min(int i, int j) {
return (i < j)? i: j;
}


not a conditional?? how come?
--
#include <stdio.h>
#define p(s) printf(#s" endian")
int main(void){int v=1;*(char*)&v?p(Little):p(Big);return 0;}

Giannis Papadopoulos
http://dop.users.uth.gr/
University of Thessaly
Computer & Communications Engineering dept.
Nov 14 '05 #28
Peter Pichler <pi*****@pobox.sk> spoke thus:
Please just ignore Richard Heathfield.
I think it would be a great relief to everyone if _you_ did, Mr E.


Shouldn't that be "Mr. T"? Maybe if he said "I pity the foo!" when he
attacks luminaries such as Mr. Heathfield people would listen :)

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #29
On Tue, 03 Feb 2004 12:48:54 -0800, "E. Robert Tisdale"
<E.**************@jpl.nasa.gov> wrote in comp.lang.c:
Raghavendra R A V, CSS India wrote:
Does any one know how to write a C program
without using the conditional statements
if, for, while, do, switch, goto and even conditional statements?


Yes.
It would be a great help for me if someone helps me...


Do you have any experience with an *applicative*
(sometimes called *functional*) computer programming language
such as the LISt Processing (LISP) computer programming language?

The idea is to write programs containing *only* constant
(because C supports numerous different types) and function definitions.
Functions return values of expressions:

int min(int i, int j) {
return (i < j)? i: j;
}

which can be used to define constants:

const int k = min(i, j);


Why bring up this rubbish here? While the function definition might
be perfectly legal C, the definition of k most certainly is not.

What sort-of-C-like-but-not-really-C language are you thinking of? On
second thought, please don't tell us.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #30
Sidney Cadot wrote:
Peter Pichler wrote:
"Sidney Cadot" <si****@jigsaw.nl> wrote:
Sidney Cadot wrote:

[...] /Any/ one-line C program is wrong by the standards
of c.l.c if it is to have a discernable side-effect.

...Unless of course you count something like

int main(){for(;;)}

which could be argued to be correct, and have a side
effect.


It does? Pardon my ignorance, where?


Its "side effect" (after adding a semicolon to get it to
compile) is to take up resources. If you disagree, you would
have a point.

However, I feel that it takes a weird definition of "side
effect" indeed to define away what happens if you execute
10,000 simultaneous instances of this program on your
computer.


'S not bad; but I like better:

int main(void){return main();}

--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c
Read my lips: The apple doesn't fall far from the tree.

Nov 14 '05 #31
On Tue, 3 Feb 2004 22:25:46 +0000 (UTC)
Richard Heathfield <do******@address.co.uk.invalid> wrote:
Sidney Cadot wrote:
Sidney Cadot wrote:
Richard Heathfield wrote:

Just one. How did you manage to get a one-line C program *wrong*???
That's easy. /Any/ one-line C program is wrong by the standards of c.l.c
if it is to have a discernable side-effect.


...Unless of course you count something like

int main(){for(;;)}

which could be argued to be correct,


Er, no. :-)

$ cat foo.c
int main(){for(;;)}
$ make
gcc -g -W -Wall -ansi -pedantic -o foo foo.c
foo.c: In function `main':
foo.c:1: parse error before `}'
foo.c:1: warning: control reaches end of non-void function
make: *** [foo] Error 1


I believe that the missing return is OK for C89, that the warning
can be ignored, and that the -W is "aggressive", if you will.

--
donLouis
Nov 14 '05 #32
donLouis wrote:
On Tue, 3 Feb 2004 22:25:46 +0000 (UTC)
Richard Heathfield <do******@address.co.uk.invalid> wrote:
Sidney Cadot wrote:
> Sidney Cadot wrote:
>
>> Richard Heathfield wrote:
>
>>> Just one. How did you manage to get a one-line C program *wrong*???
>>
>>
>> That's easy. /Any/ one-line C program is wrong by the standards of
>> c.l.c if it is to have a discernable side-effect.
>
> ...Unless of course you count something like
>
> int main(){for(;;)}
>
> which could be argued to be correct,
Er, no. :-)

$ cat foo.c
int main(){for(;;)}
$ make
gcc -g -W -Wall -ansi -pedantic -o foo foo.c
foo.c: In function `main':
foo.c:1: parse error before `}'
foo.c:1: warning: control reaches end of non-void function
make: *** [foo] Error 1


I believe that the missing return is OK for C89,


It isn't.
that the warning
can be ignored,
It can't.
and that the -W is "aggressive", if you will.


I won't. The diagnostic is required because there is an error in the syntax
of the program: specifically, the for-statement syntax is:

for(expression-opt; expression-opt; expression-opt) statement

The statement part is not optional and must not be omitted. To omit it is a
syntax error, and a conforming implementation must generate at least one
diagnostic for a program containing a syntax error.

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #33
donLouis wrote:
which could be argued to be correct,


Er, no. :-)

$ cat foo.c
int main(){for(;;)}
$ make
gcc -g -W -Wall -ansi -pedantic -o foo foo.c
foo.c: In function `main':
foo.c:1: parse error before `}'
foo.c:1: warning: control reaches end of non-void function
make: *** [foo] Error 1

I believe that the missing return is OK for C89, that the warning
can be ignored, and that the -W is "aggressive", if you will.


The problem is that the for statement isn't properly terminated using a
semicolon, I'm afraid.

Best regards,

Sidney

Nov 14 '05 #34
Christopher Benson-Manica <at***@nospam.cyberspace.org> scribbled the following:
Peter Pichler <pi*****@pobox.sk> spoke thus:
Please just ignore Richard Heathfield.
I think it would be a great relief to everyone if _you_ did, Mr E.
Shouldn't that be "Mr. T"? Maybe if he said "I pity the foo!" when he
attacks luminaries such as Mr. Heathfield people would listen :)


This is something that I've been often wondering. Why is it "the foo"
instead of "the fool"? Any relation to the ubiquitous metasyntactic
variable "foo"?

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Bad things only happen to scoundrels."
- Moominmamma
Nov 14 '05 #35
Richard Heathfield wrote:
gabriel wrote:
void main() { puts("hello world"); }

Any questions?


Just one. How did you manage to get a one-line C program *wrong*???


Pure talent. You have to be born with it.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #36
Anyway, Tcl is written is C, so this is input to a C program, rather than
the C program itself.

package require derkgwen
namespace import derk::lambda

proc SKIP {} {
}

proc JOIN {a b} {
eval $a
eval $b
}

proc IF {p a b} {
eval [lindex[list $b $a] [eval $p]]
}
IF [lambda {} {expr 1}] [lambda {} {puts true}] [lambda {} {puts false}]
IF [lambda {} {expr 0}] [lambda {} {puts true}] [lambda {} {puts false}]

proc WHILE {p a} {
IF $p[list JOIN $a[list WHILE $p $a]] SKIP
}
set x 1; WHILE [lambda {} {expr $::x<=10}] [lambda {} {puts $::x; incr ::x}]

proc DO {a p} {
JOIN $a[list WHILE $p $a]
}
set x 1; DO [lambda {} {puts $::x; incr ::x}] [lambda {} {expr $::x<=10}]
set x 11; DO [lambda {} {puts $::x; incr ::x}] [lambda {} {expr $::x<=10}]

proc FOR {p q r s} {
JOIN $p[list WHILE $q[list JOIN $s $r]]
}
FOR \
[lambda {} {set ::x 1}] \
[lambda {} {expr $::x<=10}] \
[lambda {} {incr ::x}] \
[lambda {} {puts $::x}]

proc SWITCH {p args} {
SWITCHTAIL [eval $p] $args
}
proc SWITCHTAIL {p s} {
IF \
[lambda {s} {expr [llength $s]==0} $s] \
SKIP \[list IF \
[lambda {s} {expr [llength $s]==1} $s] \
[lindex $s 0] \
[lambda {p q r s} {
IF [lambda {p q} {expr {$p==$q}} $p $q] \
$r \[list SWITCHTAIL $p $s]
} $p [lindex $s 0] [lindex $s 1] [lrange $s 2 end]]
]
}
SWITCH [lambda {} {expr 3}] \
1 [lambda {} {puts one}] \
2 [lambda {} {puts two}] \
3 [lambda {} {puts three}] \
4 [lambda {} {puts four}] \
5 [lambda {} {puts five}]

proc BLOCK {block} {
BLOCKTAIL 1 SKIP @ $block
}
proc BLOCKTAIL {K code curr block} {
IF [lambda {block} {expr [llength $block]>0} $block] \
[lambda {K code curr statement block} {
SWITCH [lambda {statement} {lindex $statement 0} $statement] \
goto \[list JOIN \[list proc $curr {}[list JOIN $code [lindex $statement 1]]] \[list BLOCKTAIL $K SKIP % [lrange $block 1 end]] \
] \
gotoif \[list JOIN \[list proc $curr {}[list JOIN $code \[list IF [lindex $statement 1] [lindex $statement 2] %$K] \
]] \[list BLOCKTAIL [expr $K+1] SKIP %$K [lrange $block 1 end]] \
] \
label \[list JOIN \[list proc $curr {}[list JOIN $code [lindex $statement 1]]] \[list BLOCKTAIL $K SKIP [lindex $statement 1] [lrange $block 1 end]] \
] \[list BLOCKTAIL $K[list JOIN $code $statement] $curr $block]
} $K $code $curr [lindex $block 0] [lrange $block 1 end]] \
@
}
BLOCK[list \
[lambda {} {puts top}] \
[lambda {} {set ::x 1}] \
{goto inloop} \
{label loop} \[list gotoif [lambda {} {expr {$::x>10}}] bottom] \
{label inloop} \
[lambda {} {puts $::x}] \
[lambda {} {incr ::x}] \
{goto loop} \
{label bottom} \
[lambda {} {puts bottom}] \
]
--
Derk Gwen http://derkgwen.250free.com/html/index.html
Wow. A sailboat.
Nov 14 '05 #37
nrk wrote:
Peter Pichler wrote:

"Andras Tantos" <an***********@yahoo.com> wrote in message
news:bv************@ID-201774.news.uni-berlin.de...
/* Set up some condition: */
int cond_a = (some_function_call() > 0) % 2;
int cond_b = (some_other_function() > 0) % 2;
/* Note: you can remove '> 0' and '% 2' if you make sure that the return
value of the functions is either 0 or 1 only */


Seriously, why do you need % 2 anyway? And I would go for == 0, not > 0.

/* Boolean logic: */
int cond_and = cond_a * cond_b;
int cond_or = (cond_a + cond_b) % 2;
/* Conditional assignment: */
int Value = value_true * (cond_and) + value_false * (1 - cond_and);

This takes care of conditional expressions.


And things like

if (x)
foo();
else
bar();

could be written as ((x) && foo()) || (bar()), given some restrictions on
foo() and bar(). Ugly.

((x) && (foo(), 1)) || (bar(), 1);

Look mama, creative abuse of the comma operator :-)

-nrk.

You can use recursion to simulate loops.

--
Regards,
Andras Tantos
<http://andras.tantos.homedns.org>

((x) && ({foo();}) || ({bar();});

uglier

--
#include <stdio.h>
#define p(s) printf(#s" endian")
int main(void){int v=1;*(char*)&v?p(Little):p(Big);return 0;}

Giannis Papadopoulos
http://dop.users.uth.gr/
University of Thessaly
Computer & Communications Engineering dept.
Nov 14 '05 #38
On Tue, 3 Feb 2004, Sidney Cadot wrote:
A C90 solution is easy enough.

int main(){return 0*puts("Hello world");}


I would think this program actually /is/ wrong "by the standards of c.l.c".


Which one of them?-)

Nov 14 '05 #39
Keith Thompson wrote:
gabriel <no@no--spam.com> writes:
Christopher Benson-Manica wrote:

> void main() { puts("hello world"); }
^^^^
I do hope that was for effect.


Yes, the question (to me) was not worth the effort of answering
it "properly."


So typing "void main" requires a greater effort than typing "int main"?


Maybe he was attempting to avoid typing "return 0;"

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #40
Morris Dovey wrote:
Sidney Cadot wrote:
Peter Pichler wrote:
"Sidney Cadot" <si****@jigsaw.nl> wrote:
Sidney Cadot wrote:

> [...] /Any/ one-line C program is wrong by the standards
> of c.l.c if it is to have a discernable side-effect.

...Unless of course you count something like

int main(){for(;;)}

which could be argued to be correct, and have a side
effect.

It does? Pardon my ignorance, where?


Its "side effect" (after adding a semicolon to get it to
compile) is to take up resources. If you disagree, you would
have a point.

However, I feel that it takes a weird definition of "side
effect" indeed to define away what happens if you execute
10,000 simultaneous instances of this program on your
computer.


'S not bad; but I like better:

int main(void){return main();}


That will halt with a fault on any implementation known to me.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #41
In <bv**********@news.tudelft.nl> Sidney Cadot <si****@jigsaw.nl> writes:
Richard Heathfield wrote:
A C90 solution is easy enough.

int main(){return 0*puts("Hello world");}


I would think this program actually /is/ wrong "by the standards of c.l.c".


???
For C99, the line is beginning to stretch a little:

int puts(const char *);int main(){return 0*puts("Hello world");}


Hmmm. I guess I failed to stipulate that providing your own prototypes
doesn't count.

"Providing your own prototypes doesn't count."

...So there :-p


So there:

int puts(); int main(){ return 0 * puts("Hello world"); }

No prototypes at all ;-)

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #42
In <bv**********@titan.btinternet.com> Richard Heathfield <do******@address.co.uk.invalid> writes:
donLouis wrote:
I believe that the missing return is OK for C89,


It isn't.


Chapter and verse, please.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #43
CBFalconer wrote:
Morris Dovey wrote:
int main(void){return main();}


That will halt with a fault on any implementation known to me.


With optimization (-O2 or above) turned on GCC optimizes away the tail
call, making it equivalent to an infinite busy loop.

Jeremy
Nov 14 '05 #44
CBFalconer wrote:
Morris Dovey wrote:

'S not bad; but I like better:

int main(void){return main();}


That will halt with a fault on any implementation known to me.


Lucky you! I still have a couple that'll just "go west". (:

--
Morris Dovey
West Des Moines, Iowa USA
C links at http://www.iedu.com/c
Read my lips: The apple doesn't fall far from the tree.

Nov 14 '05 #45
On Wed, 4 Feb 2004 07:11:09 +0000 (UTC)
Richard Heathfield <do******@address.co.uk.invalid> wrote:
donLouis wrote:
On Tue, 3 Feb 2004 22:25:46 +0000 (UTC)
Richard Heathfield <do******@address.co.uk.invalid> wrote:
Sidney Cadot wrote:

> Sidney Cadot wrote:
>
>> Richard Heathfield wrote:
>
>>> Just one. How did you manage to get a one-line C program *wrong*???
>>
>>
>> That's easy. /Any/ one-line C program is wrong by the standards of
>> c.l.c if it is to have a discernable side-effect.
>
> ...Unless of course you count something like
>
> int main(){for(;;)}
>
> which could be argued to be correct,

Er, no. :-)

$ cat foo.c
int main(){for(;;)}
$ make
gcc -g -W -Wall -ansi -pedantic -o foo foo.c
foo.c: In function `main':
foo.c:1: parse error before `}'
foo.c:1: warning: control reaches end of non-void function
make: *** [foo] Error 1


I believe that the missing return is OK for C89,


It isn't.
that the warning
can be ignored,


It can't.
and that the -W is "aggressive", if you will.


I won't. The diagnostic is required because there is an error in the syntax
of the program: specifically, the for-statement syntax is:

for(expression-opt; expression-opt; expression-opt) statement

The statement part is not optional and must not be omitted. To omit it is a
syntax error, and a conforming implementation must generate at least one
diagnostic for a program containing a syntax error.


Oops, missed the terminating semi-colon on the for. I use -W all
the time myself, but I don't think it is required to generate
a syntax error on the for statement. Anyway, I was (not) thinking
of -Werror...

Question; does adding the semi-colon disqualify the program as a
"one liner"?

donLouis
Nov 14 '05 #46
On Wed, 04 Feb 2004 08:23:30 +0100
Sidney Cadot <si****@jigsaw.nl> wrote:
donLouis wrote:
which could be argued to be correct,

Er, no. :-)

$ cat foo.c
int main(){for(;;)}
$ make
gcc -g -W -Wall -ansi -pedantic -o foo foo.c
foo.c: In function `main':
foo.c:1: parse error before `}'
foo.c:1: warning: control reaches end of non-void function
make: *** [foo] Error 1

I believe that the missing return is OK for C89, that the warning
can be ignored, and that the -W is "aggressive", if you will.


The problem is that the for statement isn't properly terminated using a
semicolon, I'm afraid.


Yes, I see that I missed that not-so-subtle point. Oh well, more
coffee and cigarettes should fix that.

--
donLouis
Nov 14 '05 #47
Jeremy Yallop wrote:
CBFalconer wrote:
Morris Dovey wrote:

int main(void){return main();}


That will halt with a fault on any implementation known to me.


With optimization (-O2 or above) turned on GCC optimizes away the
tail call, making it equivalent to an infinite busy loop.


and so it does! Thus disproving the old adage that optimization
can introduce bugs. This one removes one. Now if we can persuade
ERT here, and EGN on comp.programming, to run it on start-up, the
world would become a more peaceful place.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 14 '05 #48
Joona I Palaste <pa*****@cc.helsinki.fi> spoke thus:

<offtopic degree="severe">
This is something that I've been often wondering. Why is it "the foo"
instead of "the fool"? Any relation to the ubiquitous metasyntactic
variable "foo"?


Short answer: because that's how Mr. T always said it.

Long answer: Mr. T is an old 70's television character. He was black,
proud of it, and drove a mean van. He was both a bastion of wholesome
living (I think he drove his van around on crusades for the local
YMCA, or something) and an icon of flavalicious style. One of his
characteristic expressions was "I pity the foo!" - the 'l' was omitted
since it made the expression sound so much cooler. More than you
wanted to know, I'm sure.

</offtopic>

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #49
"donLouis" <pa*******@earthlink.net> wrote:
> > Sidney Cadot wrote:
> >
> > int main(){for(;;)}


Question; does adding the semi-colon disqualify the program as a
"one liner"?


Doesn't it depend on *where* you put the semicolon? ;-)
Nov 14 '05 #50

This discussion thread is closed

Replies have been disabled for this discussion.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.