Programming in Go

When I start learning a new programming language I often write the program to play the “Guess the Animal” game to get a feel for the language. Below is a simple version of this game written in Go.  The program starts “knowing” only about two animals, but as you play it it “learns” more, by asking you to provide new animals and new questions.

Here is the code. Enjoy!


package main

import (
	"bufio"
	"fmt"
	"os"
)

type Question struct {
	text      string
	yesAnswer *Question
	noAnswer  *Question
}

func yesOrNo(prompt string) (answer string) {
	fmt.Printf("%s [y/n]? ", prompt)
	fmt.Scanln(&answer)
	return answer
}

func (q *Question) isLeaf() bool {
	return q.yesAnswer == nil && q.noAnswer == nil
}

func (q *Question) ask() (result *Question, answer string) {
	answer = yesOrNo(q.text)
	if answer == "y" {
		result = q.yesAnswer
	} else {
		result = q.noAnswer
	}
	return result, answer
}

var root *Question

func initialize() *Question {
	shark := Question{"shark", nil, nil}
	cat := Question{"cat", nil, nil}
	return &Question{"Is it a mamal", &cat, &shark}
}

func addAnimal(q *Question) {
	var animal, question string
	fmt.Printf("Ok. I give up. What animal is it? ")
	fmt.Scanln(&animal)
	fmt.Printf(
		"Enter a question to tell a difference between %s and %s: ",
		animal, q.text)
	reader := bufio.NewReader(os.Stdin)
	line, _, _ := reader.ReadLine()
	question = string(line)
	answer := yesOrNo(fmt.Sprintf("And for %s the answer is", 
		animal))
	oldAnimal := Question{q.text, nil, nil}
	q.text = question
	newAnimal := Question{animal, nil, nil}
	if answer == "y" {
		q.yesAnswer = &newAnimal
		q.noAnswer = &oldAnimal
	} else {
		q.yesAnswer = &oldAnimal
		q.noAnswer = &newAnimal
	}
}

func play() {
	var answer string
	currentQuestion := root
	for !currentQuestion.isLeaf() {
		currentQuestion, answer = currentQuestion.ask()
	}
	answer = yesOrNo(fmt.Sprintf("Is it a %s", 
		currentQuestion.text))
	if answer == "y" {
		fmt.Printf("OK!!! Got it!!\n")
	} else {
		addAnimal(currentQuestion)
	}
}

func main() {
	root = initialize()
	quit := "n"
	for quit == "n" {
		play()
		quit = yesOrNo("Quit ")
	}
}

Defective God – reflections on two audio books

Recently I started listening to audio books. What I like to do is to listen to books I have already read, as I have discovered that while listening to a book I notice a lot more details. This is because I tend to rush through books, especially when the story is interesting. In addition, and somewhat surprisingly, some scenes stand out much more when they are read aloud.

In the past two months I listened to two books: “Solaris” by Stanislaw Lem and “The Heart of Darkness” by Joseph Conrad. I have read both of these books few times before – in fact I wrote about “Solaris” in this blog.

While listening to “Solaris” I noticed one scene that really stood out when read aloud. It was a conversation between two characters, in which Kelvin – the main character in “Solaris” – asks if there was ever a faith that believed in  “a defective God”. Here is what he said:

“I mean a God whose deficiencies don’t arise from the simple mindedness of his human creators, but constitute his most essential, immanent character. This would be a God limited in his omniscience and omnipotence, one who can mistakes in foreseeing the future of his works, who can find himself horrified by the set of events he has set in motion. This is… a cripple God, who always desires more than he’s able to have, and doesn’t always realize this to begin with.”

This theme occurs in other works by Lem – perhaps most explicitly in a story called “Non Serviam” – but also in “Star Diaries” and related stories.

But surprisingly I found the idea a “defective God” resonating when I listened to “The Heart Of Darkness”, as it applied to the character of Mr. Kurtz. As we find out though Marlowe, the narrtor of the story, Mr. Kurtz was highly educated, cultured and idealistic man – who not only went into Africa to act as an agent bringing ivory from the depths of the Congo, but also to exert a civilized influence on the native population.

However, when he went into the wilderness things went wrong. Kurtz found himself among peoples who took him for a god – who could strike people dead with thunder (his firearm). He was seduced by the power he held over the primitive tribes and proceeded to exploit it – an action at one time he would have considered immoral.

Here is how Marlow described the last moments of Kurtz’s life:

(…) It was as though a veil had been rent. I saw on that ivory face the expression of sombre pride, of ruthless power, of craven terror — of intense and hopeless despair. Did he live his life again in every detail of desire, temptation, and surrender during that supreme moment of complete knowledge? He cried in a whisper at some image, some vision – he cried out twice, a cry that was no more than a breath: “The horror! The horror!”

Kurtz became a defective god, horrified by events he had set in motion.

Ted Nelson’s “Computers for Cynics”

You may not have heard of Ted Nelson, but he is the guy who first imagined hypertext back in the late 1950s.  Recently I stumbled upon a series of short videos called “Computers for Cynics” in which Ted Nelson talks about what is wrong with computers and particularly with user interfaces.

Perhaps the observation I liked most was when he said that the WYSIWIG interface (invented at XEROX PARC and popularized by Apple) was just a way to simulate paper on computers. After all WYSIWIG stands for “What You See Is What You Get” ….. after you print!

Anyway here is a link to these videos on YouTube. Enjoy!

Other Reading in 2013

Here are short summaries of several books I have read in the past year:

  • Do you Believe in Magic? Sense and Nonsense in Alternative Medicine” (Paul A. Offit). Interesting book on the nonsense we find in medicine. The item that got to read this book was a story about Linus Pauling and vitamin C, as it shows that even a brilliant scientist can go off the rails. Pauling’s claims on how high doses of vitamin C cure everything proved to be mostly quackery. The book covers Pauling as well as bunch of other questionable trends of the past few decades.
  • Goodbye Columbus” (Philip Roth). Philip Roth has been recommended to me by number of people, so I decided to give him a try. This was one of his early books, a collection of short stories. The stories are very well written, some are pretty funny and all have a tendency to draw you in the first few paragraphs. Perhaps I will read more of Philip Roth in the future.
  • Speaker For The Dead” (Orson Scott Card). Since I saw the movie “Ender’s Game”, I went back and re-read this book, which is next in the Ender Wiggins sequence. It still is my favorite book of the Ender saga, although reading it this time I found it bit melodramatic. Turned out that the introduction was very interesting, as the author talked about the genesis of the Ender books. He wanted to write “Speaker for the Dead” and he decided to use Ender Wiggin as the main character. But then when he began to introduce the story he wrote  too much material that was irrelevant to “Speaker for the Dead”. All that back story became another book – “Ender’s Game”.
  • Surfaces and Essences” (Douglas Hofstadter and Emanuel Sander). This is a new book by Douglas Hofstadter on the nature of thought. The subtitle is “Analogy as the Fuel and Fire of Thinking”. In the book the authors present a very compelling argument that analogies – seeing one thing like another – is the basic process of human thinking.  Rather than me trying to explain this, you can see a lecture by Hoftstadter outlining the main ideas of the book.  Although enjoyable, at times this book takes too long to get to the point and wastes time with hundreds of examples. I’ve only read about half the book. I hope to finish this book in the future, as the ideas are fascinating.
  • Doorknob Five Two”  (Fredrick Arnold).  This book is a fictionalized account of Fredrick Arnold’s experiences as a P-38 fighter pilot in the Mediterranean during World War. It is clear, that although the author was an superb pilot and an ace, he was a reluctant warrior. He understood too well, that shooting down other aircraft included killing other people. In fact, he kept the fact that he was a combat pilot from his parents, especially his father. His parents only understood his role in the war when he returned home with a uniform marked with ribbons.  “Doorknob Fifty Two” is really well written and was a great read.

> Science Fiction: “Do Androids Dream of Electric Sheep”

Do Androids Dream of Electric Sheep” is now a classic SF novel by Philip K. Dick. The plot revolves around a bounty hunter, named Rick Decard, who’s job is to hunt and kill androids who are illegally staying on Earth. Since androids look like humans, the only way for a bounty hunter to tell a difference is to administer an empathy test. If a person fails the test he(or she) is proven to be an android and is then “retired”.

If all this sounds familiar, it is probably because you have seen the movie “Blade Runner”, which is based on this book. The interesting factoid is that the phrase “Blade Runner” does not appear in Philip K. Dick’s book, but there exists another SF book titled “The Blade Runner”. That “Blade Runner” is about a smuggler of surgical equipment, so the title is apt. I suppose “Blade Runner” is a cooler title for a movie.

In the dystopia of “Do Androids Dream of Electric Sheep”, the world has been devastated by war and only people who are too sick or too poor are left. Very few other animals have survived, so the remaining humans keep cybernetic animals as pets and pretend they are real ones. An actual animal, if one could be found, would be extremely expensive, although owning one would seriously status enhance one’s status.

The test that is supposed to tell human from android tests for empathy. It occurred to me that such a test could perhaps also identify a sociopath (lack of empathy, selfishness etc), so androids in this novel could be identified by sociopathic behavior.

In any case, this was about a third time I had read this book and I still enjoyed it. Since by today’s standards it is not very long, it can serve as good introduction to Philip K. Dick’s works.

>Science Fiction: “Roadside Picnic”

Roadside Picnic” is a book written by the Strugatsky brothers – two Russian writers who worked during the Soviet era. I had wanted to read this book for a long time, and I finally did when a new, uncensored translation was published.

The world of “Roadside Picnic” is a world after a visit by outer space aliens. The aliens stayed for a while, without establishing contact, and then left. They left behind several “Zones”, that are now restricted, in which mysterious items can be found and unexplainable and dangerous phenomena occur. Around the  Zones a new occupation, that of a “stalker”, has arisen. A stalker is a person who goes into the Zones and fetches things for study or for sale.

The main character of the book is “Red” Schruhrat – a stalker.  Stalkers can earn a lot of money, especially when they sneak into the Zones and bring out stuff to sell on the black market.  One of the early missions of Red into the Zone is a trip sanctioned by a local Institute where he is employed. Although the trip seems successful, due to Red’s inattention a scientist touches  a “silver web” and this slip apparently causes his death shortly after the return.

The book covers large portion of Red’s life and further trips into the Zone. Some political and military intrigue is alluded too. However, the Zone remains incomprehensible throughout the  book.

One hypothesis explaining the Zones is presented during a conversation in a bar where a friend of Red  muses that the Zones are simply garbage the aliens left, and are just incomprehensible to us as stuff left in the grass after a roadside picnic would be to ants.

This book was an inspiration for another Tarkowski movie – called “The Stalker”.  I have seen parts of this movie – it is visually stunning – but is extremely slow. I have yet to watch it entirely.

I really enjoyed this book and I will probably read of Strugatsky’s novels.