Sunday, December 13, 2009

Now you can program the ZX Spectrum

OK, if you just want to be nostalgic and do some BASIC programming on the ZX Spectrum, click on the image below which will run a faithful emulation of the Sinclair ZX Spectrum complete with BASIC. For more information on Sinclair BASIC you can read the manual.

but here is a very simple program to get you started, follow the instructions:

1. type "10 LET a=10" - (note that the spectrum had short-cuts on the keys so immediately you press L it will display "LET" - you might find this weird at first but after you get used to it it's a very fast way to write a program). Then press RETURN (ENTER) - the code will appear at the top of the screen, that is the first line of the program

2. type "20 PRINT a" - (again, pressing 'P' will result in PRINT being displayed). Then press RETURN (ENTER). Your second program line will be displayed at the top of the screen.

to run the program type: 'R' and 'RUN' will be displayed. Press RETURN (ENTER) and the program will run and print "10" - voila!

Here's another program from the manual that demonstrates taking input from the user and the used of FOR loops:

10 LET total=0
20 FOR c=1 TO5
30 INPUT a
40 REM c=number of times that a has been input so far
50 LET total=total+a
60 NEXT c
80 PRINT total




and here is a screenshot of a program with a background of the original Spectrum keyboard:


The ZX Spectrum - Running "The Hobbit"

Below is a ZX Spectrum Emulator (written entirely in Java) and running the game "The Hobbit" which I spent many hours playing when I was 12 years old.

To start playing, just click on the image and press a key.  You can use basic commands such as:

NORTH, SOUTH, EAST, WEST, UNLOCK/LOCK TAKE/DROP, ATTACK, OPEN DOOR etc. and here is the manual if you would like to have a real play :-)

Wednesday, December 9, 2009

Reversing a Double Linked-List in C++ using a 'reference pointer'

Here's an example I wrote for how to reverse a double (or 'doubly' as some people like to say) linked-list. The main point of this example is to show that reference pointers are actually useful in the real world. In this particular example it has two advantages: to avoid having to return a Node* and to make the code clearer. The alternative is to use a Node** or Node*** but the code is less readable.

Here is the example - and because i'm an ex ZX Spectrum junkie, it's using the original speccy font! If you don't know what a ZX Spectrum is, go to http://www.worldofspectrum.org


Saturday, November 28, 2009

A Glorious Dawn

The incomparable Carl Sagan.


Thursday, November 26, 2009

My First Erlang Server - A MathServer

Erlang is a very cool language - OK, it's not the most intuitive to learn if you're a Java/C++ developer but it is extremely powerful and scaleable.  I managed to start 15,000 client sessions against my MathServer on my laptop without any problem whatsoever.  Imagine doing that in Java or C++... not easy to get the kind of performance that Erlang gives you for free.

Here is the code:





and for completeness, this is how you start the server (replace AndrewTosh with the name of your PC):



See the Erlang official website for more details. I'll be doing lots more with Erlang so watch this space...

Saturday, November 21, 2009

MatLab on Windows 7 Tip

In order to run MatLab on Windows 7 you will need to reset your theme to 'Windows Classic'.  If you don't, you will only see the splash screen before the app quickly shuts down:



Notice I have a SuSE Linux desktop background ;-) with a carbon-fibre feel.

How to Avoid Windows 7 'Corrupt Disk' Errors

If you install Windows 7 on an apparently clean and perfectly nice partition created & formatted with Acronis Disk Director (like I did) you will suffer from a stream of seemingly never-ending The disk structure is corrupted and unreadable errors, especially when installing new software.

Don't panic!, your disk is not corrupted.  I too was alarmed at first and immediately started running CHKDSK which didn't help at all because the disk was fine.  The problem is with an incompatibility between Acronis Disk Director (and presumable other Acronis tools that format the disk) and Windows 7.  Now, you can still quite happily install programs on your other partitions (if you still have them) that were created for XP/Vista but not on your new partition that I presume you created explicitly for Windows 7 in the first place - which is just annoying.

There are two ways to solve this problem.

Solution 1.
  1. re-boot into XP/Vista
  2. use the Disk Management console (under Administrative Tools -> Computer Management) tool OR your favourite paritioning tool to delete the Windows 7 partition & re-create it
  3. this is the part that makes the difference.  Using the Windows disk management console, format the new partition (use the normal slow format, NOT the Quick Format)

  4. re-install Windows 7 - be sure to select 'Custom Install' and select your newly formatted partition.  All the 'corrupt disk' error messages will now disappear.  If you do get them at some point, it most likely is a corrupt disk but so far i've installed lots of apps and it's working perfectly
Solution 2.

  1. Using your Windows 7 installation DVD, select a custom install and let the installation format your partition for you.  This is the easiest solution if you have your Windows installation DVD handy.

Thursday, November 19, 2009

C++ Palendrome Detector

Don't ask me why (please don't!) but i've just written a Palendrome detector in C++. If you don't already know, a palendrome is a sentence that reads the same backwards as forwards. Some famous palendromes are:

"A man, a plan, a canal, Panama"
"Madam, I'm Adama"
"Madam, in Eden I'm Adam"

