Scala vals and vars

vals are immutable, vars are mutable, as follows:

val doctor = "I am the doctor"
// the following will error - can't reassign a val
doctor = "David Tennant" 

var who = "Tom Baker"
// this is OK as its a var
who = "Jon Pertwee"


Simple linked list in Perl

This is the simplest linked link implementation I have yet to see. It takes more lines of code in C or Java. In Perl it is so straightforward:


# adapted from Mastering Algorithms in Perl
use strict;
use warnings;

my $squares = undef;

# we put the last element in the linked list first, then add to it.
foreach (reverse 1..5) {
	$squares = [ $squares, $_ * $_ ];

my $sq = $squares->[0][0][1];
print "3rd square: $sq\n";

$sq = $squares->[0][0][0][0][1];
print "5th square: $sq\n";

Exercise: post a singly linked list implementation in a language of your choice.

Fibonacci in Java

Basic recursive fibonacci in Java. Very inefficient. Set N >= 50 to see why.

How can we make this code more efficient when a) still using recursion, b) re-coding iteratively?

public class Fibonacci {
    public static long fibonacci(int n) {
        if (n <= 1) 
        	return n;
        	return fibonacci(n-1) + fibonacci(n-2);
    public static void main(String[] args) {
        int n = 10;
        for (int i = 1; i <= n; i++) {
            System.out.println("N:" + i + ": " + fibonacci(i));

Perl: iterate over an array

Most basic way to iterate (loop over) an array in Perl.


use strict;
use warnings;

# @doctors is the array, the list is the names inside the brackets 
my @doctors = qw(Baker Pertwee Hartnell Troughton Davison McCoy McGann Baker);

foreach my $doc ( @doctors ) {
    print( "Doctor: $doc\n" );


Note: you can use ‘for’ and ‘foreach’ interchangeably – they are synonyms.

Exercise 1: Rework this using a ‘C’ style ‘for’ loop construct.

eval is evil in both Perl and Javascript

So you can do this in both languages:

eval ('$product = 6 * 12');

Avoid this kind of code. It is compiled and executed at runtime. If you are ever unsure what could end up in the dynamic string then this could be a very dangerous thing to do. Avoid entirely, there is always a better way to code this.

Perl list iteration

How to iterate over a list. You can use the Perl sort keyword to pre-sort your list if you wish.


use strict;
use warnings;

my @colours = sort qw( Blue Red Pink Orange Magenta Rose Salmon Black White Maroon Aqua);

foreach my $colour (@colours) {
        print ("$colour\n");


Hello World in Perl

Start from the basics. See if you need to upgrade your version of Perl to the latest release. Most pre-installed versions are usually quite a few versions old. At time of writing the latest version of Perl is: 5.24.1. You can always download the latest version here: Latest Perl


use strict;
use warnings;

print("Hello World\n");


Very simple Fibonacci in Scala

Straightforward fibonacci in Scala, just prints values as it recurses. Think about how you would modify this examples by adding the values to an array and then printing the array at the end.

object Fibonacci extends App {

    println(fibonacci(1, 2))
    def fibonacci(x: Int, y: Int) {
        val next = x + y
        if (next > 1000) System.exit(0)
        fibonacci(y, next)

Blog at

Up ↑