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

How to do this in Sepples?

Name: Anonymous 2011-08-14 12:58

iHaskal:
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)]

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;
}

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