Thursday, March 25, 2010

Youtube down

I've seen that for the first time in years.

Thursday, March 18, 2010

Experimenting with Scala

This is my first useful Scala program. It was ported from a Python program (see below). In 1993 I wrote an equivalent in Modula-2 ;-)

import scala.io.Source;
import java.io.File;
import scala.util.Random;

object voc {
  def train(lst : List[Tuple2[String, String]]) : Unit = {
    println();
    if (lst.length == 0) {
      println(":-D");
      return;
    }

    val shuffled = Random.shuffle(lst);
    val current = shuffled.head;
    println(">>> " + current._1);
    var line = Console.readLine();
    if (line == current._2) {
      println("correct");
      train(shuffled.tail);
    } else {
      print("\007");
      println("wrong - correct is: " + current._2);
      train(shuffled);
    }
  }

  def main(args: Array[String]) {
    val lst = Source.fromFile(new File(args(0))).getLines()
          .map { s => s.split("\t") }
          .filter( l => l.length == 2 )
          .map (l => Tuple2(l.head, l.last))
          .toList;

    train(lst);
  }
}


Python version:

from __future__ import with_statement
import random
import sys

def train(lst):
    while len(lst) > 0:
        random.shuffle(lst)

        lang1, lang2 = lst[0]

        print ">>>", lang1
        try:
            answer = raw_input("answer: ").strip()
        except EOFError:
            # ^Z pressed, ignore
            continue

        if answer == lang2:
            print "correct"
            lst = lst[1:]
        else:
            print chr(7)
            print "wrong - correct is =>", lang2

        print
        print

    print ":-D"

def main():
    filename = sys.argv[1]

    lst = []
    for line in open(filename):
        line = line.strip()
        if not line:
            continue
        parts = line.split("\t")
        assert len(parts) == 2
        lst.append(parts)

    train(lst)


if __name__ == "__main__":
    main()

Sunday, March 14, 2010

PHP, the forgotten programming language

I just took a quiz and had a complete blackout of PHP. Can you name the 25 most popular programming languages? Quiz here.