Sunday, December 26, 2004

Continuing my foray into Lisp, I thought I'd implement the Wondrousness Test for a number. Here's what I ended up with:


(defun is-wondrous (n)
(do ((val n))
((eq val 1) t)
(if (evenp val)
(setf val (/ val 2))
(setf val (+ (* val 3) 1)))
(print val)))

Here's the (almost) equivalent code in Smalltalk:


WondrousPropertyTester class>>hasWondrousProperty: aNumber
|temp|
Transcript clear.
temp := aNumber.

Transcript print: temp; cr.

[temp > 1] whileTrue:
[
temp := (temp even)
ifTrue: [temp / 2]
ifFalse:[3 * temp + 1].
Transcript print: temp; cr.
].
[Transcript show: 'Yes'; cr]


Chalk one up for Lisp.

Update: Smalltalk code pruned to bring it closer to the Lisp code's behaviour (I had written the Smalltalk version earlier with checks to stop after a prescribed number of iterations; there was some leftover code from this).