2
0
Inspired by Sygmei's question, I'm interested in finding out what languages can "do this properly".
Your task is to write a "for loop" function / macro / instruction. The arguments should be:
- an initializer
- a test
- an increment
- a body
All of these are "pieces of code" and should be able to access variables in the context of the call. The body should be able to include multiple statements.
The aim is to have the fewest lexical tokens excluding comments, except that #define
and friends (using a dumb preprocessor) are worth 50, and eval
and friends (taking a string argument) are worth 100. Feel free to make your code readable with comments, whitespace and meaningful names.
You should include a "test harness" which is not counted towards length.
The C version definitely won't win:
// the do {...} while (0) idiom enables a code block to be a single C statement, so it can safely be followed by a semicolon
#define my_for(init, test, inc, body) do { \
init; \
while (test) { \
body; \
inc; \
} \
} while (0)
// test harness:
#include <stdio.h>
main()
{
int i, total;
my_for((i=1, total=0), i<=3, ++i, {printf("%d\n", i); total=total+i;});
printf("total: %d\n", total);
}
Edit to add: the meaning of "lexical token" depends on your language. Here's how the scoring breaks down for my C version:
//
[...]: 0
#define
: 1 occurrence, 50 points
my_for
: 1
(
: 3
init
: 2
,
: 3
test
: 2
inc
: 2
body
: 2
)
: 3
do
: 1
{
: 2
\
: 6
;
: 3
while
: 2
}
: 2
0
: 1
TOTAL: 85
3Unclear to me: scoring. For example how would your c reference be scored? – Digital Trauma – 2017-01-27T04:42:56.023
I was actually going to do this challenge, but I ran into a fair number of difficulties in laying it out. I hope someone posts a well explained version; preferably normal code golf. – Carcigenicate – 2017-01-27T16:18:28.540
@DigitalTrauma does my edit clarify it sufficiently? If so how can I get the question re-opened? I can't tag all the on-hold-voters in this comment – Hugh Allen – 2017-01-28T02:40:36.563
The C scoring only makes it clear for C programs. It's very hard to define "tokens" for a wide range of languages. You either have to give up on the idea, or only allow a narrow range of languages (which is a whole different can of worms). – Mego – 2017-01-28T04:29:02.537
>
for
loop syntax in the implementation?@Mego: Can you give an example where it is be hard to identify tokens? If it's just edge cases, can't they be resolved by asking the task author on a case by case basis? – smls – 2017-01-28T22:32:45.640
@smls Asking the OP for every unclear case isn't reasonable. The rules need to be static and objective, not constantly being added to because edge cases come up that require the OP's discretion. – Mego – 2017-01-28T22:34:01.643
Name suggestion: My little For loop (sorry, mlp reference kek) – Matthew Roh – 2017-01-29T00:19:59.373
@smls 1. an anonymous function is fine. 2. reusing the builtin for-loop syntax is fine; reusing the builtin for-loop implementation is cheating. – Hugh Allen – 2017-01-29T02:04:12.433
I'm sorry, but this is still very confusing to me; in particular, it seems like we need judges to tell when something in a language counts as an eval or "dumb macro"; as in your comment in the TCL answer (which by no means is apparent to me reading your description here), or possibly how to judge the use of FORTH words. Heck, I'm not even sure whether a traditional C preprocessor metaprogramming EVAL counts as a macro, or an eval, or both, or neither if using CPP as the language (despite your use of CPP in C as an example of dumb macros). – H Walters – 2017-01-29T02:50:13.343
@HWalters re TCL answer:
uplevel
is likeeval
because it takes a string argument, and contains an interpreter for the language. Re CPP: I'm penalising C-style macros partly so we don't have to worry about it (and partly because they suck) – Hugh Allen – 2017-01-29T04:09:01.803@HWalters just read it as "don't use these" – Hugh Allen – 2017-01-29T04:44:17.837
Okay, no chat, so cleaning up clutter, but I'll leave it at this. (1) I really think you need to define "dumb preprocessor", and clearly state whether this involves the same language level parsing or not. (2) You also need to specify what counts as "eval" here; everything is a string until parsed, so this only becomes conflated with interpreted languages. (3) You might have to worry about CPP; I can pretty confidently say it won't win, but unless you forbid it it's not up to you whether there's a submission in it. Hacking in CPP is actually a personal hobby of mine. – H Walters – 2017-01-29T05:28:22.820