I have started reading the Scala book (which doesn’t seem to have its own URL, but is for sale at the artima.com shop) and I have two remarks on programming-language books.

  1. In any such book, the first example program should print the string “Hello world.” There are no exceptions to this rule, except that an exclamation mark may replace the closing period.

  2. I am getting really, really bored with factorial and Fibonacci algorithms. It is really remarkably infrequent that I implement any code that looks much like either.

[Update: DeWitt Clinton disagrees, pounding Fibonacci to a bloody pulp with erudition and grace.]


Comment feed for ongoing:Comments feed

From: Bob Aman (May 19 2008, at 16:19)

Yeah, I have to agree at least on the Fibonacci thing. I'd love to see those replaced with an example of how to use map or reduce (assuming the language features those).


From: John Cowan (May 19 2008, at 16:27)

Actually, there's a good deal of flexibility in the tradition:

hello, world [K&R]

Hello, world!

HEllO WORld [Malbolge]

Saluton mondo! [Esperanto]

Namaskaram, lokame [Malayam in Latin script]

See http://helloworldsite.he.funpic.de/index.htm and http://en.wikibooks.org/wiki/List_of_hello_world_programs


From: Tkil (May 19 2008, at 16:54)

I find your two comments mildly self-contradictory. :)

You want "Hello world" because it's one of the simplest (and traditional). That exact simplicity is what makes it handy: you know how to do it in a zillion other languages, so you can immediately see what makes the new language different.

I'd defend Fibonacci and factorial on the same grounds: they're dead-simple, you already know how to do it in zillions of languages, and doing it again in a new language lets you see what makes the new language different.

(As well as set a certain low bar; consider what would happen if you ran into a book on pre-Fortran90 Fortran: they can't do recursion, so you wouldn't have those examples...)

What would you suggest as a sample recursive algorithm?


From: Glenn Vanderburg (May 19 2008, at 17:38)

The worst offenders in both cases are books about functional languages, and of course this reflects the authors' desire to highlight the best parts of those languages and defer discussion of the awkward bits.

But your principles are spot on, and functional programming books especially should heed them, because if those books don't start with simple programs of the sort that most programmers need to write every day (i.e., programs that do I/O), all the mathematical elegance in the world won't help. Because, of course, nobody needs to write a fibonacci program.


From: Justin Sheehy (May 19 2008, at 17:49)

Haskell might be the only major language in which it's interesting to show a definition of the fibonacci sequence in an introduction, as doing so demonstrates a fundamental aspect of the language:

fibs = 0 : 1 : zipWith (+) fibs (tail fibs)


From: Jamie (May 19 2008, at 21:04)

The only recursive code I can recall writing in the last couple of years is walking through XML elements. Perhaps that should be the new Fibonacci?

(I tell a lie, perhaps once a year I write a binary search tree, or similar).


From: Norbert (May 19 2008, at 21:15)

Actually, the first example program probably should print the string “你好世界.” If a programming language doesn’t support Unicode and the world’s most popular human language out of the box, it’s not really fit for the 21st century.


From: bryan (May 20 2008, at 00:12)

norbert's comment is pretty damn funny. Anyway, I hate hello world examples. Fibonacci are least somewhat interesting.


From: Henry (May 20 2008, at 06:20)

I agree that Fibonacci is almost as traditional as "Hello World" and can be useful for that reason.

Here is Justin's example literally translated into Scala:

def fib: Stream[Int] = Stream.cons(0, Stream.cons(1, fib zip fib.tail map {case(x,y) => x + y}))

It's not such a basic pattern is Scala, but it's there.


From: Mike Mondragon (May 20 2008, at 11:40)

the teapot is an acceptable replacement for "Hello World" if its a graphics programming book, correct?


From: Gavin Bong (May 20 2008, at 20:48)

I beg to differ. A hello world program has no utility. You can only learn how to bootstrap a minimal program.

With a fibonacci function (see http://www.nabble.com/Constraints-on-function-method-params-to16832593.html);

you might learn (in scala) about:

a) memoizing function results for performace

b) the require(..) guard macro

c) guarded pattern match case statements.

You can find Hello worlds in a multitude of languages at http://helloworldsite.he.funpic.de/


From: Tony Fisk (May 20 2008, at 23:50)

Is Norbert saying 'hello world' in chinese, or is that the first numbers in the Fibonacci sequence? (fair point, though)

Not a teapot for graphics, but something simpler: a simple solid of revolution, such as a sphere

Next, try mapping a simple texture to your sphere, a la Mercator....

Hello, what's this?


From: Erik Engbrecht (May 21 2008, at 07:34)

Expanding on your theme...

The hello world example should be something (or build up to something) that produces the languages equivalent of an executable.

So an example the just prints hello world in an interpretter is bogus. A big piece of the hello world program is showing you how to build and the program, not just show the necessary code to print "Hello, World!" to the console. I think a lot of scripting languages (and maybe Scala...haven't bought the book) give a false sense of simplicity (and confuse those of us who have actually, say, write a program) by initially focusing on the interpretter instead of on the minimal structure of a complete program.


From: John Cowan (May 22 2008, at 11:21)

In addition, the point of "hello, world" programs is that you should copy the code into your computer, do whatever magic is used to get it running, and make sure the right thing comes out. Learning that magic is a significant hurdle to adopting any new language.


author · Dad
colophon · rights
picture of the day
May 19, 2008
· Technology (90 fragments)
· · Coding (98 more)

By .

The opinions expressed here
are my own, and no other party
necessarily agrees with them.

A full disclosure of my
professional interests is
on the author page.

I’m on Mastodon!