(archive 'newLISPer)

November 24, 2006

Let's play

Filed under: newLISP — newlisper @ 17:58

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?
(1 3 2 4 5 6 7 8 9 10)
Reverse how many?
(2 3 1 4 5 6 7 8 9 10)
Reverse how many?
(3 2 1 4 5 6 7 8 9 10)
Reverse how many?
(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.


November 15, 2006

What's new?

Filed under: newLISP — newlisper @ 23:34

Things are a bit quiet around here at the moment. But I can at least mention a few newLISP-y things, to pass the time until I have something interesting to post…!

  • newLISP on Noodles is a new wiki dedicated to newLISP advocacy. There are quizzes, too, which is cool… :-)
  • visit Fanda’s web site at Intricate Visions for some photography and programming
  • Seth Dillingham created a BBEdit Codeless Language Module for newLISP. You’ll need version 8.5 of BBEdit to use it. He wrote an article about it at the Truer Words site, and you might be interested in his use of recursive back-reference grep patterns to match Lisp parentheses, even if you don’t use BBEdit…
  • If you can understand Russian, you’ll be interested in a new tutorial to newLISP to be found at Dmitry’s EnFeauTec site.
  • There’s lots of good newLISP code at terpri.
  • Both Intricate Visions and Terpri are wikis that use the newLISP wiki code, which you can get from the main newLISP site. One day I hope this blog will move to a newLISP-powered wiki.

I suppose I’d better post a bit of newLISP code. I had to produce some 2007 calendars the other day, and I used the following code to test some of the basic output routines. Here’s a stand-alone version, which needs a PostScript interpreter of some kind to produce output.

(set 'buffer "")
(define (ps str)
	(write-line str buffer))
(define (render)
	  (& 0x7F (sys-info 7)) ; MacOS X...
		  (write-file (string (env "HOME") "noname.ps")
				(string "%!PS-Adobe-3.1" "\n" "%%Creator: newLISP" "\n")
					buffer "showpage" "\r\n")
	  	(exec (string "open "(env "HOME") "/Desktop/noname.ps")))
	  ; other platforms
	  (write-file "noname.ps"
	  	(string "%!PS-Adobe-3.1" "\n" "%%Creator: newLISP" "\n")
	  		buffer "showpage" "\r\n")))
(define (write-string s)
  (ps (format {%f %f moveto} x y))
  (ps (format {(%s) show} s))) ; x and y are unchanged
(define (weekday-format)
  (ps (format {/Eurostile findfont 8 scalefont setfont}))
  (ps (format {0 0 0 setrgbcolor})))
(define (weekend-format)
  (ps (format {/Eurostile findfont 8 scalefont setfont}))
  (ps (format {0 .3 .3 setrgbcolor})))
(define (month-format)
  (ps (format {/Eurostile findfont 12 scalefont setfont}))
  (ps (format {.7 0 0 setrgbcolor})))
(define (next-day)
  (if weekend
  (write-string day-number-text)
  (dec 'y row-height))
(define (new-week)
  (set 'y (- ruler month-day-space))
  (inc 'x column-width))
(define (new-month)
  (and (> x 450)
		(set 'x left-margin)
		(dec 'ruler month-month-space)
		(dec 'y month-month-space))
		(swap y ruler)
		(inc 'x month-width-space)
		(write-string month-name)
		(swap y ruler)
		(ps (format {%f %f moveto} x y))
; start
(set 'x 10
	'y 750
  'ruler y
  'row-height 15
  'left-margin 25
  'column-width 15
  'month-day-space 15
  'month-month-space 160
  'month-width-space 30)
(ps (format
	{0.1 setgray /Eurostile findfont 8 scalefont setfont}))
(ps (format
	{%f %f moveto} x y))
(for (week 0 52)
  (for (day 0 6)
    (set 'date-val
    	(+ (date-value 2007 1 1)
    		 (* 24 60 60 (+ (+ (* 7 week) day)))))
    (set 'day-number (int (date date-val 0 {%d}) 0 10))
    (set 'month-name (date date-val 0 {%B}))
    (set 'day-name (date date-val 0 {%A}))
    (if (starts-with day-name "S") ; weekend, English :-)
      (set 'weekend true)
      (set 'weekend nil))
    (if (= day-number 1) ; start of new month
    (set 'day-number-text
    	(string (date date-val 0 {%d})))


November 1, 2006

1 + 2 = 3

Filed under: newLISP — newlisper @ 17:11

One of the good things about the Ruby programming language is how much activity the community generates for promoting and exploring the language. One of the cool things they get up to is the Ruby Quiz. This is a weekly problem-solving ‘contest’: a problem is set on Friday and Ruby-ists submit their solutions before the following Thursday, when the results are discussed on the list. No winners, just solutions varying in length, speed, and elegance.

I believe that there are similar rolling quizzes for other languages, including Perl and Haskell. Unfortunately I don’t think that there are enough newLISP users out there (yet) for something similar, and it would also need an administrator with a good knowledge of the language, a spare web site, a mailing list, and quite a lot of time as well…

I bumped into one of their problems the other day, while looking for something else. How to print out one of these, Pascal’s triangle:

                         1     1
                      1     2     1
                   1     3     3     1
                1     4     6     4     1
             1     5    10    10     5     1
          1     6    15    20    15     6     1
       1     7    21    35    35    21     7     1
    1     8    28    56    70    56    28     8     1
 1     9    36    84    126   126   84    36     9     1

You might enjoy reading the Ruby solutions here.

I would have tried to produce a newLISP solution myself. But before I could muster up the necessary brain power to make an attempt, I discovered that Peter had got there before me. And his solution looks like it would be hard to improve.

Blog at WordPress.com.