Updated: 12/1/02; 12:31:07 AM.
Rough Days for a Gentil Knight
The Radio weblog of Oblivious Allan Baruz.
“He was a verray parfit gentil knight.” —Chaucer
        

Saturday 2 November 2002

I tried my hand at coding the Luhn algorithm in Smalltalk, as almost every other language seems to have had a shot at it. I am certain that are better implementations than this quick hack, but here it is. It works true for the example case that the accordian guy uses, but having no other test cases, this is what I have. Note that this is in the Squeak dialect, and it attaches to a base class.

Integer>>isValidLuhn

	| str accOdd accEven anEven |
	accOdd _ accEven _ 0.
	str _ ReadStream on: self asString reverse.
	[str atEnd]
		whileFalse: [accOdd _ accOdd + str next asString asNumber.
			anEven _ str next.
			anEven
				ifNotNil: [anEven _ anEven asString asNumber * 2.
					anEven > 9
						ifTrue: [anEven _ anEven - 9].
					accEven _ accEven + anEven]].
	^ accOdd + accEven \ 10 = 0

[update: Whoops. I was using a stripped image (the image I use for my Comanche Swiki), so all the variable names were lost, so I replaced them. The underscore is the keyboard key used for the assignment operator, which in Squeak images renders as a left-pointing arrow. The caret sign is an up arrow and returns from the function.

update: There must be a better implementation, probably using #inject:into:. The implementation above streams through the digits of the integer, accumulating the odd and even digits alternately.

update: I don’t know why the algorithm description insists on adding the digits of the doubled even-positioned digits; it seems to me that the digit-added property holds true even if you add the doubled digits together after, instead of at each step. That is, 35 + 11 and 8 + 2 both ultimately equal one. I don’t remember what this property is called. If that is the case, the implementation above could be simplified to something like, um, oh, wait, never mind. Sequence of actions is important. Never mind.

update: I originally pasted in the file-in version, but I thought if I presented as it is presented in Kent Beck, it would be less scary. Smalltalk really is a pretty language.

update: Whoops, more Smalltalk implementations just popped into my head: here and here.]
10:06:50 PM    comment []


An oldie, but Paul Graham writes for the ages.

Good design...

  • ...is simple
  • ...is timeless
  • ...solves the right problem
  • ...is suggestive
  • ...is often slightly funny
  • ...is hard
  • ...looks easy
  • ...uses symmetry
  • ...resembles nature
  • ...is redesign
  • ...can copy
  • ...is often strange
  • ...happens in chunks
  • ...is often daring

9:21:47 PM    comment []
categories: Hostage to Crap

C. Wright Mills, in one (or more?) of his works on sociology, lists (listed if you consider the man in time and place, dead alas of a motorbike incident, though we are speaking of books, or more properly, writing of books, or rather a book, which is speaking or ought to speak to us from infinite time, but of course I digress) three means or routes to power—coercion, authority, and manipulation.

Coercion and manipulation—force and fraud—are the basis of comedy or so I read somewhere, perhaps Maurice Charney? Or not. Anyway, if that is the case, then perhaps the exploration of authority and how characters relate to it is the basis of tragedy?

Quick examples: Hamlet—yes; the Œdipus plays, yes; Romeo and Juliet—yes. Any counterexamples?

Hm. Something to think on.

Although, come to think of it, there is proper, condign, acknowledged power—authority—in comedy, as well. Midsummer Night’s Dream. But in such plays as those proper authority is subverted by force or fraud: say, by pixie dust in the eyes and a merry Robin Goodfellow. Twelfth Night has an authoritative power who is brought low by the power of love; its exercise never comes up. The hijinx occur due to fraud (cross-dressing, ghostwritten letters) and force (bullying and mockery).

Tragedy seems to depend on some defiance or loss of authority. One thinks of one of the progenitors of the form, Prometheus Bound. In Romeo and Juliet the two warring families defy the Prince. Or perhaps tragedy originates in the violation of authority, where something is wrong with the natural order, and people try to put it right: Hamlet. Or there is a conflict between the exercise of authoritative power and the natural order: Elektra.
9:20:49 PM    comment []


© Copyright 2002 Richard Allan Baruz.
This is a personal weblog; that is, it is in no way affiliated nor connected with the company for which I work, nor the clients to whom I am contracted.
 
November 2002
Sun Mon Tue Wed Thu Fri Sat
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
Oct   Dec

Previous/Next


All Your Link Are Belong To Us!
If you came by way of a search engine and did not find exactly what you were looking for, try the

People who may think me ungrateful rather than incompetent
Smart people I ought to read more
Those who have cared to comment
Well-connected
Can’t help myself
Unfiled for now

Self-linked... creepy, or crappy?

Click here to visit the Radio UserLand website.

Subscribe to "Rough Days for a Gentil Knight" in Radio UserLand.

Click to see the XML version of this web page.

Click here to send an email to the editor of this weblog.