etc.

To test the function I also use the one of the longest palendromes ever created - a 15,139 word palendrome at http://norvig.com/pal1txt.html which I copied into a file and read into a string.

Here is the code:



Wednesday, November 18, 2009

Laurence Krauss - A Universe From Nothing

Laurence Krauss is one of the world's foremost theoretical physicists and a very interesting and funny guy as well. I regularly read his books and view his talks and lectures on physics and cosmology.

more info on Laurence Krauss here:

Google Chrome (Native) Browser on Linux

I've just installed the native Google Chrome (developer edition) on SuSE Linux 11.2. It's fast and easy to use and in my opinion it's better than Firefox. You could install this via WINE but why do that when there's a native version?

All I had to do was execute the following commands in the shell:

sudo rpm -Uvh http://www.benkevan.com/upload/software/google-chrome-unstable-3.0.191.3-1.i386.rpm --nodeps

and

for i in libplds4.so libplc4.so libnspr4.so ; do sudo ln -s /usr/lib/$i /usr/lib/$i.0d; done

I didn't get any errors but if you do please let me know and i'll try to help. This may also work in other Linux distro's.

Tuesday, November 17, 2009

How Does Polymorphism Work Under the Hood?



Not being happy just using Polymorphism (and other OO mechanisms) I wanted to know exactly how it works at a lower level. This is well documented and has been for some time but I like to write my own programs to truly understand what is going on. It can be a painful way to learn, full of frustrations but in the end you truly understand how these seemingly 'magical' (although we all know they aren't) mechanisms work. I'll start with a quick introduction about virtual function tables (VFT) and move on to the example after.

C++ implements dynamic dispatch using virtual function tables (VFTs). VFTs were first used by Simula [DM73] and today are the preferred C++ dispatch mechanism [ES90]. The basic idea of VFTs is to determine the function address by indexing into a table of function pointers (the VFT).

Each class has its own VFT, and each instance contains a pointer to the appropriate VFT. Function names (selectors) are represented by numbers. In the single-inheritance case, selectors are numbered consecutively, starting with the highest selector number used in the superclass. In other words, if a class C understands m different messages, the class’ message selectors are numbered 0..m-1. Each class receives its own dispatch table (of size m), and all subclasses will use the same selector numbers for methods inherited from the superclass. The dispatch process consists of loading the receiver’s dispatch table, loading the function address by indexing into the table with the selector number, and jumping to that function.

I don't like theory alone - most developers learn by doing and not saying. So I wrote the following little C program to illustrate how polymorphism would work using offsets.




Bash Shell Scripting Basic Hints & Tips

A Bash shell script is simply a file containing a set of commands that will be interpreted by the Bash interpreter and executed sequentially.

Here are some tips for creating & executing scripts:

Creating your script:

  1. create a file using your favourite text editor with the extension .sh
  2. always start the file with #!/bin/sh or #!/bin/bash depending on which shell script you have installed on your system. This first line looks like a comment but it's actually call a Shebang and it provides a path to the shell script interpreter to be used to run the script.
  3. don't forget to set the file attributes to allow you to execute the script. There are various ways of doing this. For example, if you want to grant execute permission to yourself you can use: chmod u+x myscript.sh or chmod ugo+x myscript.sh if you want everybody to be able to execute your script. You can also use chmod 777 myscript.sh of course.
Executing your script:

There are two ways to execute your script:

bash$ ./myscript.sh

or

bash$ myscript

The latter only works if the script is located in a directory specified in your PATH variable. The PATH variable is located in your .bashrc file in your $HOME directory. If this file doesn't exist, create a new one and add the path setting, for example if your script is located in $HOME/bin you would add the following to your file:

PATH="$HOME/bin:$PATH"

remeber to save it!, terminate your shell and start a new one as this will force a read of your new or modified .bashrc file.

Remember to put plenty of comments in your script. Apparently, 20% to 50% of the lines in your script should be comments as a guiding rule. You can add a comment to your script by putting a # at the start of a word, for example:

#!/bin/bash
# author: A.Sealy-Bell
# -o specifies the output file
# -lstdc++ links the standard c++ library

gcc -o writetofile write_to_file.cpp -lstdc++

Wednesday, July 15, 2009

A Very Short History of Object-Oriented Programming Languages.

After a little research, here is a rough history of Object-Oriented Programming Languages for those of you that are interested (anyone? :-) )

'SketchPad' Program written by Ivan Edward Sutherland in 1961 - with some influence by Douglas T.Ross from the Lincoln Laboratory. Sketchpad was the first program to implement of 'class' and 'instance-based' inheritance (although not called classes and objects at the time) and abstraction through a graphical interface (the pre-cursor and influence on Xero-PARC and the Mac/Windows Graphical User Interface).

It's pretty amazing to think that in 1961 a program existed that enabled the user to create drawings on-screen with shape 'objects' and contraints that could be applied to your drawing. You could drag 'n' drop shapes and it even had the concept of a graphical 'button' which you could press to get it to execute your command. It used the pre-cursor to the mouse which was a light-pen. This was truly an amazing and ground-breaking program which has had a massive influence on OOP and OOD.

