Pretty much every programmer that I've ever met learns new languages by writing a number of small programs in the new language, that should show up the major features of the language and enable the transference of skills from known languages to new ones. Here's a list of my favourite small problems:

The basics: choice, iteration, recursion

- Factorial function
- Factorials are easy, but the trick here is to deal with all the various boundary conditions, preferably using exceptions.
- Fibonacci sequence
- Generating the next Fibonnaci number has a simple recursive solution and a slightly more complex iterative solution which can show up features such as simultaneous assignment.
- The first
*n*primes - Print the first
*n*primes using the

Sieve of Eratosthenes. This is an interesting one -- it has nice solutions both iteratively and recursively. Also, you can do neet things with exceptions to give a solution.

Files and other I/O

- Caesar cipher
- Read in an ASCII sentence and encipher it with the Caesar cipher (add a key to each letter) and print out the enciphered text. Write the converse function to decipher.
- Count the occurances of letter 'a' in a file
- Read a file name in on the console, open the file for reading and count the occurances of 'a' in the file. Print the result (and close the file!).

Linear data structures: arrays and so on

- Binary search
- Look for a value in a sorted structure -- iterative or recursive solutions are both interesting to try out.
- Bubble sort
- The simplist sorting algorithm, but it should give a reasonable idea of how to manipulate mutable structures, or deal with immutable ones if that's all the language has available.

Modularity: modules, classes, objects, etc.

- Sets data structure
- Sets are a simple data structure to implement, they're easy to test and a good instroduction to polymorphism. Union, difference, intersection, etc. all make useful methods and you can play about with mutable or immutable sets and see the difference.

I think all of those examples you gave are somewhere on Rosetta Code: http://rosettacode.org

ReplyDeleteIf I wanted to learn a new language, then I would answer some of their tasks in the new language, without looking at an answer in the new language first.

- Paddy.