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

Lisp and Scheme have helped my C programming

Name: Anonymous 2008-04-23 22:58

Yesterday I wrote a silly program that randomly places queens onto a chess board until every space either has a queen or is blocked by a queen. The "hardest" part was writing the code to mark the appropriate spaces as blocked by the placement of a new queen.

I started by writing code like this.

if(r >= 0)
    for(i = r - NUM_COLS; i >= 0; i -= NUM_COLS)
        mark(board, i, BLOCK);


But after doing this for the first couple types of blocks, I realized that I could factor a pattern out of it.


if(blocks[j].pre(r, r) == blocks[j].expected_pre)
    for(i = r + blocks[j].change;
        blocks[j].during(i, r) == blocks[j].expected_during;
        i += blocks[j].change)
            mark(board, i, BLOCK);


Then I could just write an array of structures that hold information on the prerequisites for each type of block, the loop checking function, and the loop change offset, like this.


Block blocks[] = {
    {isUnderTopEdge,    true,  isUnderTopEdge,    true,  -NUM_COLS},
    {isAboveBottomEdge, true,  isAboveBottomEdge, true,  NUM_COLS},
    {isLeftEdge,        false, isRightEdge,       false, -1},
    {isRightEdge,       false, isLeftEdge,        false, 1},
    {isLeftEdge,        false, isLeftOf,          true,  -(NUM_COLS+1)},
    {isRightEdge,       false, isRightOf,         true,  -(NUM_COLS-1)},
    {isLeftEdge,        false, isLeftOf,          true,  (NUM_COLS-1)},
    {isRightEdge,       false, isRightOf,         true,  (NUM_COLS+1)},
    {0},
};


That made it much easier to write the last types of blocks (i.e. left, right, and diagonal).

Of course, I learned a lot about factoring out patterns by learning Lisp and Scheme, specifically reading and watching SICP.

Name: Anonymous 2008-04-23 23:03

wtf

Name: Anonymous 2008-04-23 23:09

yo... nigga... ur for loop needs sum {} luv..

Name: Anonymous 2008-04-23 23:14

Have you heard of the n-queens problem? (Is it in SICP?)

Name: Anonymous 2008-04-23 23:21

>>3
No it doesn't. There's only one statement in the loop, so brackets would just be syntactic salt.

Name: Anonymous 2008-04-23 23:32

>>1
Lisp and Scheme have helped my C programming
Of course, lisp shall be learned even if one does not intend to use it.
>>3
No, it doesn't.
>>4
I don't think OP lives under a god damn rock.
>>5
They'd be reduntant, but not syntactic salt or sugar. Learn what syntactic sugar is.

Name: Anonymous 2008-04-23 23:52

>>6
I know what syntactic sugar is. I know that technically the brackets wouldn't be syntactic salt, but to me they would be because they would be useless, and would actually make the code look worse to me.

And, actually, I'd never heard of the n-queens problem, but my program doesn't really do that. I've used my program to show that an arrangement of 5 queens is enough to block the entire board, though :\

Name: Anonymous 2008-04-24 0:06

>>6
Syntactic sugar is a term coined by Peter J. Landin for additions to the syntax of a computer language that do not affect its functionality but make it ``sweeter'' for humans to use.1

1http://en.wikipedia.org/wiki/Syntactic_sugar

Name: Anonymous 2008-04-24 0:11

Superfluous use of syntax.

Name: Anonymous 2008-04-24 1:01

Guys, we have been trolled.

Name: Anonymous 2008-04-24 2:53

>>7
Why have you picked salt as the villian? People love salt.

Name: Anonymous 2008-04-24 5:20

>>7
That's not a rigorous proof though.

Name: Anonymous 2008-04-24 5:21

>>11
Yeah, as the opposite, I prefer ``syntactic shit''.

Name: Anonymous 2008-04-24 14:42

yo... nigga... ur for loop needs sum {} luv..

Name: Anonymous 2008-04-24 14:50

>>13
What do diabetics call syntactic sugar?

Name: Anonymous 2008-04-24 19:53

>>12
True, but here's a layout of queens that protect every other space.


---------------------------------
| * | * | * | * | * | * | * | * |
---------------------------------
| * | Q | * | * | * | * | * | * |
---------------------------------
| * | * | * | * | * | Q | * | * |
---------------------------------
| * | * | * | * | * | * | * | * |
---------------------------------
| * | * | * | * | * | * | * | * |
---------------------------------
| Q | * | * | * | * | * | * | * |
---------------------------------
| * | * | * | * | Q | * | * | * |
---------------------------------
| * | * | * | * | * | * | Q | * |
---------------------------------

Name: Anonymous 2008-04-24 19:55

Name: Anonymous 2008-04-24 20:31

>>16
My god, it's full of stars!

Name: Anonymous 2008-04-24 20:41

>>15
Syntactic insulin

Name: Anonymous 2008-04-24 20:52

>>15
My dog has no pancreas.
How does he secretive syntactic digestive enzymes?
Terrible!

Name: Anonymous 2008-04-25 3:43

>>7
I've used my program to show that an arrangement of 5 queens is enough to block the entire board
Not in every case, of course.

Name: Anonymous 2008-04-25 3:47

>>21
Yeah true, only when the moon is full.

Name: Anonymous 2008-04-25 21:24

Reading SICP is serious business

Name: Bucket !!PhiVV3U2X7TT1Xm 2008-04-25 21:25

Sicp is a french football player. Renowned for his solo work in business. I am being serious idiot.

Name: Anonymous 2008-04-26 4:25

/prog/ is not your /blog/

Name: Anonymous 2008-04-26 8:06

>>25
    AHAHAHAAA YOU THINK YOUR CLEAVER DON"T YOU
BECAUSE YOU HAVE A RYMING DICTIONARY?
      WELL I HAVE A SUPRISE FOR YOU
       YOUR NOT
                IM A GODDAMN   EXPERT PROGRAMMER
        I WAS HAXING ANII WHEN YOU WERE STILL IN PRESCHOOL
 I HAVE A DATABASE OF RHYMING WORDS THATS SEVEN FUQIN GIGABYTES
WHAT DO YOU THINK ABOUT THAT HUH !!

Name: Anonymous 2008-04-26 8:44

>>26
What about my cleaver? Want me to hit you in the head with it?

Name: Anonymous 2008-04-26 9:20

>>26
Watch this become a forced meme. Just watch

Name: Anonymous 2008-04-26 9:58

>>28
What's destroying /prog/ is that every forced meme succeeds.

Name: GJSussman !xpQSO2ECEY 2008-04-26 10:36

>>29
This may surprise you, but this unscientific way of thinking is destroying /prog/ just as I have predicted

Name: Anonymous 2011-02-03 0:13

<

Name: Anonymous 2011-02-18 14:05

<-- check 'em dubz

Name: Sgt.Kabukiman䖉 2012-05-22 23:20

All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy

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