See the original live demo of Sketchpad (in two parts). You can see how Graphical User Interfaces evolved from Sketch. I like the way you can point the light pen directly on the screen - that's pretty cool and amazing for the time of course. For further technical details you might like to read the technical report submitted to Cambridge University.

Douglas T.Ross & C.A.R Hoare both sat on the Algol 68 Committee. Douglas T.Ross works on a record-like data structure (called a plex) which in turn influences C.A.R Hoare's ideas on abstract data types
Nygaard and Dahl cited C.A.R Hoare's idea on abstract data types as the origin of the class denition mechanisms in Simula [7]. Simula was the first Object-Oriented programming language, developed by the Norwegians at the Norwegian Computing Center. At a later stage Simula influenced the development of Smalltalk, a pure OOPL.

Bjarne Stroustrup influenced by Simula 67 & C to write 'C With Classes', then evolved into C++

James Gosling influenced by Modula-3, C++ and other languages develops Java

A bunch of 'dynamic' languages such as GRAILS, Ruby etc. emerge based on Java but with functional and dynamic features

The future....

Friday, May 29, 2009

Ode to Java

Oh Java code that strikes the page

Like wisdom siphoned from a Sage
Like random models fierce with light
That make the darkened worlds grow bright
Now opened to the world's great gifts
Now powered by the minds' new lift
May generics bloom as geeks grow wise
While the Source traps bugs within a vise

Monday, May 25, 2009

Working Remotely - Some Hints & Tips

Many development teams - especially in the Open Source world, are distributed geographically.  There are certain challenges with distributed teams which need special attention but luckily there are some great tools around to aid us in our daily work.  Here are some tools and advice, based on my personal experience that may help you work more effectively when working remotely.

Attitude

I believe the most important aspect of working remotely is a good, collaborative work ethic.  Talk to other developers on the team, don't just e-mail them, pick up the phone! open communication is key and without it everything else falls apart.  I cannot understate the importance of picking up the phone (or Skyping).  It's all too easy when you have a question to send a quick e-mail, move on to something else and forget where you were and the context of the question and problem.  You may think i'm stating the obvious, but think about the case of a geographically distributed development environment.  The decision to e-mail a question can lead to the question not being answered until the next day or even later, hindering the progress on that particular task which may have other unforeseen 'knock on' effects.  So, if at all possible - pick up the phone.

Discipline

It takes a certain amount of personal discipline to work remotely.  It's important to have clear objective which are measurable by management.  The developer should provide regular updates against agreed project deliverables.  This is an important part of gaining the trust of the rest of the dev team and management.

Useful Tools

There are a number of tools which can help a great deal when working remotely.  For example, you can author and co-author documents using Google Documents or Buzzword.  You can share larger documents and spare your inbox with Omnidrive.

Use cases can be developed together using on-line collaboration tools such as PBWorks which also has many other useful features to make teams more productive.

Evolve mind maps with Mind42 or MindMeister.  Need to actually talk to each other? well Skype is the obvious choice but Yuuguu lets you share applications too.  WebEx is also a great tool for on-line conferencing and holding secure meetings.  There are free versions of WebEx available.

Taking collective feedback on design has always been tricky, espcially in a distributed development environment. One of the most useful apps for de-centralised design is ConceptShare which allows online collaboration on design by way of contextual comments, markup and a full history of discussion. Another popular collaboration tool is Campfire.

Tracking the time you've spent on projects can be made easier by using a tool such as Klok - a personal time tracker.  Another tool which can be very useful it RescueTime which "gives your team the ability to see how they spent their time and helps them spend it more productively.  Team members have full control over their data.

One great tool for developers - if you happen to use the IntelliJ IDEA is IDE Talk which is instant messaing for developers within the IDE.  I've found this very useful.

If you want to send large files, i've found YouSentIt very useful.

Accessing Your Office PC/Network

It may be necessary at times to access your office network or PC.  Normall, your company will provide you with VPN access to allow you to access the company network remotely.  You can also use tools such as GoToMyPC or VNC, which I have used extensively and found very useful when accessing my office PC.

Thursday, February 19, 2009

A Short Modern Definition of Evolution & Natural Selection

Evolution is a change in gene frequencies (%) in the gene pool (the population of individuals).

Natural Selection is the biased change in gene frequencies - biased in the direction of improvement, improvement for survival and reproduction of those very same genes.

Unweaving the Rainbow, by Richard Dawkins

We are going to die, and that makes us the lucky ones. Most people are never going to die because they are never going to be born. The potential people who could have been here in my place but who will in fact never see the light of day outnumber the sand grains of Sahara. Certainly those unborn ghosts include greater poets than Keats, scientists greater than Newton. We know this because the set of possible people allowed by our DNA so massively outnumbers the set of actual people. In the teeth of these stupefying odds it is you and I, in our ordinariness, that are here.
RichardDawkins.net

Books I am reading this book at the moment:

Atheist

The Out Campaign: Scarlet Letter of Atheism