Archive for the ‘Programming’ Category

Common Lisp heresy: syntactic lambdas

Saturday, May 3rd, 2008
(defun bracket-reader (stream char)
  (declare (ignore char))
  (let* ((lst (read-delimited-list #] stream t))
         (pos (position '|| lst)))
    (if pos
        `#'(lambda ,(mapcar #'intern (subseq lst 0 pos))
             ,@(nthcdr (1+ pos) lst))
        `#'(lambda (_) ,lst))))

(set-macro-character #[ #'bracket-reader)
(set-macro-character #] (get-macro-character #)))

Closures are beautiful, but the heaviness of CL’s lambda syntax kept jumping out at me as fairly ugly after a few months of writing Smalltalk. The above snippet allows you to write things like:

(mapcar [+ _ 1] '(1 2 3))

and:

(maphash [k v || (print v)] tbl)

Which, to my eyes at least, is nicer than:

(mapcar #'(lambda (x) (+ x 1)) '(1 2 3))

and:

(maphash #'(lambda (k v) (print v)) tbl)

Of course, to add syntax to Lisp is to wade into failure-littered territory. But although no-one agrees how it should be done, I really don’t think it’s a bad idea.

Lisp Machines

Sunday, April 27th, 2008

The Lisp Machines were easily the best environments ever created for developing in Lisp, but the companies that produced them died out quite a while ago (Dan Weinreb has described some of the reasons for this). They weren’t just great tools for writing Lisp—Genera is one of the best environments for writing code that’s been created for any language. Squeak is the only thing I’ve seen that comes close.

Although the current legal status of the IP around the Lisp Machines is unclear, you can now get your hands on the source, and run the environment pretty easily on a 64-bit Linux installation (or even OS X + VMWare + Linux)—credit for the Linux port goes to Brad Parker.

Setting it up isn’t exactly a point-and-click operation, and guides are non-existent (I think)—so here’s what I did to get it up and running on OS X using VMWare and Ubuntu 7.10 x86-64 (this may clobber other things; I’m assuming a dedicated Ubuntu installation):

curl -o og.torrent http://torrents.thepiratebay.org/3769989/Symbolics_Open_Genera_2.0_for_Alpha_-_complete_package_with_Lisp.3769989.TPB.torrent
rtorrent og.torrent
tar xfj opengenera2.tar.bz2

curl -O http://www.unlambda.com/download/genera/snap4.tar.gz
tar xfz snap4.tar.gz

apt-get install xorg nfs-common nfs-user-server inetutils-inetd

cat <<EOF > /etc/inetd.conf
daytime stream tcp nowait root internal
daytime dgram udp wait root internal
time stream tcp nowait root internal
time dgram udp wait root internal
EOF

/etc/init.d/inetutils-inetd restart

hostname genera-host

cat <<EOF > /etc/hosts
10.0.0.2 genera
10.0.0.1 genera-host
EOF

echo “/ genera(rw,no_root_squash)” > /etc/exports
/etc/init.d/nfs-user-server restart

cd snap4

cat <<EOF > .VLM
genera.network: 10.0.0.2; mask=255.255.255.0; gateway=10.0.0.1
genera.virtualMemory: 2048
genera.world: Genera-8-5.vlod
genera.debugger: VLM_debugger
genera.coldLoad.geometry: 800×600
EOF

SDIR=/var/lib/symbolics
mkdir $SDIR
# you could just link below, but I want keep a clean copy somewhere
cp -R ../og2/sys.sct $SDIR
mkdir $SDIR/rel-8-5
ln -s $SDIR/sys.sct $SDIR/rel-8-5/sys.sct

./genera

Once you’re in the Genera environment, evaluate the following:

Define Site foo
Namespace server name: genera
Unix Host Name: genera-host
Login
Login: Lisp-Machine

To test things out, you can evaluate something like Edit Definition read-from-string, which should pull the source for read-from-string from the Linux host over NFS. If you edit the definition, load it with M-x Compile Changed Definitions. If it all works, congratulations; you now have a fully-functioning Genera 8.5 environment.

Genera itself is excellently documented (just fire up the Document Examiner), but the emulator has very sparse docs. I’ll post some more soon with further details about day-to-day use—keybindings, etc.

Land of Lisp

Tuesday, April 1st, 2008

Conrad Barski just posted a preview of his new book, Land of Lisp. It is distilled awesomeness. His other comics, like Casting SPELs in Lisp, are also great. Having him on board as an author looks like a coup for No Starch Press.

(Via Lemonodor)

GPRS in Ireland with Vodafone and the iPhone

Friday, March 14th, 2008

I recently switched from Meteor to Vodafone when in Ireland because Vodafone’s data rate (50MB for €1) is 3,000 times cheaper than Meteor’s (6c/KB).

Vodafone don’t have an EDGE network, but they do support GPRS—which works with the iPhone, despite appearances (GPRS coverage is indicated by an odd-looking blank square beside the carrier logo).

To get it to work, I edited my SystemPreferences plist file (/var/preferences/SystemConfiguration/preferences.plist) to contain the Vodafone APN details and a pointer to a PAC file describing the Vodafone proxy server (here’s the relevant section), and then created a simple proxy.pac in /var/root.

The Vodafone proxy doesn’t seem to allow much bar web traffic, but it’s enough for basic use on the move.

TeTeX on Leopard

Thursday, March 13th, 2008

TeTeX isn’t maintained any more, but I’ve written a fair few scripts over the years that depend on it, and so I’ve yet to switch over to something like MacTeX or TeX Live.

TeTeX 3.0 doesn’t build out-of-the-box on Leopard. In case it’s useful to anyone, here is the patch I hacked together to get it to do so.

Unicode and Arc

Thursday, February 7th, 2008

When Arc version 0 was released last week, people quickly seized upon Paul’s comment that he hadn’t spent any effort on supporting Unicode as some sort of heresy. The highest-rated comment in the Reddit thread declared Arc “instantly obsolete”.

Inevitably, few noticed that Arc actually supports Unicode pretty well. Arc’s primitive character type works with any Unicode codepoint; built-in functions like len work correctly when applied to strings with multi-byte characters; and Arc has no problem with Unicode-containing identifiers in source files:

arc> (def héλλô () (prn "héλλô world"))
#<procedure: héλλô>
arc> (héλλô)
héλλô world
"héλλô world"
arc>

Let’s see how other (presumably non-obsolete?) languages like Python and Ruby fare:

$ cat > foo.py
def ô():
	return "ô"
$ python foo.py
  File "foo.py", line 1
SyntaxError: Non-ASCII character 'xc3' in file foo.py on line 1, but no
encoding declared; see http://www.python.org/peps/pep-0263.html for
details
$ cat > foo.rb
def ô
	"ô"
end
$ ruby foo.rb
foo.rb:1: Invalid char `303' in expression
foo.rb:1: Invalid char `264' in expression
foo.rb:2: syntax error, unexpected tSTRING_BEG, expecting 'n' or ';'
	return "ô"

The programming world (and the Lisp community especially) is no stranger to excellent flamewars, but the debate surrounding Arc’s “lack” of Unicode support ranks right up with the very best.

Footnote: Due to Arc’s Unicode support, it was fairly easy to add Unicode support to Hacker News. I submitted a patch to do so back a few weeks ago, and it went live today.

Update (7/2): Markus points out in the comments that my example works fine in Ruby 1.9, and he’s right. At time of writing, though, the latest stable version of Ruby is 1.8.6-p111, so I think the original point still stands.

Google Code project for Wikipedia iPhone

Thursday, February 7th, 2008

Now that people are making improvements to the code for the Wikipedia iPhone app, I’ve gone ahead and set up a Google Code project to centralise things. Future updates and development will be announced there. I’ve so far found Google Code very straightforward and flexible, especially when compared to the nightmare that is SourceForge. If you’d like to follow progress and/or receive updates on new releases, you can subscribe to the fledgling mailing list.

Minesweeper is NP-complete!

Thursday, November 15th, 2007

Richard Kaye’s work on Minesweeper is a lot of fun to read. He first proved that Minesweeper is NP-complete (though the article in The Mathematical Intelligencer isn’t available online, unfortunately), and has a very neat paper that demonstrates some logic circuits in the game (the AND gate is truly impressive). I suspect that his latest paper, “Infinite versions of minesweeper are Turing complete“, is even more interesting, but I don’t know enough about the area to properly appreciate it.

Stock exchange in Erlang

Saturday, November 10th, 2007

Joel Reymont has a kinda unusual programming background, having written big programs in Erlang, Common Lisp, Haskell, and OCaml. As you might expect, he has plenty of interesting things to say about them. (John Wiseman has some more info including quotes whose original sources seem to have disappeared.)

Joel wrote recently that he’s working with Pragmatic Programmers on a successor to Programming Erlang, and today announced that, as part of it, he’s going to build a stock exchange app with Erlang, Mnesia and EC2. Joel has a lot of experience here, and it sounds like it could be a great project. I’m really looking forward to seeing how it goes.

Update (30/1): Looks like it’s no more.

Tracking time

Friday, November 9th, 2007

A few weeks ago, I realised that I didn’t really have much idea of where the time I spent on my computer actually went, so I wrote something to track it. I just came across RescueTime, which seems to set out to solve this problem, but hasn’t launched yet. While I’m sure they’ve done a good job of things, I don’t really like the idea of using something that phones home here (though I can see why they’d love to have the data), and I’d also like to be able to analyze the data in different ways (with a spreadsheet, some custom script, Dabble, or whatever), rather relying on whatever they build.

So if anyone’s interested, I’ve uploaded the code I use. It’s very simple-minded—to track idleness, it waits for the screensaver process to start, so you probably want to set a low timeout; doesn’t daemonize; when storing URLs, it assumes you’re using Safari; and is also OS X-only. But maybe somebody can build something more interesting on top of it.

A README is still to come, but to get started, use something like:

$ sh time/time-log.sh &!