(archive 'newLISPer)

November 24, 2006

Let's play

Filed under: newLISP — newlisper @ 17:58
Tags:

I chanced upon this little diversion today, and I thought it was quite cute.

The game of REVERSE requires you to arrange a list of numbers in numerical order from left to right. To move, you tell the computer how many numbers (counting from the left) to reverse. For example, if the current list is 2 3 4 5 1 6 7 8 9 and you reverse 4, the result will be 5 4 3 2 1 6 7 8 9. Now if you reverse 5, you win.

It looks a bit like this when played in the terminal:

...
(4 2 3 1 5 6 7 8 9 10)
Reverse how many?
4
(1 3 2 4 5 6 7 8 9 10)
Reverse how many?
3
(2 3 1 4 5 6 7 8 9 10)
Reverse how many?
2
(3 2 1 4 5 6 7 8 9 10)
Reverse how many?
3
(1 2 3 4 5 6 7 8 9 10)
Done! That took you 19 steps!

Well, before you read further, you can wander off and write a newLISP version of your own.

Don’t worry, I’ll wait here until you get back.

No, really – I’m happy to wait. I’ll surf the web a bit, and polish up the keys of my keyboard (using a bottle of Reverse Polish, since newLISP now courses through my veins).

Ah, have you finished? Good.

Now you can compare your exquisitely-fashioned jewel of a program with my version:

(define (rev-slice n lst)
    (append (reverse (0 n lst)) (n lst)))
(set 'steps 0 'goal (sequence 1 10) 'state (randomize goal))
(until (= goal state)
    (println state "\nReverse how many?")
    (inc 'steps)
    (set 'state (rev-slice (int (read-line)) state)))
(string "\\nDone! That took you " steps " steps!")

Well, it’s nice and short, but it will insist on printing out the list as a list, and the ending could do with a bit of work. So more of a roughly-cut diamond, perhaps.

That’s about it, really, but if you wander over to Paul Bissex ‘s site, you’ll be able to see what it looks like in different languages. Good fun, and fascinating to see what’s hard and what’s easy.

Advertisements

3 Comments »

  1. >Have you tried a negative number already in your reversing? should it be that way?

    Comment by newdep — December 8, 2006 @ 08:20 | Reply

  2. >I needed some time to figure out what it does but its quit a nice little game ;-)

    Comment by newdep — December 8, 2006 @ 08:23 | Reply

  3. >no i didn’t try a negative number. After looking at the other versions, I noticed that everyone was worried about number of lines/characters, and didn’t put much error checking in. so there’s plenty of things that could be changed!

    Comment by newlisper — December 8, 2006 @ 09:16 | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: