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 ")
	}
}

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!

>Science Fiction: “Manna: Two Visions of Humanity’s Future”

Recently I have read several articles on robots and machines that are taking more and more jobs humans are doing today. For example Wired had a long article on future of robots. Then there is Baxter the friendly robot that can be easily trained for simple manufacturing tasks.  Thinking of taking robotics to the extreme, I have been wondering what the nature of work and society will be in the future,  a future in which machines can make everything. This is how I stumbled on this book.

“Manna” is a short novel that describes two possible worlds that can result when robots take over making everything. The book is interesting, mostly because of the descriptions of two possible versions of a future world. It is borderline SF story, with some political overtones.

In the first world, people’s work is controlled by software – that’s right, management is replaced by a program called Manna. In the story, everything starts when fast food restaurants begin use Manna  to control everything the workers are doing. People are guided via voice commands spoken into the headphones they wear while working. Since this software improves profitability of business so much that it becomes widely adopted.

Wide adoption of Manna eliminates management jobs first. But  eventually, even the low skill jobs are taken over by machines still guided by Manna. Consequently, more and more people cannot find any work. Such people are kept in “welfare towns”, where they can live out their live with basic necessities provided by the government, but they can never leave.

The other world is referred to as the “Australia Project”. This one is more utopian. Here the labor of robots is equally shared by all citizens. Since robots can manufacture everything the only real cost to anything is the energy required to make something, so every citizen of the Australia Project is granted an allowance  units of energy which covers his or her needs. This system makes sure that everyone has all the basic necessities of life, and then can pursue whatever else interests him.

For larger projects (for example space travel) many citizens can pull their energy resource together and have the robots build what is necessary.

One technology used by citizens of the Australia Project are computer body implants that allow instant communication with others, and instant access to world’s information and entertainment. Think Google Glass on steroids.

Although, I found both possibilities somewhat farfetched, it still is interesting to think what the world will be like in the near future as the robotic technology evolves further.