How to do this in Sepples?
1
Name:
Anonymous
2011-08-14 12:58
i Haskal:
Data.Ix> range ((0,0), (3,4))
[(0,0),(0,1),(0,2),(0,3),(0,4),(1,0),(1,1),(1,2),(1,3),(1,4),(2,0),(2,1),(2,2),(2,3),(2,4),(3,0),(3,1),(3,2),(3,3),(3,4)]
2
Name:
Anonymous
2011-08-14 13:33
I don't know but I just wrote it in ANSI C.
#include <stdio.h>
#include <stdlib.h>
struct num2list {
int car[2];
struct num2list* cdr;
};
/* Warning: no error checking */
struct num2list* range2x2(int x0, int y0, int x1, int y1)
{
const unsigned int incx = x1 > x0 ? -1 : 1;
const unsigned int incy = y1 > y0 ? -1 : 1;
struct num2list* ret = NULL;
while (x1 != x0 + incx)
{
int iy = y1;
while (iy != y0 + incy)
{
/* Invalid C++ code. */
struct num2list* new = malloc(sizeof *new);
new->car[0] = x1;
new->car[1] = iy;
new->cdr = ret;
ret = new;
iy += incy;
}
x1 += incx;
}
return ret;
}
void print_num2list(struct num2list* x)
{
struct num2list* i;
putchar('(');
for (i = x; i; i = i->cdr)
{
printf("(%d, %d), ", i->car[0], i->car[1]);
}
puts("\b\b)");
}
void free_num2list(struct num2list* x)
{
struct num2list* i;
struct num2list* in;
for (i = x; i; in = i->cdr, free(i), i = in);
}
main()
{
struct num2list* foo = range2x2(0, 0, 3, 4);
print_num2list(foo);
free_num2list(foo);
return 0;
}
3
Name:
2
2011-08-14 13:38
I'm sorry. incx and incy should be signed .
Accept my apologies and have a free age .
4
Name:
Anonymous
2011-08-14 13:40
>>2
That's kind of long, I was thinking there'd be some kind of two-line solution. Also it should work on any number of dimensions or data types.
5
Name:
Anonymous
2011-08-14 13:52
It's called a ``for loop.''
6
Name:
Anonymous
2011-08-14 13:55
What exactly is this operation called in combinatorics terms?
>>6
iT'S CALLED INTERSECTION OF THE EQUIANGULAR QUADRILATERAL SPAWNED BY SOME VECTOR WITH Z^2 AND IT HAS NOT TO DO WITH COMBINATORICS.
8
Name:
Anonymous
2011-08-14 14:14
>>5
Doesn't work:
struct a { int b, c; };
for (a d(0,0); d < e(2,3); ++d) {
}
9
Name:
TRUE
2011-08-14 14:16
>>7
mY ASSISTANT HAS JUST INFORMED ME THAT A EQUIANGULAR QUADRILATERAL IS ALSO CALLED A RECTANGLE. i GUESS YOU GUYS MIGHT PREFER THAT TERM.
10
Name:
Anonymous
2011-08-14 15:38
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char **argv)
{
int first[2], last[2];
int i, j;
if (argc < 5) goto error;
first[0] = atoi(argv[1]);
first[1] = atoi(argv[2]);
last[0] = atoi(argv[3]);
last[1] = atoi(argv[4]);
if (first[0] > last[0]) goto error;
if (first[1] > last[1]) goto error;
goto run;
error:
printf("%s: Error\n", argv[0]);
return EXIT_FAILURE;
run:
printf("[");
for (i = first[0]; i <= last[0]; i++) {
for (j = first[1]; j <= last[1]; j++) {
printf("(%i,%i)%s", i, j, i < last[0] || j < last[1] ? "," : "");
}
}
printf("]\n");
return EXIT_SUCCESS;
}
Remember the C paradigm: if you need to malloc something print it to output instead.
11
Name:
Anonymous
2011-08-14 16:50
// faggot_iteration.cpp
#include <cstdio>
int main() {
std::printf("[");
for (int i = 0; i <= 3; ++i)
for (int j = 0; j <= 4; ++j)
std::printf("(%d,%d)%c", i, j, (i * j) == 12 ? ']' : ',');
std::printf("\n");
return 0;
}
$ g++ -O2 -s -o faggot_iteration faggot_iteration.cpp
$ ./faggot_iteration
[(0,0),(0,1),(0,2),(0,3),(0,4),(1,0),(1,1),(1,2),(1,3),(1,4),(2,0),(2,1),(2,2),(2,3),(2,4),(3,0),(3,1),(3,2),(3,3),(3,4)]
12
Name:
Anonymous
2011-08-14 16:57
>>10
Sorry,
>>11- san here, I wrote up my response without first reading yours. My apologies. However, mine is more terse and does exactly what the OP's does, plus it's written in Sepples, at the cost of not being as flexible.
13
Name:
Anonymous
2011-08-15 0:17
idjits
#include <string>
std::string A(int x1, int y1, int x2, int y2)
{
std::string res = "[";
char asdf[100];
for (int x = x1; x <= x2; x++) for (int y = y1; y <= y2; y++) {
sprintf(asdf, "(%d, %d),", x, y);
res += asdf;
}
return res.substr(0, res.size()-1)+"]";
}
14
Name:
Anonymous
2011-08-15 0:25
>>13
Enjoy your useless memory allocations on the heap.
15
Name:
Anonymous
2011-08-15 0:48
>>14
implying heap allocations are slower than stack allocations
16
Name:
Anonymous
2011-08-15 1:33
Shut up SHUT UP SHUT UP!! ALL OF YOU JUST SHUT THE FUCK UP
17
Name:
Anonymous
2011-08-15 2:06
>>16
I will when you learn how to sage properly.
18
Name:
Anonymous
2011-08-15 2:44